1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

C++ Sum & Average not working

Discussion in 'Software Development' started by aushaheed, Sep 6, 2001.

Thread Status:
Not open for further replies.
  1. aushaheed

    aushaheed Guest Thread Starter

    Joined:
    Sep 6, 2001
    Messages:
    85
    Hi:
    can anyone figure out why the sum and average isn't displaying based on what the user enters? it runs and i get values of 0 for each sum and average...any help by MIDNIGHT WOULD BE GREAT!

    Code:

    #include<iostream.h>

    int main()
    {
    int y, i;
    double g, m=0, a;
    cout << "Please enter number of values to be processed: ";
    cin >> y;//receiving number of values
    cout << "Please enter the " << y << " values:\n"; //asking for those values
    a = m/y;
    for(i = 1; i <= y; i++)
    {

    cin >> g;
    g += m;
    }

    cout << "Sum: " << m << "\n"; cout << "Average: " << a << "\n";
    cout << "Thank you come again!";
    return 0;
    }
     
  2. MacFromOK

    MacFromOK

    Joined:
    Mar 18, 2001
    Messages:
    1,741
    Here ya go.

    BTW, I suggest using var names that
    give you an idea of what you're doing,
    such as "total", "numentries", etc.

    int main()
    {
    int y, i;
    double g, m=0, a;
    cout << "Please enter number of values to be processed: ";
    cin >> y;//receiving number of values
    cout << "Please enter the " << y << " values:\n"; //asking for those values
    for(i = 1; i <= y; i++)
    {
    cin >> g;
    m += g; // Add entry (g) to total (m).
    }
    a = m/y; // divide m (total) by y (number of entries)
    cout << "Sum: " << m << "\n"; cout << "Average: " << a << "\n";
    cout << "Thank you come again!";
    return 0;
    }


    Hope I'm not doing somebody's homework here... :)
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/52752

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice