简体   繁体   中英

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] . 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 .

So when you have i <= 11 in your loop; 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.

numbers [x] = {num}; would be better written as 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.

If you want to putout the location then do 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; does that. You could go 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. In your case, x has been initialized to zero. So, the first item of the array gets set to num and

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!! for finding the index you should do a loop on your array and compare the array[i] and the 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

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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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