我在标记的“Median”功能中遇到了一些麻烦。我收到一个错误,表示数组下标的'Horse [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;
}
}
答案 0 :(得分:0)
首先,您不能使用double来下标数组,因此ahorse[x].gettime()
不能工作,数组必须使用整数进行下标。想一想,ahorse[9.5]
会引用什么元素?
其次,模数运算符%
仅适用于整数。如果你想在双重
第三,数组从零开始索引,因此中间部分对ahorse[10]
的访问是访问它之外的数组,并且作为{{的最后一个元素是无效的内存访问。 1}}数组是Horse ahorse[10]
。
可能还有其他问题,但上面提到的问题是我第一眼看到的问题。