how do i get the largest, smallest, first and last values??

This is a discussion on how do i get the largest, smallest, first and last values?? within the C++ Programming forums, part of the General Programming Boards category; how do i write a code to get the largest, smallest, first and last entries from a series of number ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    31

    Question how do i get the largest, smallest, first and last values??

    how do i write a code to get the largest, smallest, first and last entries from a series of number that the user inputs ( the series of number is large- about 50 numbers)

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    As you read the numbers in, keep a record of the highest/lowest values. The first and last are pretty easy to spot, the first one will be the one the user enters first, and the last one will be...

    No need to store all the numbers unless you want to. Just create a few variables like these:

    int first, last, highest, lowest.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    31


    well i guess you're basically re- stating my question...how do i code largest, smallest, first and last entries??
    im really a new programmer!

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Post some code, so we can see how far you've got, and then we'll be in a better position to help you.

    For example, can you get a number from the user ?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    well heres the qustion:
    write a program tht accepts series of numbers form the user, stopping at the first zero or negative number (n <= 0) entered. Echo-print the numbers as they are entered (or print them if you re-direct input from a file). Do not processs the non-positve number entered to stop the sequence at all. After the non-postive number entry, print the count of entries, the sum of
    entries, the largest entry, the smallest entry, the first entry, the last entry, and the (floating-point) average of the entries.

    well the thing is i don't know how to code the sum of
    entries, the largest entry, the smallest entry, the first entry, and the last entry

    the user has to enter about 50 integers...

    can u help me now?

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    #include <iostream>
    
    int main ( void )
    {
      int i,
          highest,
          lowest,
          first,
          last;
      
      if (cin>>first)
      {
        highest = lowest = last = first;
        while (cin>>i)
        {
          // More code needed here.
          // Simply test the current entry with the highest.  If it is bigger,
          // make the highest entry the same value as the current entry.
          // Then do the same kind of thing for the lowest entry.
        }
      }
    }
    There's a starter for you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    Thank You!!

    well i have another quetion
    how do i make the user enter the integer soo many times??

  8. #8
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    a for loop. Say u want 5 values:

    Code:
    for (int i = 1; i <= 5; i++)
    {
    cout << ....
    cin >> ...
    }

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Nikisha
    how do i make the user enter the integer soo many times??
    --> while (cin>>i)
    This loops until a bad read occurs, so if the user keeps entering, the prog will keep running. There are many ways to control a loop though. You could also add a second condition to this while statement, like loop while i is greater than 0.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    well here's my program so far:

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    int main()
    {
    	int n, sum, num, last;
    	
    	cout << "Enter count of entries: ";
    	cin  >> n;
    	
    	sum = 0;
    	for (int i = 0; i <= n; i++)
    	{
    		cout << "enter an integer: ";
    		cin >> num;	
    
    		sum = sum + num;
    	}
    	
    	cout << "Sum of integers = " << sum << endl;
    
    	last = num;
    
    	cout << "The last number is " << last << ' ';
    
    	
    
    	
    	return 0;
    }
    so how do i fit in the highest, lowest and first values into this?

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    Originally posted by Nikisha
    well here's my program so far:

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    int main()
    {
    	int n, sum, num, last;
    	
    	cout << "Enter count of entries: ";
    	cin  >> n;
    	
    	sum = 0;
    	for (int i = 0; i <= n; i++)
    	{
    		cout << "enter an integer: ";
    		cin >> num;	
    
    		sum = sum + num;
    	}
    	
    	cout << "Sum of integers = " << sum << endl;
    
    	last = num;
    
    	cout << "The last number is " << last << ' ';
    
    	
    
    	
    	return 0;
    }
    so how do i fit in the highest, lowest and first values into this?
    Well, getting the first number is easy. Look at where the user enters the first number. Right after that, you'll want to add the code
    Code:
    first = num.
    But here's the problem: because of the loop, first is always getting num, every time the user enters the next number.

    So how do we get around this? Well, you could do one of two things. You can either add a if statement in the loop, and checking that
    Code:
    if (i == 0)
    {
    //then first gets num
    }
    OR you could simply place one line of cin>>num before the loop, but then you have to make some change to the loop accordingly.

    What about highest/lowest you ask? Well, what you need to do

    The first time the user enters a number, your going to treat that number like it's the highest and the lowest (cause you don't know yet), so your going to set your two veriables, highest and lowest, to that first number. (if (i == 0)) Then when the user enters the next number, you need to re-set highest and lowest to the second number ONLY IF num > highest and the same goes with lowest. Get it?
    Last edited by funkydude9; 03-23-2003 at 02:21 PM.

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    thnx ..it works!!

    but then wht bout the highest n lowest values??

  13. #13
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,230
    Pretty simply...

    For high:
    First read in the very first value and place it in the variable high. Now, as you read in the user's input, constantly test if the value currently in high is larger than the value being inputted.

    Code:
    if(high < num)
         high = num;
    So your loop might look something like:

    Code:
    for (int i = 0; i <= n; i++)
    {
        cout << "enter an integer: ";
        cin >> num;
        sum = sum + num;
    
        if(high < num)
            high = num;
    
        if (i == 0)
            high = first = num;
    }
    use the same idea for lowest values

  14. #14
    Registered User
    Join Date
    Mar 2002
    Posts
    249
    I edited my post.

  15. #15
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    well i seen to get the hang of the highest, first and last values but i still cant figure out the lowest value
    plus i just realized tht the program should calculate the numer of counts...i made a mistake by asking the user to enter the counts.
    can neone help me figure this out?

    heres my program so far:
    Code:
    // This program prompts the user for a series of numbers and then displays the 
    // count of entries, sum of entries, largest entry, smallest entry, first entry and
    // the last entry
    
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    	int n, sum, num, last, count, first, high, low;
    	float avg;
    	
    	cout << "Enter number of entries: ";
    	cin  >> n;
    
    	sum = 0;
    	for (int i = 0; i < n; i++)
    	{
    		cout << "enter an integer: ";
    		cin >> num;
    
    		if (i == 0)
    		{
    			first = num;
    		}
    
    		sum = sum + num;
    
    		if (high < num)
    		{
    			high = num;
    		}
    
    		if (num < low)
    		{
    			low = num;
    		}
    
    	}
    
    	cout << endl;
    
    	last = num;	
    	count = n;
    	avg = sum / count;
    
    	cout << "The first number is " << first << endl;
    	cout << endl;
    	cout << "The last number is " << last << endl;
    	cout << endl;
    	cout << "The highest number is " << high << endl;
    	cout << endl;
    	cout << "The lowest number is " << low << endl;
    	cout << endl;
    	cout << "Your count of entries was " << count << endl;
    	cout << endl;
    	cout << "Sum of integers = " << sum << endl;
    	cout << endl;
    	cout << "Average = " << avg << endl;
    	cout << endl;
    
    	return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

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