Calculate median from 2 dimensional array.

I am having trouble figuring out why I am getting the following error. I'm also attaching the section of code I'm working on. A nudge in the right direction would be appreciated.

Error: cannot covert 'double[5]' to 'double' in assignment.

1
2
3
4
5
6
7
8
9
  bubblesort(bowlers,11,scores,11);
    if(rows%2==0)
    {//top of if
        median=(scores[rows/2]+scores[rows/2]-1)/2;
    }//bottom of if
    else
    {//top of else
        median=scores[rows/2];
    }//bottom of else 
You really haven't shown enough code. However from your title, I'm implying that scores is a 2 dimensional array. I'm also guessing that median is a simple double, not an array. If those assumptions are correct, then at lines 4 and 8 you're trying to assign a 1D array (scores[n]) to a simple double.


You are right. I have figured out the error, however, the median is not returning the correct value. the adjusted code is:

1
2
3
4
5
6
7
8
    if(scores[r][c%2]==0)
    {//top of if
        median=(scores[r][c/2]+scores[r][c/2]-1)/2;
    }//bottom of if
    else
    {//top of else
        median=scores[r][c/2];
    }//bottom of else 

Here are my declarations.
1
2
3
4
5
6
7
int r=0,c=0,rows=10,cols=3;
void bubblesort(string arry[],int length,double scores2[][5],int length2);
int main(int argc, char** argv) 
    ifstream infile;
    string bowlers[20];
    double scores[20][5];
    double median=0.0;
Here is the whole code, minus the sort function.
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using namespace std;

int r=0,c=0,rows=10,cols=3;
void bubblesort(string arry[],int length,double scores2[][5],int length2);
int main(int argc, char** argv) 
{//top of main

    ifstream infile;
    string bowlers[20];
    double scores[20][5];
    double median=0.0;
    int pos=0;
    cout<<fixed<<setprecision(2);
    infile.open("c:\\c++data\\bowlers.txt");
    if(!infile)//verify infile is found
    {//top of verify if
        cout<<"cannot open file.\n";
        cout<<"This program will terminate.\n";
        return 1;
    }//bottom of verify if
    //Read Loop
    for(int r=1;r<=rows;r++)
    {//top of r read
        getline(infile,bowlers[r]);
        pos=bowlers[r].find_first_of('\r');
        bowlers[r].erase(pos,2);
        for(c=1;c<=cols;c++)
        {//top of c read
            infile>>scores[r][c];
        }//bottom of c read
        infile.ignore(100,'\n');
    }//bottom of r read
    infile.close();
    //calculate average
    for(r=1;r<=rows;r++)
    {//top of r average across for
        for(c=1;c<=cols;c++)
        {//top of c average across for
            scores[r][4]=scores[r][4]+scores[r][c];
        }//end of c average across for
        scores[r][4]=scores[r][4]/3.0;
    }//end of r average across for
    //average down
    for(c=1;c<=cols;c++)
    {//top of c average down for
        for(r=1;r<=rows;r++)
        {//top of r average down for
            scores[11][c]=scores[11][c]+scores[r][c];
        }//end of r average down for
        scores[11][c]=scores[11][c]/10.0;
    }//end of c average down for
        //Print Loop
    bowlers[11]="Average Score";
    for(int r=1;r<=11;r++)
    {//top of r print
        cout<<left;
        cout<<setw(18)<<bowlers[r];
        for(int c=1;c<=4;c++)
        {//top of c print
            if(r==11&&c==4)
                break;
            cout<<right;
            cout<<setw(8)<<scores[r][c];
        }//end of c print
        cout<<"\n";
    }//end of r print
    //determine lowest score
    double lowest=101.0;
    string lowname="";
    for(r=1;r<=10;r++)
    {//top of lowest for
        if(scores[r][4]<lowest)
        {//top of lowest if
            lowest=scores[r][4];
            lowname=bowlers[r];
        }//end of lowest if
    }//end of lowest for
    //determine highest score
    double highest=-1.0;
    string highname="";
    for(r=1;r<=rows;r++)
    {//top of highest for
        if(scores[r][4]>highest)
        {//top of highest if
            highest=scores[r][4];
            highname=bowlers[r];
        }//end of highest if
    }//end of highest for
    bubblesort(bowlers,11,scores,11);
    if(scores[r][c%2]==0)
    {//top of if
        median=(scores[r][c/2]+scores[r][c/2]-1)/2;
    }//bottom of if
    else
    {//top of else
        median=scores[r][c/2];
    }//bottom of else
    cout<<"\n";
            //Print Loop
    for(int r=1;r<=11;r++)
    {//top of r print
        cout<<left;
        cout<<setw(18)<<bowlers[r];
        for(int c=1;c<=4;c++)
        {//top of c print
            if(r==11&&c==4)
                break;
            cout<<right;
            cout<<setw(8)<<scores[r][c];
        }//end of c print
        cout<<"\n";
    }//end of r print
    cout<<"The lowest average of "<<lowest<<" was bowled by "<<lowname<<".\n";
    cout<<"The highest average of "<<highest<<" was bowled by "<<highname<<".\n";
    cout<<"The median value is "<<median<<".\n";
    return 0;
}//end of main 
Topic archived. No new replies allowed.