Determine the largest and the smallest

This is a discussion on Determine the largest and the smallest within the C++ Programming forums, part of the General Programming Boards category; As title, I'm just a new C++ beginner (a.k.a noob), currently I have an exercise to determine the largest and ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    2

    Unhappy Determine the largest and the smallest

    As title, I'm just a new C++ beginner (a.k.a noob), currently I have an exercise to determine the largest and the smallest from 3 different integers.

    Code:
    // Read three different integers and hence determine the largest and the smallest integer
    #include <iostream> // program performs input and output
    
    	using std::cout; // program uses cout
    	using std::cin; // program uses cin
    	using std::endl; // program uses endl
    
    // function main begins program execution
    int main()
    {
    	int x; // first input integer
    	int y; // second input integer
    	int z; // third input integer
    
    	cout << "Input three different integers: "; // prompt user for data
    	cin >> x >> y >> z; // read three integers from user
    
    	if ( x < y )
    		cout << "Smallest is " << x << endl;
    	if ( x < z )
    		cout << "Smallest is " << x << endl;
    	if ( y < x )
    		cout << "Smallest is " << y << endl;
    	if ( y < z )
    		cout << "Smallest is " << y << endl;
    	if ( z < x )
    		cout << "Smallest is " << z << endl;
    	if ( z < y )
    		cout << "Smallest is " << z << endl;
    
    	return 0; // indicate program executed successfully
    
    } // end function main
    With this, I got the result as below when I entered 13, 27 and 14 :
    Code:
    Smallest is 13
    Smallest is 13
    Smallest is 14
    Instead, the result suppose to be like this (forgive me for skipping the parts looking for the largest number cuz it didn't work at this moment):
    Code:
    Smallest is 13
    Largest is 27
    Geez, I'm stuck in this just at the beginning of my C++ programming learning, can anybody guide me and show me where I wrong. So far, I'd only know how to use the 'if' statement and those binary and relational operators including equality operators. Thanks

  2. #2
    ZuK
    ZuK is online now
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You have to use an additional variable, say smallest.
    After input of all 3 variables you assign it eg. the value of x.
    After that you need just 2 if statements like
    Code:
    if ( y < smallest ) smallest = y;
    After that you just print smallest.
    Kurt

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Code:
    int x, y, z;
    int smallest, largest;
    
    cout << "Input three different integers: "; // prompt user for data
    cin >> x >> y >> z; // read three integers from user
    
    smallest = x;
    largest = x;
    
    if(y < smallest)
    	smallest = y;
    
    if(z < smallest)
    	smallest = z;
    
    if(y > largest)
    	largest = y;
    
    if(z > largest)
    	largest = z;
    
    cout << "Smallest is: " << smallest << endl;
    cout << "Largest is: " << largest << endl;

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2
    Quote Originally Posted by ZuK View Post
    You have to use an additional variable, say smallest.
    After input of all 3 variables you assign it eg. the value of x.
    After that you need just 2 if statements like
    Code:
    if ( y < smallest ) smallest = y;
    After that you just print smallest.
    Kurt
    Quote Originally Posted by 39ster View Post
    Code:
    int x, y, z;
    int smallest, largest;
    cout << "Input three different integers: "; // prompt user for data
    cin >> x >> y >> z; // read three integers from user
    smallest = x;
    largest = x;
    if(y < smallest)
    	smallest = y;
    if(z < smallest)
    	smallest = z;
    if(y > largest)
    	largest = y;
    if(z > largest)
    	largest = z;
    cout << "Smallest is: " << smallest << endl;
    cout << "Largest is: " << largest << endl;
    Wow, amazing, I'm studying on my own and this is the last question I need to solve in chapter 2. However, the book didn't teach me much on the skill like yours. Thanks for your helps

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    Another way would be to store all three numbers in an array and periodically search for the highest and smallest numbers in the array. It requires less code.
    Does any of you know how to use arrays?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  6. #6
    ZuK
    ZuK is online now
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Elysia View Post
    Does any of you know how to use arrays?
    No I dont. Please tell me.
    Kurt

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,172
    Very funny. Wasn't referring to you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21