I'm making a program that determines the largest number from the 10 loops. How can I make it determine the largest from the 10 loops. Here's the program I'm doing. *NOTE* I mean loops as in after you build and run it, it will make you enter an number 10 times.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* 2.20 Make a program that willl inputs a series of 10 numbers,and determines and prints the largest of the
numbers. */
#include<iostream>
usingnamespace std;
int main()
{
int counter,number,largest;
counter = 0;
while ( counter <= 10 )
{
cout << "Enter a number: ";
cin >> number;
}
return 0;
}
/* 2.20 Make a program that willl inputs a series of 10 numbers,and determines and prints the largest of the
numbers. */
#include<iostream>
usingnamespace std;
int main()
{
int counter,number,largest;
counter = 0;
while ( counter < 10 )
{
cout << "Enter a number: ";
cin >> number;
if ( number > largest )
{
largest = number;
cout << endl;
cout << "The largest number is: " << largest;
}
counter++;
}
return 0;
}
/* 2.20 Make a program that willl inputs a series of 10 numbers,and determines and prints the largest of the
numbers. */
#include<iostream>
usingnamespace std;
int main()
{
int counter,number,largest;
counter = 0;
while ( counter <= 10 )
{
cout << "Enter a number: ";
cin >> number;
if ( number > largest )
{
largest = number;
}
counter++;
}
cout << endl;
cout << "The largest number is: " << largest;
return 0;
}
/* 2.20 Make a program that willl inputs a series of 10 numbers,and determines and prints the largest of the
numbers. */
#include<iostream>
usingnamespace std;
int main()
{
int counter,number,largest,large2;
counter = 0;
largest = 0;
while ( counter <= 10 )
{
cout << "Enter a number: ";
cin >> number;
if ( number > largest )
{
largest = number;
}
if ( number > large2 )
{
large2 = number;
}
counter++;
}
cout << endl;
cout << "The largest numbers is: " << largest << " and " << large2;
return 0;
}
OK same problem as before - you need to initialise your variables.
I like to declare, initialise and comment all on 1 line like this:
1 2 3 4
int counter = 0; //count the numbers
int number = 0; // A particular number
int largest = 0; //The largest number
int large2 = 0 //The second largest number;
It makes it much easier for someone else to understand what all your variables mean.
Now think about how to do this. If you find a new largest number, what happens to the old largest number?
Say you have found a number that is bigger than the existing largest number. This will be the new largest number. The old largest number go into which variable?