Click to See Complete Forum and Search --> : Can you fix this code???


Ercan
March 30th, 1999, 03:38 AM
I have to write a code that asks the user

How many numbers would you like to enter?

And the program is supposed to give the smallest, largest , average

and the difference between each number and the average.

I have most of it here thanks to some help from ( M.D ). But i have

trouble with the difference between each number and the average.

Everything else works fine.

Could you please go through the code and show me what is wrong and how to fix

it. I am a beginner so please use the most basic of code possible.

I Thank You in advance.

Ercan

#include <iostream.h>

#include <conio.h>

main()

{

clrscr();

int a,b,f,min,max,sum=0;

float tavg,davg,avg;

cout<<"How many numbers would you like to enter?\t";

cin>>a;

for (int count = 0; count < a; count++)

{

cout<<"\n\nEnter a number please:\t";

cin>>b;

sum += b;

f+=b;

{

if (count>1)

avg=(f/2);

cout<<"\nAverage of these Numbers are\t"<<avg;

davg=(f-avg);

cout<<"\nDifference between these Numbers and there Average is\t"<<davg;

}

if (count == 0)

{

min = max = b;

}

else

{

if (b < min)

min = b;

if (b > max)

max = b;

}

}

tavg = sum / a;

cout<<"\nSmallest number is\t"<<min;

cout<<"\nLargest number is\t"<<max;


cout<<"\nTotal Average is\t\t"<<tavg;

getch();

return 0;

}

Franky Braem
March 30th, 1999, 04:09 AM
You have to assign avg to f after calculating avg.

Dave Lorde
March 30th, 1999, 05:08 AM
The average should be the sum divided by the number of items. Since count starts at zero, the average is:

average = sum / (count + 1);

It also looks as if you want the difference between the average and the current number entered, not the difference

between the sum and the average.

When coding, try to use meaningful names, and use plenty of spaces to make it readable. In C++, it is good practice to

wait to declare variables until they are needed, and initialize them immediately. For clarity, each variable should be

declared on a separate line.

Here is my version of your program:

#include <iostream.h>

#include <conio.h>

#include <limits.h> // contains INT_MAX and INT_MIN

int main() // main has an int return value

{

// clrscr(); // VC++ doesn't have this

cout << "How many numbers would you like to enter?\t";

int maxEntries = 0;

cin >> maxEntries;

if (maxEntries <= 0) // Bail out if bad entry!

return 0;

int sum = 0;

int minNumber = INT_MAX; // Start at largest

int maxNumber = INT_MIN; // start at smallest

for (int count = 0; count < maxEntries; count++)

{

cout << "\n\nEnter a number please:\t";

int number = 0;

cin >> number;

sum += number;

// Ensure floating point arithmetic by casting one operand to float

float average = (float)sum / (count + 1);

cout << "\nAverage of these numbers is " << average;

float difference = number - average;

cout << "\nDifference between this number and the average is " << difference;

if (number < minNumber)

minNumber = number;

if (number > maxNumber)

maxNumber = number;

}

cout << "\nSmallest number is " << minNumber;

cout << "\nLargest number is " << maxNumber;

// Ensure floating point arithmetic by casting one operand to float

float totalAverage = (float)sum / maxEntries;

cout << "\nTotal Average is " << totalAverage;

cout.flush(); // VC++ needs this to ensure output before getch()

getch();

return 0;

}


hope this helps,


Dave