C++ - Determining the largest number out of 15 numbers entered

This is a discussion on C++ - Determining the largest number out of 15 numbers entered within the C++ Programming forums, part of the General Programming Boards category; Good Afternoon, This is my first attempt at writing a C++ program. I recently learned programming in C and it ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    C++ - Determining the largest number out of 15 numbers entered

    Good Afternoon,
    This is my first attempt at writing a C++ program. I recently learned programming in C and it did not seem to be a problem for me. I am feeling pretty stupid with this program. Here is the question:

    Create a program to determine the largest number out of 15 numbers entered (numbers entered one at a time). This should be done in a function using this prototype:

    double larger (double x, double y);


    Make sure you use a for loop expression inside your function.

    Enter 15 numbers
    11 67 99 2 2 6 8 9 25 67 7 55 27 1 1
    The largest number is 99


    Hints:

    * Read the first number of the data set. Because this is the only number read to this point, you may assume that it is the largest number so far and call it max.
    * Read the second number and call it num. Now compare max and num, and store the larger number into max. Now max contains the larger of the first two numbers.
    * Read the third number. Compare it with max and store the larger number into max.
    * At this point, max contains the largest of the first three numbers. Read the next number, compare it with max, and store the larger into max.


    Repeat this process for each remaining number in the data set using a for expression.


    Here is my code:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int main()
    
    {
    int max;
    int num;
    
    for (int counter = 0; counter<15; counter++)
    
    {
    cout <<"Enter a number: ";
    cin >> max;
    cout << "Enter another number: ";
    cin >> num;
    if (num > max)
    num = max;
    
    cout << "The largest number is " <<max;
    }
    return 0;
    }
    As you can see, I have the largest number line printing out after every user input number. I know it's probably something wrong with my for loop or my braces, but I can not figure it out for the life of me. Any input will be appreciated. Thank you.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Read these lines again:
    Code:
    if (num > max)
    num = max;
    The bug is in there, see if you can find it.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think there are other problems besides that, such as having user enter the max value 15 times. You'd rather need to keep a running max.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User QuestionKing's Avatar
    Join Date
    Jan 2009
    Posts
    68
    Create a program ... This should be done in a function using this prototype: ...
    You do not yet have a function.
    Make sure you use a for loop expression inside your function.
    Looks like you did well on the loop so far...
    * Read the first number ... this is the only number read to this point, ... it is the largest number so far and call it max.
    This hint is more of a reminder as to what is happening, rather than an instruction to follow. If you think about it, what you do inside your loop for every step, is capable of handling your first cycle through the loop. (you don't need to assign to max inside your loop on the first cycle, and then cin num, that actually causes your loop to cin 30 numbers in 15 cycles) Your cout is also going to repeat, as you noted, because it is within the body of the loop. You want to find the largest number before outputting, so the loop must complete all cycles before the cout can be reached.
    * Read the second number and call it num. Now compare max and num, and store the larger number into max. Now max contains the larger of the first two numbers.
    Looks like what you did for this part is the right idea, but has a problem. Consider the rules of the assignment operator.

    using std::endl; .... you never used endl at all
    ... your code has room for a few comments yet
    ... and there are other minor *"things" some may disagree with such as int main()
    * as not to refer to them as bugs or errors so to speak
    Asking a question you already know the answer to might teach you something you did not know...

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    27
    just screwing around and I didn't follow your constraints. but this is the general idea of how it works.. of course you need to use a function taking 2 doubles and return a double (the max). You just missed using a temporary value..

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    	{
    	int max = 0, tempmax = 0, i =0;
    	int	num, num1;
    	
    	
    
    while (i<7)
    	{
    		cout << "enter two numbers" << endl;
    		cin >> num >> num1;
    		
    
    		if (num > num1)
    			tempmax = num;
    		else 
    			tempmax = num1;
    	
    		if (tempmax > max)
    			max = tempmax;
    	 
    	i++;
    	}
    	
    	cout << "The largest number is " << max << endl;
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. smallest largest number
    By manzoor in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2008, 08:56 AM
  2. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 09:28 AM
  3. Reading in an unknown number of numbers
    By cboard_member in forum C++ Programming
    Replies: 3
    Last Post: 02-01-2006, 04:21 AM
  4. Replies: 3
    Last Post: 03-29-2005, 04:24 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-26-2001, 12:45 AM

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