c++ - Invalid types of subscript error -
i having trouble in marked "median" function. getting error says `horse[10][double]' array subscript. not sure how fix , looking friendly help.
#include <cstdlib> #include <iostream> #include <math.h> /* name: horses2 author: grant birkinbine date: 03/12/14 18:25 description: program modifies horses program */ //start using namespace std; //class class horse{ private: string name ; int lane; double time; public: horse(string hname , int hlane , double htime){ name = hname ; lane = hlane ; time = htime; } horse(){ name = "" ; lane = 0 ; time = 0 ; } void setname(string hname){ name = hname; } void setlane(int hlane){ lane = hlane; } void settime(double htime){ time = htime; } string getname (){ return name ; } int getlane(){ return lane; } double gettime(){ return time; } void print(){ cout << "horse name: " << name << endl; cout << "horse lane: " << lane << endl; cout << "horse time: " << time << endl; cout << endl; } }; //main void insertion_sort(horse x[],int length); int main(int argc, char *argv[]) { cout << "horse race results: " << endl << endl; horse ahorse[10]; ahorse[0] = horse("american idol " , 1 , 45.41); ahorse[1] = horse("bababooey " , 2 , 42.42); ahorse[2] = horse("charlie horse " , 3 , 40.94); ahorse[3] = horse("dog biscuit " , 4 , 43.55); ahorse[4] = horse("echo " , 5 , 41.41); ahorse[5] = horse("firefox " , 6 , 42.58); ahorse[6] = horse("google " , 7 , 42.58); ahorse[7] = horse("hoof-hearted " , 8 , 44.57); ahorse[8] = horse("ima loozer " , 9 , 41.57); ahorse[9] = horse("just walking " , 10 , 50.00); //sorting int length = 10; insertion_sort(ahorse, length); double avg; double x; double max; double min; avg=0; x = 0; int abovea = 4 ; int belowa = 6 ; int abovem = 4 ; int belowm = 4 ; //average (int =0 ; i<10 ; i++ ) { x = x + ahorse[i].gettime() ; } avg = (x / 10); //max for(int = 0 ; < 10 ; i++){ max = -1000; if ( ahorse[i].gettime() > max){ max = ahorse[i].gettime(); } } //min for(int = 0 ; < 10; ++){ min = 1000; if ( ahorse[i].gettime() < min){ min = ahorse[1].gettime() ; } } for(int = 0; < 10; i++){ ahorse[i].print() ; } //median double median; if(ahorse[x].gettime() % 2 == 0){ median = (ahorse[10].gettime() / 2) + (ahorse[10].gettime() / 2 + 1) / 2; } else{ median = (ahorse[10].gettime() / 2) + 1; } cout << "average: " << avg << endl; cout << "fastest time: " << min << endl; cout << "slowest time: " << max << endl; cout << "# above mean: " << abovea << endl; cout << "# below mean: " << belowa << endl; cout << "# above median: " << abovem << endl; cout << "# below median: " << belowm << endl; cout << endl; system("pause"); return exit_success; } void insertion_sort(horse x[],int length){ horse key; int i; for(int j=1;j<length;j++) { key= x[j]; i=j-1; while(x[i].gettime()>key.gettime() && i>=0) { x[i+1]=x[i]; i--; } x[i+1]=key; } }
first, can't use double subscript array, soahorse[x].gettime()
won't work, arrays has subscripted using integers. think it, element wouldahorse[9.5]
refer to?
second, modulus operator%
only works integers. use fmod if want mod on double
third, arrays indexed starting zero, access toahorse[10]
in median part accessing array outside it's bound , invalid memory access last element of thehorse ahorse[10]
array isahorse[9]
.
there might other issues, ones mentioned above ones spotted @ first glance.
Comments
Post a Comment