cplusplus.com cplusplus.com
cplusplus.com   C++ : Forums : Beginners : finding greatest character
  Search:
- -
C++
Information
Documentation
Reference
Articles
Sourcecode
Forums
Forums
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Articles
Lounge
Jobs

-

post  finding greatest character

tyler (22)
i'm new to c++ and i've been trying to figure out a way of finding out the largest number after inputing > 25 numbers. the use of if..else statements doesnt seem to be viable at all.
any suggestions?
|
Nandor (82)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
cout<<"Enter 25 numbers";
int input[25];
int aux;
for(aux=0;aux<25;aux++)
{
     cin>>input[aux];
}
vector<int>vctr(input,input+25);
vector<int>::iterator it;
sort(vctr.begin(),vctr.end());
it=vctr.end();
cout<<"The highest is:";
cout<<*it;
}
| Last edited on
Faldrax (310)
Or alternatively
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
   cout<<"Enter 25 numbers";
   int input[25];
   int aux,highest=0;
   for(aux=0;aux<25;aux++)
   {
      cin>>input[aux];
      if (input[aux]>highest)
      {
          highest = input[aux];
      }
   }
   cout<<"The highest is:";
   cout<<highest;
}
| Last edited on
bnbertha (404)
Nandors version won't work, at least not without a slight modification. vctr.end() does not return the last element in the vector, it indicates that you have gone past the last element. To make it work you would have to decrement the iterator by one before trying to dereference it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
cout<<"Enter 25 numbers";
int input[25];
int aux;
for(aux=0;aux<25;aux++)
{
     cin>>input[aux];
}
vector<int>vctr(input,input+25);
vector<int>::iterator it;
sort(vctr.begin(),vctr.end());
it=vctr.end();
it--; // move the iterator back to the last element
cout<<"The highest is:";
cout<<*it;
}
|
NTPY (1)
int main()
{
cout<<"Enter 25 numbers\n";
int input[25];
int aux;
int high=0;
for(aux=0;aux<5;aux++)
{
cin>>input[aux];
if(input[aux]>high)
high=input[aux];
}
cout<<"The highest is: ";
cout<<high;
getche();
}
|
tyler (22)
thanks a lot for the answers everybody
|

This topic is archived - New replies not allowed.
Home page | Privacy policy
© cplusplus.com, 2000-2008 - All rights reserved - v2.2
Spotted an error? contact us