(Find the Two Largest Numbers) Using an approach similar to that in Exercise 4.1 7, find the two largest values among the 1 0 numbers. [Note: You must input each number only once.]
#include < cstdlib>
#include < iostream>
using namespace std;
int main()
{
//инициализация переменных
int counter = 1;
int largest = 0;
int largest2 = 0;
int number;
//выполнение цикла со счетчиком
while( counter <= 10)
{
cout << "Enter number: ";
cin >> number;
if( (number > largest) & & (number > largest2) )
{
largest = largest2;
largest2 = number;
}
else
if( (number > largest) & & (number < largest2) )
{
largest = number;
}
cout << "Largest two numbers are " << largest << " and " << largest2 << endl;
counter++;
}
cout << "Largest two numbers are " << largest << " and " << largest2 << endl;
//for pause
system("PAUSE") ;
return 0;
}
Solution:
#include < cstdio>#include < cstdlib>
#include < iostream>
using namespace std;
int main()
{
//инициализация переменных
int counter = 1;
int largest = 0;
int largest2 = 0;
int number;
//выполнение цикла со счетчиком
while( counter <= 10)
{
cout << "Enter number: ";
cin >> number;
if( (number > largest) & & (number > largest2) )
{
largest = largest2;
largest2 = number;
}
else
if( (number > largest) & & (number < largest2) )
{
largest = number;
}
cout << "Largest two numbers are " << largest << " and " << largest2 << endl;
counter++;
}
cout << "Largest two numbers are " << largest << " and " << largest2 << endl;
//for pause
system("PAUSE") ;
return 0;
}
Post A Comment:
0 comments: