Hello,
I am trying to solve the Stanford course materials at home Chapter1 ex 5 course cs106bx. I am a Beginner , new to programming. The question is: cout the max number in a set of numbers. Complex forms of data manipulation have not read yet, like arrays etc.
Here is the code I have written:
#include <iostream>
using namespace::std;
int main()
{
const int SENTINEL = 0;
cout<<" Please input integers to compare :- "<<endl;
cout<<" (Enter "<<SENTINEL<<" to signal end of list) "<<endl;
int x = 0; int y = 0; int z = 0; int i = 0;
cin>>x;
while (true)
for (x!=SENTINEL; ) break;
if (x>y) y=x; z=x;
cout<<"Largest number is "<<z;
cout<<"Total numbers compared are "<<i;
//cout<<"Largest number is "<<y;
// cout<<"Second largest number is "<<larger;
return 0;
}
The question is to find the largest number and the second largest number. But I always get the largest number as the last input number.
for the while loop, change condition to x != 0; remember to put {} for compiler to know where to end. put one cout in while to tell use how to end the program, and another cin to get input.
in this way, at least you can get a largest number.
#include <iostream>
#include <algorithm>
usingnamespace ::std;
int main()
{
constint SENTINEL = 0;
cout << "Please input integers to compare :- \n""(Enter " << SENTINEL << " to signal end of list) " << endl;
int largest = 0;
int second_largest = 0;
cin >> largest >> second_largest ; // read in the first two numbers
// http://en.cppreference.com/w/cpp/algorithm/swapif( largest < second_largest ) swap( largest, second_largest ) ;
int number ;
int cnt = 2 ; // we already have two to start with
while( cin >> number && number != SENTINEL )
{
++cnt ;
if( second_largest < number )
{
second_largest = number ;
if( largest < second_largest ) swap( largest, second_largest ) ;
}
}
cout << "Largest number is " << largest
<< "\nThe second largest number is " << second_largest << '\n'
<< "Total numbers compared are " << cnt << '\n' ;
}
JLBorges, thanks for the solution. But I am just at the first chapter, algorithm has not been introduced yet. I suppose, I will need something more logical.
Just an idea should be enough, I'd like to solve this myself...
While the above solution will work for non-negative input it will not work if the input was strictly negative numbers. A better solution will be to read the first 2 numbers separately and assign those to your largest and second_largest variables, then continue reading the numbers and comparing them to the 2 values you currently have
Lines 16 and 27:
Replace if( largest < second_largest ) swap( largest, second_largest ) ; with
1 2 3 4 5 6 7
if( largest < second_largest )
{
// swap the values of largest and second_largest
constint temp = largest ;
largest = second_largest ;
second_largest = temp ;
}
Ignore the post above this one; Smac89 is completely wrong about this. The current solution does work for for strictly negative numbers; the proffered 'better' solution happens to be identical to the current solution.