2017-04-05 52 views
-1

我想製作一個程序來總結分數並通過使用二維數組找到最高分數。 我遇到了一些奇怪的事情,沒有任何錯誤。 我發現循環(帶有cla,stu變量)似乎被跳過。 我試圖在每個循環上打印一些東西,但我找不到原因。 我上的Visual Studio 2017年的工作在Windows 7爲什麼沒有這個循環工作

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

#define makeScore() (rand() % 60 + 40); 

int main() { 

    int arScore[2][3]; 
    int cla, stu; 
    int sum[2]; 
    int maxScore = 0; 
    int maxc = 0, maxs = 0; 
    int i = 0, j = 0; 

    for (i = 0; i < sizeof(arScore)/sizeof(arScore[0]); i++) { 
     for (j = 0; j < sizeof(arScore[0])/sizeof(arScore[0][0]); j++) { 
      arScore[i][j] = makeScore(); 
     } 
    } 

    for (cla = 0; i < sizeof(arScore)/sizeof(arScore[0]); cla++) { 
     sum[cla] = 0; 

     for (stu = 0; j < sizeof(arScore[0])/sizeof(arScore[0][0]); stu++) { 
      sum[cla] += arScore[cla][stu]; 

      if (maxScore < arScore[cla][stu]) { 
       maxScore = arScore[cla][stu]; 
       maxc = cla; 
       maxs = stu; 
      } 
     } 
     printf("the total score of %d class : %d\n", cla + 1, sum[cla]); 
    } 

    puts("the student with the highest score"); 
    printf("%d class, %d student, %d score\n", maxc, maxs, maxScore); 

    return 0;  
} 
+1

「不工作」 是不是一個適當的問題描述。請給出預期的產出和實際產出。你做了什麼來自己調試問題? – kaylum

+0

我認爲你的意思是「最大」而不是「屋頂」。但你的問題仍然不清楚。它以什麼方式不起作用?發生了什麼(具體的輸入)以及你期望的(與這些輸入有關)。 –

+0

感謝您的建議。其實這是我第一次發佈這個問題。所以現在我把我的問題改爲規範。謝謝 –

回答

0

你寫錯了變數,

for (cla = 0; i < sizeof(arScore)/sizeof(arScore[0]); cla++) { // i instead of cla 
     sum[cla] = 0; 

     for (stu = 0; j < sizeof(arScore[0])/sizeof(arScore[0][0]); stu++) { 
      sum[cla] += arScore[cla][stu]; //j instead of stu 


寫這個代替

for (cla = 0; cla < sizeof(arScore)/sizeof(arScore[0]); cla++) { 
     sum[cla] = 0; 

     for (stu = 0; stu < sizeof(arScore[0])/sizeof(arScore[0][0]); stu++) { 
      sum[cla] += arScore[cla][stu]; 
+0

天啊,我感到很慚愧。感謝您的好意 –

+0

@JangchunLee您可以標記爲正確的,如果有幫助 –

相關問題