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]
#include <iostream>
#include <fstream>
usingnamespace std;
constint 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 ;
}
elseif(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.(",)
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 .