1 #include<iostream> 2 #include<stdlib.h> 3 using namespace std; 4 int sort(int *score,int count); //函数间传递数组:int score[]/int *score 5 int ave(int *score,int count); 6 int main() 7 { 8 int count; 9 cout << "请输入成绩个数:"; 10 cin >> count; 11 cout << endl; 12 if(count<=0) 13 cout << "error!"; 14 else 15 { 16 int *point; 17 point=new int[count]; 18 if(point==NULL) 19 { 20 cout << "动态分配内存空间失败!"; 21 exit(0); 22 return NULL; 23 } 24 cout << "请输入成绩:"; 25 for(int i=0;i<count;i++) //注意输入和输出! 26 cin >> point[i]; 27 cout << endl; 28 int average=ave(point,count); 29 cout << "平均分为:"<<average<<endl; 30 sort(point,count); 31 cout << "排序后成绩为:"; 32 for(int i=0;i<count;i++,point++) 33 cout << *point<<" "; 34 } 35 return 0; 36 } 37 38 39 int sort(int *score,int count) 40 { 41 int i,j,tem; 42 for(i=0;i<count;i++) 43 { 44 for(j=i+1;j<count;j++) 45 { 46 if(score[j]>score[i]) 47 { 48 tem=score[j]; 49 score[j]=score[i]; 50 score[i]=tem; 51 } 52 } 53 } 54 } 55 56 int ave(int *score,int count) 57 { 58 int i,sum=0; 59 float ave; 60 for(i=0;i<count;i++) 61 { 62 sum+=score[i]; 63 } 64 ave=sum/count; 65 return ave; 66 }
总算改对了一个。
1 #include<iostream> 2 #include<stdlib.h> 3 using namespace std; 4 int* space(int count); 5 int sort(int *score,int count); 6 int ave(int *socre,int count); 7 int main() 8 { 9 int count; 10 cout << "请输入成绩个数:"; 11 cin >> count; 12 cout << endl; 13 if(count<=0) 14 cout << "error!"; 15 else 16 { 17 int point[count]; 18 *point=*space(count); 19 cout << "请输入成绩:"; 20 for(int i=0;i<count;i++) 21 cin >> point[i]; 22 cout << endl; 23 int average=ave(point,count); 24 cout << "平均分为:"<<average<<endl; 25 sort(point,count); 26 cout << "排序后成绩为:"; 27 for(int i=0;i<count;i++) 28 cout << point[i]<<" "; 29 } 30 return 0; 31 } 32 33 int *space(int count) 34 { 35 int *dyn; 36 dyn=new int [count]; 37 if(dyn==NULL) 38 { 39 cout << "动态分配内存空间失败!"; 40 exit(0); 41 return NULL; 42 } 43 return dyn; //返回空间地址 44 } 45 46 int sort(int *score,int count) 47 { 48 int i,j,tem; 49 for(i=0;i<count;i++) 50 { 51 for(j=i+1;j<count;j++) //注意排序方式! 52 { 53 if(score[j]>score[i]) 54 { 55 tem=score[j]; 56 score[j]=score[i]; 57 score[i]=tem; 58 } 59 } 60 } 61 } 62 63 int ave(int *score,int count) 64 { 65 int i,sum=0; 66 float ave; 67 for(i=0;i<count;i++) 68 { 69 sum+=score[i]; 70 } 71 ave=sum/count; 72 return ave; 73 }