求解答啊 不知道哪錯了 運(yùn)行不出來
#include <stdio.h>
int main()
{
? ? int score[N]={67,98,75,63,82,79,81,91,66,84};
? ? int total = fun1(score);
? ? int max = fun2(score);
? ? int min = fun4(score);
? ? int average = total/10;
? ? fun3(score);
? ? printf("%d",max);
? ? printf("%d",min);
? ? printf("%d",total);
? ? printf("%d",average);
? ? return 0;
}
int fun1(int score[]){
? ? int total = 0;
? ? for(int i=0;i<10;i++){
? ? ? ? total += score[i];
? ? }
? ? return total;
}
int fun2(int score[]){
? ? int max;
? ? for(int i=0;i<9;i++){
? ? ? ? if(score[i]>max){
? ? ? ? ? ? max = score[i];
? ? ? ? }
? ? }
? ? return max;
}
int fun4(int score[]){
? ? int min;
? ? for(int i=0;i<9;i++){
? ? ? ? if(score[i]<min){
? ? ? ? ? ? min = score[i];
? ? ? ? }
? ? }
? ? return min;
}
void fun3(int score[]){
? ? for(int i=8;i>=0;i--){
? ? ? ? for(int j=0;j<=i;j++){
? ? ? ? ? ? if(score[j]<score[j+1]){
? ? ? ? ? ? ? ? int temp;
? ? ? ? ? ? ? ? temp=score[j];
? ? ? ? ? ? ? ? score[j]=score[j+1];
? ? ? ? ? ? ? ? score[j+1]=temp;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? for(int i=0;i<10;i++){
? ? printf("%d ",score[i]);
? ? }
}
2018-11-23
求最大值、最小值等幾個函數(shù)判斷循環(huán)結(jié)束的條件應(yīng)該為i<10或i<=9
2018-12-19
必須先聲明函數(shù),然后才能調(diào)用函數(shù),而且數(shù)組里的N是不用加的,放在編譯器里是錯誤的,因?yàn)镹并未聲明,慕課網(wǎng)的代碼運(yùn)行有一點(diǎn)問題,換成下邊在編譯器運(yùn)行是可以的,希望可以幫助到你。
#include <stdio.h>
int fun1(int score[]){
? ? int total = 0;
? ? for(int i=0;i<10;i++)
? ? {
? ? ? ? total += score[i];
? ? }
? ? return total;
}
int fun2(int score[]){
int max=score[0];
? ? for(int i=0;i<9;i++)
? ? {
? ? ? ? if(score[i]>max)
? ? ? ? {
? ? ? ? ? ? max = score[i];
? ? ? ? }
? ? }
? ? return max;
}
int fun4(int score[]){
? ? int min=score[0];
? ? for(int i=0;i<9;i++){
? ? ? ? if(score[i]<min){
? ? ? ? ? ? min = score[i];
? ? ? ? }
? ? }
? ? return min;
}
void fun3(int score[]){
? ? for(int i=8;i>=0;i--){
? ? ? ? for(int j=0;j<=i;j++){
? ? ? ? ? ? if(score[j]<score[j+1]){
? ? ? ? ? ? ? ? int temp;
? ? ? ? ? ? ? ? temp=score[j];
? ? ? ? ? ? ? ? score[j]=score[j+1];
? ? ? ? ? ? ? ? score[j+1]=temp;
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? for(int i=0;i<10;i++){
? ? printf("%d ",score[i]);
? ? }
? ? printf("\n");
}
int main()
{??
? ? int score[]={67,98,75,63,82,79,81,91,66,84};
? ? int total = fun1(score);
? ? int max = fun2(score);
? ? int min = fun4(score);
? ? int average = total/10;
? ? fun3(score);
? ? printf("%d\n",max);
? ? printf("%d\n",min);
? ? printf("%d\n",total);
? ? printf("%d\n",average);
? ? return 0;
}
2018-12-01
除了循環(huán)條件外,還有在函數(shù)實(shí)現(xiàn)的時(shí)候int score[N]是不對的,改成int score[]就好了