非常卡住這個問題。我最終得出了平均值,但它給出了負值3200萬等。這裏是我的代碼:C編程(GPA計算器)
#include <stdio.h>
#include <stdlib.h>
int main()
{
float fArray[30];
int choice = 0;
int x = 0;
float total = 0;
float avg = 0;
printf("1. Calculate GPA Average");
printf("\n2. Enter GPA");
printf("\n3. Quit");
printf("\n\nEnter your choice (1-3): ");
scanf("%d", &choice);
if(choice == 2)
{
printf("\n\nEnter GPA: ");
scanf("%.2f\n\n", &fArray[x]);
total = total + fArray[x];
}
else if(choice == 3)
{
return 0;
}
else if(choice == 1)
{
printf("The average is: %f", total/x);
}
for(x = 1; x < 30; x++)
{
fflush(stdin);
int temp = 0;
printf("1. Calculate GPA Average");
printf("\n2. Enter GPA");
printf("\n3. Quit");
printf("\n\nEnter your choice (1-3): ");
scanf("%d", &temp);
if(temp == 2)
{
printf("\n\nEnter GPA: ");
scanf("%.2f\n\n", &fArray[x]);
}
else if(temp == 3)
{
break;
}
else if(temp == 1)
{
printf("The average is: %f", total/x);
}
}
system("pause");
}
這是一個功課題嗎?如果是這樣,請將其標記爲家庭作業。 – snicker 2009-11-12 00:55:22
聞起來像給我做功課。 – 2009-11-12 00:58:52
爲什麼會出現代碼重複? – 2009-11-12 00:59:58