Thread: averaging program

  1. #1
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    averaging program

    ok, i want a program to average, but it wont work. tell me whats wrong.

    void avg (void)
    {
    cout<<"This program will get the average of numbers you enter."<<endl;
    float g, x, y, num, avg, num1;
    cout<<"How many numbers do you want to enter? ";
    cin>>num1;
    num=num1;
    do
    {
    cout<<"Please enter your numbers: ";
    cin>>x;
    num=num-1;
    avg=avg+x;
    }while(num>0);
    g=avg/num1;
    cout<<"The average of your numbers is: "<<g<<flush<<endl;
    _getch();
    }

  2. #2
    zoo
    Guest
    You might need to initialize avg to zero at the beginning of your function.

    Code:
    void avg (void)
    {
    cout<<"This program will get the average of numbers you enter."<<endl; 
    float g, x, num, num1;
    float avg = 0;
    cout<<"How many numbers do you want to enter? "; 
    cin>>num1; 
    num=num1; 
    do 
    { 
       cout<<"Please enter your numbers: "; 
       cin>>x; 
       num--;
       avg+=x;
    }while(num>0); 
    g=avg/num1; 
    cout<<"The average of your numbers is: "<<g<<flush<<endl; 
    _getch(); 
    }

  3. #3
    Unregistered Leeman_s's Avatar
    Join Date
    Oct 2001
    Posts
    753

    lol

    why didnt i think of that before!? im so stupid sometimes

  4. #4
    zoo
    Guest
    I actually ran it without the initialization on my computer, and it gave the correct results, but probably just lucky.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM