接受用户输入,搜索其范围值位于数组中的位置并返回2d数组的行索引

时间:2021-10-11 15:41:46

This is my 2d array with available speeds listed. this code I must keep. If my user enters 75349 as their clock speed input, how will I be able to recognize that speed and return the value of the row index ?... which is row 2 int the 2d array because it lies between 7500- 14900

这是我列出的可用速度的二维数组。这个代码我必须保留。如果我的用户输入75349作为其时钟速度输入,我将如何识别该速度并返回行索引的值?...这是2d数组中的第2行,因为它位于7500- 14900之间

int UserClockSpeedInput;

const uint32 SpeedTable[5][2] {
{15000, 99990}, //between 15k - 99.99k
{7500, 14900}, //between 7.5k - 14.9k
{3500,7400},
{1900,3400},
{6000,1800}
}

I want to return the row index of whatever value the user entered, these are the set values I already set in place. They are not allowed to enter any other values

我想返回用户输入的任何值的行索引,这些是我已经设置的设置值。他们不允许输入任何其他值

for(i=0;i<5;i++)
{
    for(j=0;j<2;j++)
    { 

 //not sure of what to do from here.

2 个解决方案

#1


0  

how will I be able to recognize that speed and return the value of the row index

我将如何识别该速度并返回行索引的值

Solution :

You can simply achieve it by using the following for loop

您可以通过使用以下for循环来实现它

    int number; //variable to hold user's input

    //scanning user's input
    printf("enter any number : ");
    scanf("%d",&number); 

    //for loop to determine position
    for (int index = 0; index < 5; index++)
    {
        if( (number >= SpeedTable[i][0]) && (number <= SpeedTable[i][1]) )
        {
            break;
        }
    }

    //printing index
    printf("return index value : %d ",i);

Suggestions :

firstly,

  • I think your last element of the 2-D array must not be {6000,1800},
  • 我认为二维数组的最后一个元素不能是{6000,1800},

  • I think it must instead be {600,1800}
  • 我认为它必须是{600,1800}

and,

  • Don't use void main(). to know why, see this : click
  • 不要使用void main()。要知道原因,请看:点击

  • Instead use int main(void). to know why, see this : click
  • 而是使用int main(void)。要知道原因,请看:点击


so altogether your code would be :

所以你的代码完全是:

#include <stdio.h>

int main(void){

    const unit32 SpeedTable[5][2] = {
        {15000, 99990}, //between 15k - 99.99k
        {7500, 14900}, //between 7.5k - 14.9k
        {3500,7400},
        {1900,3400},
        {600,1800} //between 0.6k - 1.8k
    };

    int number;

    printf("enter any number : ");
    scanf("%d",&number);

    int i;

    for (i = 0; i < 5; i++)
    {
        if( (number >= SpeedTable[i][0]) && (number <= SpeedTable[i][1]) )
        {
            break;
        }
    }

    printf("return index value : %d ",i);

}

#2


-1  

You want to check the input against the upper and lower bounds that you have provided in your arrays. You should check to see if the input is greater than the 1st element, and less than the 2nd element.

您希望根据数组中提供的上限和下限检查输入。您应该检查输入是否大于第1个元素,并且小于第2个元素。

#include <stdio.h>

void main(){
    const int SpeedTable[5][2] = {
        {15000, 99990}, //between 15k - 99.99k
        {7500, 14900}, //between 7.5k - 14.9k
        {3500,7400},
        {1900,3400},
        {6000,1800}
    };

    int user_input = 7534;

    int i;

    for (i = 0; i < sizeof(SpeedTable)/ (2 * sizeof(int)); i++){
        if (SpeedTable[i][0] < user_input && SpeedTable[i][1] > user_input){
            break;
        }
    }

    printf("%d\n", i);
}

#1


0  

how will I be able to recognize that speed and return the value of the row index

我将如何识别该速度并返回行索引的值

Solution :

You can simply achieve it by using the following for loop

您可以通过使用以下for循环来实现它

    int number; //variable to hold user's input

    //scanning user's input
    printf("enter any number : ");
    scanf("%d",&number); 

    //for loop to determine position
    for (int index = 0; index < 5; index++)
    {
        if( (number >= SpeedTable[i][0]) && (number <= SpeedTable[i][1]) )
        {
            break;
        }
    }

    //printing index
    printf("return index value : %d ",i);

Suggestions :

firstly,

  • I think your last element of the 2-D array must not be {6000,1800},
  • 我认为二维数组的最后一个元素不能是{6000,1800},

  • I think it must instead be {600,1800}
  • 我认为它必须是{600,1800}

and,

  • Don't use void main(). to know why, see this : click
  • 不要使用void main()。要知道原因,请看:点击

  • Instead use int main(void). to know why, see this : click
  • 而是使用int main(void)。要知道原因,请看:点击


so altogether your code would be :

所以你的代码完全是:

#include <stdio.h>

int main(void){

    const unit32 SpeedTable[5][2] = {
        {15000, 99990}, //between 15k - 99.99k
        {7500, 14900}, //between 7.5k - 14.9k
        {3500,7400},
        {1900,3400},
        {600,1800} //between 0.6k - 1.8k
    };

    int number;

    printf("enter any number : ");
    scanf("%d",&number);

    int i;

    for (i = 0; i < 5; i++)
    {
        if( (number >= SpeedTable[i][0]) && (number <= SpeedTable[i][1]) )
        {
            break;
        }
    }

    printf("return index value : %d ",i);

}

#2


-1  

You want to check the input against the upper and lower bounds that you have provided in your arrays. You should check to see if the input is greater than the 1st element, and less than the 2nd element.

您希望根据数组中提供的上限和下限检查输入。您应该检查输入是否大于第1个元素,并且小于第2个元素。

#include <stdio.h>

void main(){
    const int SpeedTable[5][2] = {
        {15000, 99990}, //between 15k - 99.99k
        {7500, 14900}, //between 7.5k - 14.9k
        {3500,7400},
        {1900,3400},
        {6000,1800}
    };

    int user_input = 7534;

    int i;

    for (i = 0; i < sizeof(SpeedTable)/ (2 * sizeof(int)); i++){
        if (SpeedTable[i][0] < user_input && SpeedTable[i][1] > user_input){
            break;
        }
    }

    printf("%d\n", i);
}