what am i doing wrong?!?!?!?!

This is a discussion on what am i doing wrong?!?!?!?! within the C++ Programming forums, part of the General Programming Boards category; hi? apparently when i run the program it comes back with a error after i enter in 10 numbers. THe ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    14

    what am i doing wrong?!?!?!?!

    hi? apparently when i run the program it comes back with a error after i enter in 10 numbers. THe error states integer is divisible by 0? anyone help me out?

    #include<iostream>
    using namespace std;

    void scan_array(int[]);
    void print_scores(int);
    int average(int[]);
    int main()
    {
    int avg;
    int exams[10];
    scan_array(exams);
    avg=average(exams);
    print_scores(avg);
    }
    void scan_array(int grades[])
    {
    cout << "Enter 10 Exam Grades :";
    for(int i=0;i<=10; ++i)
    cin >> grades[i];


    }

    int average(int grades[])
    {
    int count=0;
    for(int i=0;i<=10;++i)
    count=count + grades[i]/ i;
    return count;
    }

    void print_scores(int average)
    {
    cout <<"exam average" << average;
    }
    Last edited by swarm; 11-28-2001 at 09:36 AM.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    154
    avg should be a float or double, not an int. On a quick look, check how you're calculating average. I don't think you should divide on every iteration through the loop; you need two statements here.
    For example, on the first loop, i = 0. You divide grades[i] by zero, which is illegal. That's a syntax error. Dividing every time even when i > 0 is a logic error. That will compile, but give the wrong answer.
    Last edited by Brown Drake; 11-28-2001 at 09:46 AM.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    int average(int grades[])
    {
    int count=0;
    for(int i=0;i< 10;i++)
    count += grades[i];
    return count / i;
    }


    void scan_array(int grades[])
    {
    cout << "Enter 10 Exam Grades :";
    for(int i=0;i<10; i++)
    cin >> grades[i];


    }
    Also try using float instead of int. You are losing decimal places.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 04:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 01:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 02:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21