題目內容:撰寫一個程式包括主函式和fun函式:主函式的功能是將用戶輸入的m個人的成績存放在score陣列中,呼叫fun函式,并輸出最高分和最高分所在的序號;函式fun的功能是找到最高分及最高分所在的序號,將最高分存放在max中,將序號存放在Num中; 要求采用2個全域變數實作。 fun函式的原型: void fun(int score[],int m) 測驗用例:當score陣列中的資料為10、20、30、90、60、70、80 時,main函式輸出90,4
我寫的程式:
#include<stdio.h>
int max;
int Num;
int main() {
void fun(int score[], int m);
int n;
int m = 7;
int score[7] = { 0 };
for (int i = 0; i < 7; i++) {
scanf("%d", &n);
score[i] = n;
}
fun(score, m); //找最大值
printf("最高分為%d,最高分序號為%d", max, Num); //輸出
return 0;
}
void fun(int score[], int m) {
int j;
int max = score[0];
for (j = 0; j < m; j++) {
if (score[j] > max)
max = score[j];
Num = j + 1;
}
}沒有報錯但結果不對




uj5u.com熱心網友回復:
來個大佬救救孩子吧
uj5u.com熱心網友回復:
你在fun里定義int max,則把全域的max屏蔽了,num要在if里
void fun(int score[], int m)
{
int j;
max = score[0];
for(j = 0; j < m; j++)
{
if(score[j] > max)
{
max = score[j];
Num = j + 1;
}
}
}
uj5u.com熱心網友回復:
(^???^)感謝感謝
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/244937.html
標籤:新手樂園
上一篇:為啥輸入100結果不對?
