Using a if/else function when a counter is used

Started by
11 comments, last by Clover_Maximus 19 years, 6 months ago
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
Advertisement
Free cookie to whoever can help...
;)
Baby steps. You want to write out an algorithm, something like the following:
set the largest number to be the first of 10for each of the remaining 9, compare to the current largestif the number is larger than the current largest, set the largest to it instead
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.

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);}
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.
Stupid lack of using my normal computer.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
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!
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).
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Thank you Anonymous Poster. Didnt realize it was that simple.

Smart_idiot....what does that even mean?

This topic is closed to new replies.

Advertisement