program using array need to find the position of the number

please take a look at my current work. thanks

#include <iostream>
using namespace std;

int main()
{
int position, innum, n, i, a[100];
cout << "Please input a number: ";
cin >> n;
cout << "\nPlease input the " << n << " numbers: \n";
for (i=0; i<n; i++)
cin >> a[i];
cout << "\nThe following are the list of numbers: \n";
for (i=0; i<n; i++)
cout << " " << a[i];
cout << "\n\nEnter number to be search: \n";
cin >> innum;
innum == a[i];//nevermind this much for im still in deep confusion as to how to go about this
cout << "Number " << innum << "is present at position " << innum;

system("pause>0");
return 0;
}

this should be the output:
input n: 8 (n) 8 is a variable

enter 8 numbers
100 53 84 320 3 12 43 121

enter number to be search: 320
number 320 is present at position 4
number 320 is present at position 8
Code tags...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main()
{
int position, innum, n, i, a[100];
cout << "Please input a number: ";
cin >> n;
cout << "\nPlease input the " << n << " numbers: \n";
for (i=0; i<n; i++)
cin >> a[i];
cout << "\nThe following are the list of numbers: \n";
for (i=0; i<n; i++)
cout << " " << a[i];
cout << "\n\nEnter number to be search: \n";
cin >> innum;
innum == a[i];//nevermind this much for im still in deep confusion as to how to go about this
cout << "Number " << innum << "is present at position " << innum;

system("pause>0");
return 0;
}


innum == a[i];//nevermind this much for im still in deep confusion as to how to go about this

Just loop over the a[] array and over each iteration test if a[i] == innum.
If it is == then cout << "Number " << innum << "is present at position " << i;

You should also always (if possible) have 'for' loops like this:
for (int i=0; i<n; i++) //declare 'int i' in the loop itself to keep things local
Last edited on
do you mean im gonna have to put an if else loop before or after the for loop?
please bear with me with this one thanks!
1
2
3
4
5
6
7
8
9
10
11
12
13
//from line 16
cin >> innum;
for(int i=0; i<n; i++)
{
   if(a[i] == innum)
   {
       cout << "Number " << innum << " is present at position " << i;
   }
}

system("pause>0");
return 0;
}
Topic archived. No new replies allowed.