Sunday, 15 March 2015

c++ - Printing the largest 3 numbers -



c++ - Printing the largest 3 numbers -

i beginner @ c++ , have nail wall in problem. know i'm few lines off making programme work, have tried could. know has assignments after "else" statement. appreciate help.

i have create programme come in 10 numbers, , in end prints out largest 3 of 10 numbers. here's have @ moment:

int biggest; int second; int third; int number; int c; biggest=0, second=0; cout<<"enter 10 numbers: "; for(c=0;c<=9;c++) { cin >>number; if(number>biggest) { third=second; second=biggest; biggest=number; }else{ second=number; if(number<=second) third=second; } } cout<< "largest number is: " << biggest << " sec largest number is: " <<second <<" 3rd largest is: " << 3rd <<endl;

thank you!

link: http://ideone.com/4p8ax0

#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { vector<int> vec; int value = 0; for(int i=0; i<10; i++){ cin >> value; vec.push_back(value); } std::sort(vec.begin(),vec.end()); cout << "biggest" << vec[9] << endl; cout << "2nd biggest" << vec[8] << endl; cout << "3rd biggest" << vec[7] << endl; homecoming 0; }

whole programme enjoy

c++

No comments:

Post a Comment