I need help with my program. It's not adding my numbers correctly.

Duo

Registered
/* C Program 4 - Corey Jones - cjtx6362@yahoo.com */

#include <iostream.h> // include system information

int main()
{
int numValues, count;
float sum, num, largest, smallest;

count = 1;
sum = numValues + num;
numValues = 1;

cout << "C Program 4 CSCE 1020.002" << endl
<< "Corey Jones cjtx6362@yahoo.com" << endl << endl;

cout << "Enter a number between 5 and 10: ";
cin >> num;
while (num < 5 || num > 10)
{
cout << "Number invalid, please try again: ";
cin >> num;
}
for(count=1; count<= numValues; count ++)
{
if (count == 1) largest = smallest = num;
else
{
if (smallest > num) smallest = num;
if (largest < num) largest = num;
}
cout << "The sum is " << sum << endl;
cout << "The average is " << sum / numValues << endl;
cout << "The largest is " << largest << endl;
cout << "The smallest is " << smallest << endl;

}

return 0;
}


Our assignment is to create a loop. The user enters a variety of numbers and the computer bugs them to enter it in the range. It also takes the numbers that he enters and display the sum and display the average of the numbers, the smallest and the largest. I can get it to display the largest and smallest. But I can't get it to display my sum or average. I get this wierd e number for both of them. Any ideas to help?
 
Mistakes:

01. Location of 'sum = numValues + num;'. Initially, 'numValues' will be very small, thus use '0', 'num' will be '1'; therefore, 'sum' will always be '1'.

02. 'numValues' in 'for(count=1; count<= numValues; count ++)' is set to 1; therefore, the 'for' routine will loop only once.

03. 'cout << "The sum is " << sum << endl;' could have been 'cout << "The sum is " << numValues + num << endl;' ... I think.

-----

Based on the information below provided code - you are not following the instructions. You perform a mathematical operation before valid values are provided (entered), you are not requesting values within a loop. Nor is it explained how one exits the loop - either via a fix number of requests [thus the use of a 'for' command] or a value entered ends the looping process [thus the use of a 'while' command].

-----

If I were writing the code, quick and dirty wise, it may look something like ...

Code:
#include <iostream.h>

int main (int argc, char * const argv[]) {
	float lowerLimit = 5.0, upperLimit = 10.0; // Set lower and upper limit values.
	float largest = lowerLimit, num, smallest = upperLimit, sum = 0.0; // Set largetest, smallest, and sum values.
	int numValues = 0; // Set 'numValues'.
	
	cout << "C Program 4 CSCE 1020.002" << endl 
	<< "Corey Jones cjtx6362@yahoo.com" << endl
	<< "... with some help from 'http://www.macosx.com'," << endl
	<< "(compiled with 'Xcode' v. 2.2.1.)." << endl << endl;

	while (1){ // Loop infinitely ...
		cout << "Enter a number between '5' and '10' (enter '0' to exit): " << endl;
		cin >> num;
		if (num == 0) break; // Break out of 'while ()' loop.
		
		while (num < lowerLimit || num > upperLimit){
			cout << "Number invalid, please try again: " << endl;
			cout << "Enter a number between '5' and '10' (enter '0' to exit): " << endl;
			cin >> num;
			if (num == 0) break; // Break out of inner 'while ()' loop.
		}
		if (num == 0) break; // Break out of outer 'while ()' loop.
		
		sum += num; // Perform 'sum' operation.
		
		if (smallest > num) smallest = num; // Perform 'smallest' operation.
		if (largest < num) largest = num; // Perform 'largest' operation.
		
		numValues++; // Increment 'numValues'.
	}
	if (sum == 0){ // If only a '0' (or '0.0') was entered - do not display any calculation results. 
		cout << endl << "User exited early." << endl; // Be nice, and notify the user of his action.
		return 0;
	}
	
	// Calculation results follow ...
	cout << "Sum: " << sum << endl; // Display 'sum' value.
	cout << "Number of values entered: " << numValues << endl; // Display 'numValues' value.
	cout << "Average [sum / number] of values entered: " << sum / numValues << endl; // Display '(sum / numValues)' value.
	cout << "Largest value entered: " << largest << endl; // Display 'largest' value.
	cout << "Smallest value entered: " << smallest << endl; // Display 'smallest' value.

	return 0;
}
 
Back
Top