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

  1. #16
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    ok so i worked the count thing out..but i still need help with the finding the lowest value
    heres my program:

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
    	int n, sum, num, last, count, first, high, low;
    	float avg;
    	
    	sum = 0;
    	n = 1;
    	for (int i = 0; i < n; i++)
    	{
    		cout << "Enter an integer: ";
    		cin >> num;
    		++n;
    
    		if (num <= 0)
    		{
    			break;
    		}
    
    		sum = sum + num;
    
    		if (i == 0)
    		{
    			first = num;
    		}
    	
    		if (high < num)
    		{
    			high = num;
    		}
    
    		if (low > num)
    		{
    			low = num;
    		}
    	}
    	
    	cout << endl;
    
    	last = num;	
    	count = n - 1;
    	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 << showpoint << fixed;
    	cout << "Average = " << setprecision(1) << avg << endl;
    	cout << endl;
    
    	return 0;
    }

  2. #17
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When you get the first int, do something like this (as I suggested earlier):

    >highest = lowest = last = first = num;

    It set all the variables to the value of num. You can of course do that across multiple lines of code, with individual assignments, if you want.


    Why are you incrementing n inside the loop? The i variable will always be less than n if you increment them together.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #18
    Registered User
    Join Date
    Mar 2003
    Posts
    31
    thnx soooo much!!!

    i tried it n now my program works perfectly!!!!

Popular pages Recent additions subscribe to a feed