Minor Help w/ Debug

Hello guys,

I'm writing a program for my beginner c++ class, and need a little help (big surprise, I know).

Now, I'm only 2 functions in of the 6 assigned to write for the main program, but already running into problems...

specifically...lines 29, 30; second function, "getAverage" does not return a number...at all. Debug says "Function definition not allowed here" it is referring to the "{" right under the "void getAverage ()"

If anyone could help, or point me in the right direction, it will be GREATLY appreciated.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

//Global Variables...4 NBA Players with Points Per Game for the previous 4 games...

string names [4]={"Lebron James", "Dwayne Wade", "Paul George", "Kevin Durant"};
double points [4][4]=  {{36,37,29,38},
                        {15,14,17,24},
                        {22,18,0,26},
                        {27,27,23,38}
                       };

//Function that will get the total of points scored by all 4 players...
void getTotal()
{
    int r,c;
    double sum;
    for (r=0; r<4; r++)
    {
        sum=0;
        for (c=0; c<4; c++)
            sum=sum+points[r][c];
    }

//Function that will get the average points scored by all 4 players...
void getAverage()
    {
        int r,c;
        double sum, sum1;
        for (r=0; r<4; r++)
        {
            sum=0;
            for (c=0; c<4; c++)
            sum=sum+points[r][c];
            sum1=sum/16;
cout<<"The average is " <<sum1;
        }
    }
//Main Program

}int main()
{

    getTotal();
    getAverage();
    return 0;
}
Add a } on line 27 and remove the one on line 44.
Look that, all I needed was a fresh pair of eyes.


Thank You!
1
2
3
4
5
6
7
8
9
//Function will get the total from every row...
void getRowTotal ()
{
    int r;
    double sum;
        for (r=0; r<4; r++)
            sum=sum+points[r];
}
    


am i doing something wrong in this function? Im getting error saying that, "invalid operands to " 'binary' and 'binary*'"
points is a 2d array so when you do points[r] you are returning a pointer to a double. To add up a row you would have to add up the columns of that row. Also your sum is uninitialized so it would be an arbitrary result anyways.
1
2
3
4
5
6
7
8
9
//Function will get the total from every column...
void getColumnTotal()
{
    int c;
    double sum=0;
    for (c=0; c<4; c++)
        sum=sum+points[0][c];
    
}


Like this?
If you want the total of the first row.
what if i need the total of every row? would i need the user to input which row he/she needs or can i just display all of them?
i guess my question is, how must i manipulate the current function if have to output all of the row totals, instead of just the first row?
Last edited on
iterate over each row then in each row iterate over each column for the sum of the row.

1
2
3
4
5
6
7
8
for(int row = 0; row < rows; ++row) //rows is the total rows in your case 4
{
    int sum = 0; //set to 0 on each new row
    for(int column = 0; column < columns; ++column) //columns is total columns
    {
        sum += array[row][column]; //array is the name of array in your case points
    }
}
Topic archived. No new replies allowed.