У меня возникли проблемы с моей отмеченной «медианной» функцией. Я получаю сообщение об ошибке «Лошадь [10] [double]» для индекса массива. Я не уверен, как это исправить, и я ищу дружескую помощь.Недопустимые типы индексированных ошибок
#include <cstdlib>
#include <iostream>
#include <math.h>
/*
Name: Horses2
Author: Grant Birkinbine
Date: 03/12/14 18:25
Description: A program that modifies the 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
for (int i =0 ; i<10 ; i++) {
x = x + ahorse[i].gettime() ;
}
avg = (x/10);
//Max
for(int i = 0 ; i < 10 ; i++){
max = -1000;
if (ahorse[i].gettime() > max){
max = ahorse[i].gettime();
}
}
//Min
for(int i = 0 ; i < 10; i ++){
min = 1000;
if (ahorse[i].gettime() < min){
min = ahorse[1].gettime() ;
}
}
for(int i = 0; i < 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;
}
}
Где ошибка? Я ничего не вижу. – David