Thread: blight2c look this please (how to find avg)

  1. #1
    Unregistered
    Guest

    blight2c look this please (how to find avg)

    actually i dont know how i got it right the first time when i used your code, but now it's not working
    this is my code:

    #include<iostream.h>
    int main()

    {
    int clas;
    int grades;
    int number;
    int score;
    int avg;
    int time;
    int student;
    int total;
    cout<<"enter the # of students in the class";
    cin>>clas;
    cout<<"enter the # of grades for the class";
    cin>>grades;
    for (student=1; student<=clas; student++)
    {
    cout<<"enter test grades for student #";
    cin>>number;
    cout<<"enter -1 for absent"<<endl;
    for (time=1; time<=grades; time++)
    {
    cout<<"\t test #"<<time;
    cin>>score;
    total+=score; // here's the mistake, it doesnt total it correctly
    if (score==-1)
    {
    score=0;
    }

    }

    avg=total/(grades-1);
    avg=total/grades;


    cout<<"the avg of student #"<<number<<"is"<<avg<<endl;
    }

    return 0;
    }

    when it shows me the average it gives me a big number, the code seems right i dont know what's wrong with that little part, if you have any suggestions post them please... thx

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The total variable is never initialized to a value before it is used, so it contains garbage. Change:
    int total;
    to
    int total = 0;

    And it works perfectly, the processing at least.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Unregistered
    Guest
    you are right but now when the loop continues it's adding the avg total from the previous student to the next, it's confusing

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. How to find O of threads ?
    By jabka in forum C Programming
    Replies: 3
    Last Post: 03-11-2008, 12:25 PM
  3. ld.exe: cannot find -l-lstdc++
    By Tonto in forum Tech Board
    Replies: 3
    Last Post: 04-10-2007, 11:20 PM
  4. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM
  5. Q: Recursion to find all paths of a maze
    By reti in forum C Programming
    Replies: 7
    Last Post: 11-26-2002, 09:28 AM