array output

hello everybody!
I got a very big problem on inputting nd displaying the arrays. As Im new to programming specially to this topic..this is the question nd the code

Write a program that populates an array of size 20 with random integer numbers between the ranges of 1 to 100 (inclusive). Then repeatedly asks the user to enter a number. For each number entered, display the position of that number in the array. If the number doesn’t exist, display a message indicating that the number is not found. If the number appears more than one time, in the array, display all the positions. The user should be asked to enter a sentinel value if no more search is needed.



include<iostream>
using namespace std ;
const int no=20 ;
int main()
{
int array[20]={12,22,56,45,78.96,63,74,55,77,99,88,33,22,11,44, 72,29,31,30} ;
int i , val ;

for ( i = 0; i<no ; i++ )
{
cout << " please enter a value " << endl ;
cin>> val ;

for ( int i = 0 ; i<array[no-1] ; i++ )
{
cout << i << endl ;
}
}

return 0 ;
}
a program that populates an array of size 20 with random integer numbers between the ranges of 1 to 100 (inclusive)
You will need to use rand and srand

http://www.cplusplus.com/reference/clibrary/cstdlib/rand/

For each number entered, display the position of that number in the array.
So you will have the user enter numbers, and if that number is in the array, output the position of the number.
1
2
3
4
5
for (int i=0; i < 20; i++)
{
   if (array[i] == val)
      cout << "Position: " << i;
}
Use code tags


for ( int i = 0 ; i<array[no-1] ; i++ )
{
cout << i << endl ;
}

This only prints the value of i for 20 times

I wrote the code but do your homework

The above code won't print a message if the number is not in the array though
Last edited on
Topic archived. No new replies allowed.