Saving the largest and smallest numbers.

This is a discussion on Saving the largest and smallest numbers. within the C++ Programming forums, part of the General Programming Boards category; I have a program where the user has to input numbers over and over, if they want to end the ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    33

    Saving the largest and smallest numbers.

    I have a program where the user has to input numbers over and over, if they want to end the program they hit 0. After they hit zero though I have to display the largest and smallest numbers they entered. Tips on how I can do this?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I find it hard to believe you can't come up with a way to do this without someone giving you an algorithm. How about you try to come up with a way to do this first? If it seems reasonable, and it makes sense, then try coding it.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    What I had in mind was using if, setting the first number entered to a variable called temporary, and have all the numbers entered compared to that, if the number entered bigger/smaller have that number become temporary. Do you think that will work?

    Yea I'm confused. This is my code.

    Code:
    #include <iostream.h>
    
    int main()
    {
    	int sum = 0;
    	int num;
    	int total = 0;
    	int temp = 0;
    	int largest;
    
    
    	do
    	{
    		cout << "Enter a number (Enter 0 to quit): ";
    		cin >> num;
    	
    		for (temp = num)
    		{
    			if (num > temp)
    			largest = temp;
    		}
    	
    		total = total + num;
    	
    		if (num == 0)
    		
    		{
    		cout << largest;
    		cout << total << " ";
    		break;
    		}
    		 
    	}
    	while (1);
    	return 0;
    }
    Getting two errors

    --------------------Configuration: 6.1 - Win32 Debug--------------------
    Compiling...
    6.1.cpp
    D:\P2\C++\6.1.cpp(27) : error C2143: syntax error : missing ';' before ')'
    D:\P2\C++\6.1.cpp(27) : error C2143: syntax error : missing ';' before ')'
    Error executing cl.exe.

    6.1.obj - 2 error(s), 0 warning(s)
    Last edited by Sembhi; 12-06-2007 at 06:49 PM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    Quote Originally Posted by Sembhi View Post
    What I had in mind was using if, setting the first number entered to a variable called temporary, and have all the numbers entered compared to that, if the number entered bigger/smaller have that number become temporary. Do you think that will work?
    Well if it's going to output BOTH the smallest and biggest, you'll probably need 2 temp variables. And yeah that method could work.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    Yea I know, I'm just trying to get one to work first.

    Can someone take a look at the code posted above and tell me whats wrong with it?

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    20
    for (temp = num)
    {
    if (num > temp)
    largest = temp;
    }
    wrong syntax for a for statement.
    ex:

    Code:
    for(int a=0;a<=num;a++)
    {
    }

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    ^ Thanks.

    Code:
    #include <iostream.h>
    
    int main()
    {
    	int sum = 0;
    	int num;
    	int total = 0;
    	int temp = 0;
    	int largest = 0;
    
    
    	do
    	{
    		cout << "Enter a number (Enter 0 to quit): ";
    		cin >> num;
    	
    		for(int temp=0;temp<=num;temp++)
    	
    		{
    		if (num > temp)
    		largest = temp;
    		}
    	
    		total = total + num;
    	
    		if (num == 0)
    		
    		{
    		cout << largest;	
    		cout << total << " ";
    		break;
    		}
    		 
    	}
    	while (1);
    	return 0;
    }
    Why is largest not displaying?

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    Most probably because num does not equal zero ?

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    33
    Then why does the total display?

  10. #10
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    Why are you using temp as you iterator and also compare it to num?
    Last edited by clegs; 12-06-2007 at 09:16 PM.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,173
    clegs is correct. Your for loop is quite flawed and not necessary.
    Let me ask you: what is your code supposed to do? What is the "algorithm" you have in mind to do this? It's a very simple project, but your code is all wrong.
    Can you clarify how you're thinking you can do this? If you don't have an idea on the flow of your application, you need to think of one. As I like to say, re-think your design! That is, if your current design you have in mind is not correct.
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Yeah, the inner loop is completely unnecessary.

    Can you try to describe, in words, what you're trying to do? Assume you have a variable named 'largest' that holds the largest number previously found, and assume you have a variable called 'num' which holds the most recent number. How do you decide whether to update 'largest' or not?
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 05-29-2009, 05:44 PM
  2. largest and smallest
    By eldemonio in forum C Programming
    Replies: 9
    Last Post: 10-15-2007, 02:00 PM
  3. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 03:25 PM
  4. Largest / Smallest (5 integers)
    By Ripley in forum C Programming
    Replies: 4
    Last Post: 10-09-2005, 08:58 PM
  5. Replies: 16
    Last Post: 09-21-2004, 11:08 PM

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