Jump to content
  • Advertisement
Sign in to follow this  
Clover_Maximus

Using a if/else function when a counter is used

This topic is 5032 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm given the following program; #include <iostream> using std::cout; using std::endl; using std::cin; int larger( int x, int y); int main() { int num; int max; int count; cout << "Enter ten numbers. " << endl; cin >> num; max = num; for ( count = 1; count < 10; count++) { cin >> num; max = larger(max, num); } cout << "The largest number is " << max << endl; return 0; } int larger( I am supposed to write a function that will find the largest of 10 inputs using if/else statements. This i know how to do using 10 intialized variables. What confuses me is that only 2 variables are intialized (int x and int y) What am I missing? FYI I am in chapter 3 of "C++ How to Program" published by Deitel I'm only a first quarter student in this. Not much programming exp. Simple, detailed explanation would be very helpful thank you

Share this post


Link to post
Share on other sites
Advertisement
Baby steps. You want to write out an algorithm, something like the following:

  1. set the largest number to be the first of 10

  2. for each of the remaining 9, compare to the current largest
    • if the number is larger than the current largest, set the largest to it instead

    Share this post


    Link to post
    Share on other sites
    Cool beans.Thanks for the help Oluseyi.

    If I may, let me start off with what you have,


    1. set the largest number to be the first of 10
    2. for each of the remaining 9, compare to the current largest
    -if the number is larger than the current largest, set the largest to it instead

    -else keep current as largest

    How would I write this? I understand what you write, but i dont see how "num" (from above program) can be used for 10 differant numbers.

    Share this post


    Link to post
    Share on other sites
    Use an array / vector to store the input. I will not write the function for you, but here is how you do it using std::max_element():

    #include <vector>
    #include <algorithm>

    using namespace std;


    int main()
    {
    vector <int> array;
    int temp;
    for (int i=0; i<10;++i)
    {
    cout << "enter number "<< i+1 << " of 10: ";
    cin >> temp;
    array.push_back(temp);
    }
    cout << "The largest element is: "<<*(max_element(array.begin(), array.end())) << endl;
    cin.ignore(2);
    }

    Share this post


    Link to post
    Share on other sites
    Guest Anonymous Poster
    You want to return the larger of the two variables. It isn't that hard:


    int larger(int x, int y)
    {
    if(x > y) // If x is larger than y
    return x; // return x
    else // otherwise
    return y; // return y instead.
    }



    So, let us step through the program yo make sure you understand this (since just giving you the answer to a homework problem isn't going to help you).

    The user enters the first number, lets say it's six. We set max to 6.

    Then we enter the loop to get the rest of the numbers.

    The user enters 5. We set max to the larger of the currently known maximum, and 5. It returns 6, so max remains as is.

    Next, the user enters 7. Again, we set max it the larger of the currently known maximum, and 7. This time it returns 7, so max becomes 7.

    This continues for all the numbers, and in the end max is the largest number the user entered. We then display the largest number and exit. All is well in the world.

    Share this post


    Link to post
    Share on other sites
    Thanks for help Invective, but we arent allowed to use arrays yet.

    I'm not asking for anyone to write out the function or even the pogram. All Im really asking is how 1 declared variable can be used to read 10. (see my first post)

    maybe that is aksing for someone to write it... BAAH!

    Share this post


    Link to post
    Share on other sites
    Oh, in that case, the function is called 9 times with the different numbers. It returns the maximum, and that result is passed back to it the next time (as x), with the new number from the user (as y).

    Share this post


    Link to post
    Share on other sites
    Sign in to follow this  

    • Advertisement
    ×

    Important Information

    By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

    We are the game development community.

    Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

    Sign me up!