Your Ad Here Your Ad Here
Results 1 to 9 of 9

Thread: math question

  1. #1
    bornwithnoname's Avatar Bit Master BT Rep: +1
    Join Date
    Sep 2006
    Location
    USA
    Posts
    368
    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.

    I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it. (Terry Prattchet)

  2. Internet, Programming and Graphics   -   #2
    4play's Avatar knob jockey
    Join Date
    Jan 2003
    Location
    London
    Age
    35
    Posts
    5,530
    all depends what language really but it should look something like this

    Code:
    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.

  3. Internet, Programming and Graphics   -   #3
    bornwithnoname's Avatar Bit Master BT Rep: +1
    Join Date
    Sep 2006
    Location
    USA
    Posts
    368
    thanks that will help

    I'll be more enthusiastic about encouraging thinking outside the box when there's evidence of any thinking going on inside it. (Terry Prattchet)

  4. Internet, Programming and Graphics   -   #4
    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.

  5. Internet, Programming and Graphics   -   #5
    Quote Originally Posted by nsap View Post
    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

  6. Internet, Programming and Graphics   -   #6
    4play's Avatar knob jockey
    Join Date
    Jan 2003
    Location
    London
    Age
    35
    Posts
    5,530
    indeed the loop would never start and it would jump to the line
    Code:
    mean = total / total_numbers
    which would then divide by zero causing death, flooding and lots of other horrible things to happen.

  7. Internet, Programming and Graphics   -   #7
    Quote Originally Posted by 4play View Post
    indeed the loop would never start and it would jump to the line
    Code:
    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.

  8. Internet, Programming and Graphics   -   #8
    Ph4wX's Avatar Mudkip BT Rep: +8BT Rep +8
    Join Date
    Sep 2007
    Posts
    186


    but let's be serious here

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

    http://pastebin.com/m109fe83c

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

  9. Internet, Programming and Graphics   -   #9
    BANNED BT Rep: +9BT Rep +9
    Join Date
    Jan 2008
    Posts
    301
    Use Matlab dude.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •