2014-10-12 2 views
0

내 코드가 작동하지 않는 것은 :C 간단한 배열 코드가

#include <stdio.h> 
int main(){ 
int i, choice; 
int player [11] = {1, 2, 10, 13, 21, 22, 24, 25, 31,32, 33}; 
int points [11] = {60, 297, 11, 373, 154, 52, 555, 218, 29, 242, 257}; 
int games [11] = {33, 35, 12, 35, 35, 35, 35, 35, 22,35, 35}; 
int assists [11] = {64 , 27 , 2 , 112 , 23 , 3 , 53 , 39 , 4 , 15 , 9 }; 
int rebounds[11] = {30,134,3,122,85,43,210,58,17,211,169}; 
const char* names[11] = { 
    "Jaylon Tate","Joe Bertrand","Jaylon Tate","Tracy Abrams","Malcolm Hill","Mav Morgan","Rayvonte Rice","Kendrick Nunn","Austin Colbert","Nnanna Egwu","Jon Ekey" 
}; 
printf("Number\tGames\tPoints\tAssists\tRebounds\n"); 
int bestplayerppg = 0; 
int bestplayerapg = 0; 
int bestplayerrpg = 0; 
float bestppg = 0.0; 
float bestapg = 0.0; 
float bestrpg = 0.0; 
float ppg [11] ; 
float apg [11]; 
float rpg [11]; 
int bestIndexppg = 0; 
int bestIndexapg = 0; 
int bestIndexrpg = 0; 
for (i=0; i<11; i++){ 
    ppg[i] = (float)points [i]/(float)games [i] ; 
    apg[i] = (float)assists [i]/(float)games [i] ; 
    rpg[i] = (float)rebounds [i]/(float)games [i] ; 
    printf("%s \t %d \t %d \t %d \t %d \t %.1f ppg \t %.1f apg \t %.1f rpg\n", names[i], games[i], points[i], assists[i], rebounds[i], ppg[i], apg[i], rpg[i]); 
    if (ppg[i]>bestplayerppg){ 
     bestplayerppg = player[i]; 
     bestppg = ppg[i]; 
     bestIndexppg = i; 
    } 
    if (apg[i]>bestplayerapg){ 
     bestplayerapg = player[i]; 
     bestapg = apg[i]; 
     bestIndexapg = i; 
    } 
    if (rpg[i]>bestnorpg){ 
     bestplayerrpg = player[i]; 
     bestrpg = rpg[i]; 
     bestIndexrpg = i; 
    } 
} 
printf("Pick the stat you want to view:\n1. Points per game\n2. Assists per game\n3. Rebounds per game\nEnter a choice: "); 
scanf("%d",&choice); 
printf("The player with the most "); 
switch (choice){ 
    case 1: 
    printf("points per game is #%d %s with %.1f ppg.\n", bestplayerppg, names[bestIndexppg], bestppg); 
    break; 
    case 2: 
    printf("assists per game is #%d %s with %.1f apg.\n", bestplayerapg, names[bestIndexapg], bestapg); 
    break; 
    case 3: 
    printf("rebounds per game is #%d %s with %.1f rpg.\n", bestplayerrpg, names[bestIndexrpg], bestrpg); 
    break; 
    default: 
    printf("Choice is not valid\n"); 
} 
} 

코드는 점 어시스트를 완벽하게 작동합니다. 하지만 최고의 RPG를 찾을 때 작동하지 않습니다. 그것은 # 13 트레이시 일컬어 배열의 네 번째 요소는 그가하지 않을 때 가장 rpg있다. 내 포인트, 어시스트 로직 및 리바운드는

+3

세 번째 – mch

답변

0
if (rpg[i]>bestnorpg){ 
     bestplayerrpg = player[i]; 
     bestrpg = rpg[i]; 
     bestIndexrpg = i; 
} 

bestnorpg가 어디 선언되지 ... 동일하지만 포인트이며 작업을 지원합니다.

은 다음보십시오 :

잘못되면
if (rpg[i]>bestrpg){ 
     bestplayerrpg = player[i]; 
     bestrpg = rpg[i]; 
     bestIndexrpg = i; 
    } 
+0

'이 될 suppossed bestrpg' 아닌가요'bestplayerrpg'? –

+0

이 코드를 bestrpg와 함께 실행하면 효과적입니다. – kodaman