Find max number from a set of n numbers !!!

Mar 21, 2015 at 10:31pm
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.
Mar 21, 2015 at 11:49pm
because you assign same value X to Y and Z
Mar 21, 2015 at 11:50pm
For-loops dont work that way - https://www.youtube.com/watch?v=sBO8yvyyBI0


Mar 21, 2015 at 11:50pm
Thats because i have to also find the second largest number...
Mar 22, 2015 at 12:23am
you dont need const sentinel, just use 0.

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.

still think about how to get second largest.


Mar 22, 2015 at 2:47am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <algorithm>
using namespace ::std;

int main()
{
    const int 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/swap
    if( 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' ;
}
Mar 22, 2015 at 5:58am
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...
Mar 22, 2015 at 6:13am
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
Mar 22, 2015 at 9:50am
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
    const int 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.
Mar 22, 2015 at 1:16pm
Worked like a charm !!! Thanks...
Topic archived. No new replies allowed.