2014-10-12 42 views
0

我的代码是:ç简单数组代码是不能工作

#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 2014-10-12 02:53:44

回答

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

是不是'bestrpg' suppossed是'bestplayerrpg'? – 2014-10-12 03:35:02

+0

我用bestrpg运行这段代码,它工作。 – kodaman 2014-10-12 04:32:31