简体   繁体   English

数组C ++获取值

[英]Arrays C++ getting values

Working on an exercise that wants to print out the index position of the number in the array once the user inputs what value he chooses. 一旦用户输入了他选择的值,就需要进行练习以打印出数字在数组中的索引位置。

Here's the code so far but don't know how to organize it to print of the index position of the number they put in from the list of the array 到目前为止,这是代码,但不知道如何组织它以打印从数组列表中输入的数字的索引位置

#include <iostream>

using namespace std;

int main()
{
    int numbers [11] = {5, 2, 11, 6, 33, 756, 32, 0, 1, 31, -1,};
    int num = 0;
    int x = 0;

    cout << "List:" ;
    for (int i = 0; i <= 11; i++)
    {
        cout << numbers [i] << ", ";
    }
    cout << endl;
    cout << "Enter a number from the list to find its position: ";
    cin >> num;

    numbers [x] = {num};
    cout << numbers [x];
}

basically all its doing now is printing out the number you put in instead of the location it is in the array.... 基本上现在要做的就是打印您输入的数字,而不是它在数组中的位置。

also how do you input values into an array from the user input 还如何从用户输入中将值输入到数组中

First, this is Undefined behavior, 首先,这是未定义的行为

for (int i = 0; i <= 11; i++) // will iterate from 0 to 11

Arrays range starts from 0 , so if you want to loop through it or need to go to last element, you should access Array[MaxNum-1] . 数组范围从0开始,因此如果要遍历它或需要转到最后一个元素,则应访问Array[MaxNum-1] So you should be using, 所以你应该使用

for (int i = 0; i < 11; i++) // will iterate from 0 to (11-1)

Now coming to your doubt:: 现在让您怀疑:

You should iterate through entire array, find your number and then print the index as following:: 您应该遍历整个数组,找到您的编号,然后按如下所示打印索引:

int nIndex = -1 ;
for (int i = 0; i < 11; i++ )
{
   if ( numbers[i] == num )
   {
      nIndex = i ;
      break ; 
      // Or to end the loop, you can set i = SomeValueToBreakTheCondition
      // ex., i = 11
   }
}

if( nIndex == -1 )
{
   std::cout << Element Not Found ;
}
else
{
   std::cout << "Element Found Out Index::" << nIndex ;
}

When you write int numbers[11] , that is an array of 11 elements whose indices are 0 through 10 . 当您编写int numbers[11] ,它是11元素的数组,其索引是010

So when you have i <= 11 in your loop; 因此,当您的循环中i <= 11时; the last loop iteration reads beyond the end of the array, causing undefined behaviour. 最后一个循环迭代读取数组末尾之外的内容,从而导致未定义的行为。 Change this to i < 11 , or even better i < sizeof numbers / sizeof numbers[0] , which you can wrap in a macro if you think it looks nice. 将其更改为i < 11 ,甚至更好的是i < sizeof numbers / sizeof numbers[0] ,如果您认为它不错,可以将其包装在宏中。

numbers [x] = {num}; would be better written as numbers[x] = num; 最好写成numbers[x] = num; . Anyway, you then go: cout << numbers[x] which does exactly what you say: it puts out the number at the location indexed by x , which you just stored num in. 无论如何,然后您去: cout << numbers[x]完全符合您的意思:它将数字存储在x索引的位置,您刚刚将num存储在其中。

If you want to putout the location then do cout << x; 如果要放置位置,请执行cout << x; .

how do you input values into an array from the user input 您如何从用户输入中将值输入到数组中

You're already doing that , cin >> num; numbers[x] = num; 您已经在执行cin >> num; numbers[x] = num; cin >> num; numbers[x] = num; does that. 做到这一点。 You could go cin >> numbers[x]; 你可以去cin >> numbers[x]; directly. 直。 If you run a loop then you can input several numbers in a row, for example: 如果运行循环,则可以连续输入多个数字,例如:

for( x = 0; x < 11; ++x )
    cin >> numbers[x];

You are not checking in which index the given number is. 您没有检查给定数字在哪个索引中。 The statement: 该声明:

numbers [x] = {num};

simply assigns num to the x-th item of the array. 只需将num分配给数组x-th项目。 In your case, x has been initialized to zero. 在您的情况下, x已初始化为零。 So, the first item of the array gets set to num and 因此,数组的第一项设置为num

cout << numbers [x];

always prints the first item of the array. 总是打印数组的第一项。

You can fix this by replacing the lies 您可以通过替换谎言来解决此问题

numbers [x] = {num};
cout << numbers [x];

by 通过

for (int i = 0; i < 11; ++i )
{
   if ( numbers[i] == num )
   {
      cout << i << endl;
      break;
   }
}
if ( i == 11 )
{
   cout << "Number not found" << endl;
}

1- this program does not find the index!! 1-该程序找不到索引!! for finding the index you should do a loop on your array and compare the array[i] and the x 为了找到索引,您应该在数组上循环并比较array [i]和x

for(int i = 0 ; i < 10 ; i++)
{ 
     if(number[i] == x)
     {
           // do what you want with index i
     }
}

2- for inputting an array, you can do something like this 2-对于输入数组,您可以执行以下操作

for(int i = 0 ; i < 10 ; i++) {    cin >> numbers[i];    }

You can make a loop to find the index by yourself, also you can use std::find : 您可以循环查找自己的索引,也可以使用std::find

int* pos = find(&numbers[0], &numbers[11], num);
if (pos == &numbers[11]) {
    cout << "not found\n";
} else {
    cout << (pos - &numbers[0]) << endl;
}

In addition, you should change 另外,你应该改变

for (int i = 0; i <= 11; i++)

to

for (int i = 0; i < 11; i++)

to avoid array index goes out of range. 避免数组索引超出范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM