PDA

View Full Version : math question



bornwithnoname
02-09-2008, 02:25 AM
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.

4play
02-09-2008, 08:41 AM
all depends what language really but it should look something like this



while list is not at end
{
total = total + number
total_numbers = total_numbers + 1
}
mean = total / total_numbers

that should give you an idea of what you should be doing.

bornwithnoname
02-10-2008, 12:04 AM
thanks that will help

nsap
02-10-2008, 04:58 PM
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.

UnlnvlslblE
02-10-2008, 06:51 PM
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.


If it's zero entries the loop would never start :lol:

4play
02-10-2008, 07:23 PM
indeed the loop would never start and it would jump to the line


mean = total / total_numbers

which would then divide by zero causing death, flooding and lots of other horrible things to happen.

UnlnvlslblE
02-11-2008, 03:30 PM
indeed the loop would never start and it would jump to the line


mean = total / total_numbers
which would then divide by zero causing death, flooding and lots of other horrible things to happen.

Yes you are correct. I figured it wouldn't throw an exception since it's 0/0 but it still doesn't like the fact that you are dividing by zero.

Ph4wX
02-18-2008, 12:35 AM
http://users.wpi.edu/~bbrom/images/I_divided_by_zero.jpg

but let's be serious here

here's a simple c++ application that does it

http://pastebin.com/m109fe83c


//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;
}

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.

If you need more help, pm me

iamKy666
02-24-2008, 12:14 PM
Use Matlab dude.