I am working on a program. the program needs to determine mean of a group of numbers. I know how to find mean but I do not express mathematically.
Printable View
I am working on a program. the program needs to determine mean of a group of numbers. I know how to find mean but I do not express mathematically.
all depends what language really but it should look something like this
that should give you an idea of what you should be doing.Code:while list is not at end
{
total = total + number
total_numbers = total_numbers + 1
}
mean = total / total_numbers
thanks that will help
Don't forget to check to make sure the list is not "0" entries long, otherwise your program will try to divide by zero and most likely crash.
indeed the loop would never start and it would jump to the line
which would then divide by zero causing death, flooding and lots of other horrible things to happen.Code:mean = total / total_numbers
http://users.wpi.edu/~bbrom/images/I...ed_by_zero.jpg
but let's be serious here
here's a simple c++ application that does it
http://pastebin.com/m109fe83c
i just made it real fast, what it does is that it asks for how many number you want in your average, and it loops the adding, at the end it divides by the ammount of numbers there was.Code://This application will calculate the average of X numbers
//Header Files
#include <iostream> // So that we can input and output text
#include <iomanip> // So that we can manipulate the outputted text
#include <stdlib.h> // So that we can use cls to clear the screen
using namespace std;// So that we don't have to type std:: in front of commands such as cin and cout
void title()
{
cout << ":::::'###::::'##::::'##:'########:'########:::::'###:::::'######:::'########:::" << endl;
cout << "::::'## ##::: ##:::: ##: ##.....:: ##.... ##:::'## ##:::'##... ##:: ##.....::::" << endl;
cout << ":::'##:. ##:: ##:::: ##: ##::::::: ##:::: ##::'##:. ##:: ##:::..::: ##:::::::::" << endl;
cout << "::'##:::. ##: ##:::: ##: ######::: ########::'##:::. ##: ##::'####: ######:::::" << endl;
cout << ":: #########:. ##:: ##:: ##...:::: ##:. ##::: #########: ##::. ##:: ##...::::::" << endl;
cout << ":: ##.... ##::. ## ##::: ##::::::: ##::. ##:: ##.... ##: ##::: ##:: ##:::::::::" << endl;
cout << ":: ##:::: ##:::. ###:::: ########: ##:::. ##: ##:::: ##:. ######::: ########:::" << endl;
cout << "::..:::::..:::::...:::::........::..:::::..::..:::::..:::......::::........::::" << endl;
cout << endl;
cout << endl;
}
int main()
{
int mark;
double average;
int total = 0;
int count = 0;
int max;
char yn;
do
{
system("cls");
title();
cout << "How many numbers do you want in your average? ";
cin >> max;
cout << endl;
if (max < 1 || max > 100)
{
cout << "Your answer was either smaller than 1 or bigger than 100, program will now terminate" << endl;
return 0;
}
cout << "Please, enter " << max << " marks, and this application will calculate the average." << endl;
//Asks for a mark "max" times
for (count = 1; count <= max; count++)
{
cout << "Enter mark #" << count << " : ";
cin >> mark;
if (mark < 0 || mark > 100)
{
cout << "Invalid mark, please enter a mark between 0 and 100." << endl;
--count;
}
else
total = total + mark;
}
//Calculates the average
average = total / max;
//Displays the average of the 10 marks
cout << endl;
cout << endl;
cout << showpoint << setprecision(2) << fixed;
cout << "The average of the 10 marks is of " << average << " ." << endl;
cout << "Would you like to calculate another average? (y/n): ";
cin >> yn;
}
while (yn != 'n');
return 0;
}
If you need more help, pm me
Use Matlab dude.