Thread: Please help with hw...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    5

    Please help with hw...

    Hi, I have to write code for a program that finds the maximum and minimum out of the three integers entered my the user. All three numbers need to be different but if two or more are the same I need to execute the loop again. This is my 1st problem it doesn't execute the loop again if I enter similar numbers. My 2nd problem is that the program outputs the max value for both minimum and maximum. Please help. Thanks a lot!

    Code:
    #include <iostream>using namespace std;
    
    
    
    
    bool is_different(int a, int b, int c);
    int max_of_three(int a, int b, int c);
    int greater_of_two(int a, int b, int c);
    int min_of_three(int a, int b, int c);
    int smaller_of_two(int a, int b);
    void print_result(int max, int min);
    
    
    
    
    
    
    int main()
    {
        int a;
        int b;
        int c;
        int max;
        int min;
    
    
    do
        {
            cout << "Enter the first integer: "<< endl;
            cin >> a;
    
    
            cout << "Enter the second integer: " << endl;
            cin >> b;
    
    
            cout << "Enter the third integer: "<< endl;
            cin >> c;
    
    
            max = max_of_three (a, b, c);
            min = min_of_three (a, b, c);
            print_result (max,min);
        }
    
    
    while (!(is_different ( a, b, c)));
    }
    bool is_different (int a, int b, int c)
    {
    if (a == b || b ==c || b ==c)
    {
        return false;
    }
    
    
    else
    {
        return true;
    }
    }
    
    
    int greater_of_two (int a, int b)
    {
        if (a > b)
        {
        return a;
        }
    
    
        else
        {
        return b;
        }
    
    
    }
    int max_of_three (int a, int b, int c)
    {
        int max1 = greater_of_two (a, b);
        int max2 = greater_of_two (max1, c);
        return max2;
    }
    
    
    
    
    int smaller_of_two (int a, int b)
    {
        if (a > b)
        {
        return a;
        }
    
    
        else
        {
        return b;
        }
    }
    
    
    int min_of_three (int a, int b, int c)
    {
        int min1 = smaller_of_two (a, b);
        int min2 = smaller_of_two (min1, c);
        return min2;
    }
    
    
    
    
    void print_result (int max , int min)
    {
    cout << "The maximum integer is " << max << endl;
    cout << "The minimum integer is " << min << endl;
    }

    This is the output for two similar numbers:

    Enter the first integer:
    5
    Enter the second integer:
    6
    Enter the third integer:
    5
    The maximum integer is 6
    The minimum integer is 6



    This is the output for 3 different numbers:

    Enter the first integer:
    1
    Enter the second integer:
    2
    Enter the third integer:
    3
    The maximum integer is 3
    The minimum integer is 3

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Did you copy and paste much?
    Code:
    int greater_of_two (int a, int b)
    {
        if (a > b)
    ...
    int smaller_of_two (int a, int b)
    {
        if (a > b)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    1
    This is why its not repeating properly

    Code:
    if (a == b || b ==c || b ==c)
    {    return false; }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to indent properly. Until you do that, nothing else matters.
    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.

Popular pages Recent additions subscribe to a feed