Find largest number position

I have to write program using an array enter numbers and then determine largest number's position, For example if i enter 5 7 9 1 6 it should return 2 since 9 is largest number. this what i have,but it always returns 0. cant find an error



[code]
#include <iostream>
#include <fstream>
using namespace std;
const int SIZE=100;
ofstream output("output.doc");
int main()
{
int n,pos,largesofar;
int number[SIZE];
cout<<"How many numbers would you like to enter?"<<endl;
cin>>n;
cout<<"Please enter the numbers"<<endl;


for( int i = 0 ; i < n ; i++ )
{
cin >> number[i];
if( i == 0 )
{
largesofar = number[i];
pos = 0 ;
}
else if(number[i] > largesofar) {
largesofar = number[i];
pos = i ;
}
}

cout<<"The position that had the largest number is "<<pos<<endl;
system("pause");
return 0;
}
[code]
Last edited on
Please put your code inside source code braces.
like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>
using namespace std;
const int SIZE=100;
ofstream output("output.doc");
int main()
{
int n,pos,largesofar;
int number[SIZE];
cout<<"How many numbers would you like to enter?"<<endl;
cin>>n;
cout<<"Please enter the numbers"<<endl;


for( int i = 1 ; i < (n+1) ; i++ )
{
cin >> number[i];
if( i == 1 )
{
largesofar = number[i];
pos = 1 ;
}
else if(number[i] > largesofar) {
largesofar = number[i];
pos = i ;
}
}

cout<<"The position that had the largest number is "<<pos<<endl;
system("pause");
return 0;
}


anyways the size of your array is 0 to n-1. i modified it to ignore the o address array storage. it works fine now.(",)
Last edited on
I have to write program using an array enter numbers and then determine largest number's position, For example if i enter 5 7 9 1 6 it should return 2 since 9 is largest number. this what i have,but it always returns 0. cant find an error

Are you sure about that ?
Because your code gives correct result to me .
Last edited on
Topic archived. No new replies allowed.