Sign in to follow this  

Using a if/else function when a counter is used

This topic is 4816 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
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
But I was the Anonymous Poster. . .

You set max to the first number.

For the rest you set max to the currently known max and whatever the user entered.

So if the user entered 6 the first time:
max = 6.

5 the next time: 6 = larger(6, 5), so max is still 6.

7 the next time: 7 = larger(6, 7), so max is now 7.

9 the next time: 9 = larger(7, 9), so max is now 9.

2 the next time: 9 = larger(9, 2), so max is still 9.

And so on until we stop asking questions. In the end max is set to the largest number the user entered, and the larger function was invoked 9 times in order to figure out what that number was.

Share this post


Link to post
Share on other sites

This topic is 4816 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this