Monday, 15 September 2014

How do you make an array of strings in C? -



How do you make an array of strings in C? -

so far way can create code work switch statements. there way can take away switch statement , create array. heard can do: char name[11]= {"name 1", "name 2"}; , on i'm not sure how print later on in program. because numbers assigned non-array variable , used printf print it.

my code:

#include <stdio.h> int main(){ int i; 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 bestplayer = 0; float bestppg = 0.0; float ppg [11] ; (i=0; i<11; i++){ ppg[i] = (float)points [i] / (float)games [i] ; printf("%d \t %d \t %d \t %.1f ppg\n", player[i], games[i], points[i],ppg[i]); if (ppg[i]>bestplayer){ bestplayer = player[i]; bestppg = ppg[i]; } } printf("\nthe player points per game #%d ", bestplayer); switch(bestplayer){ case 1: printf("player 1"); break; case 2: printf("player 2"); break; case 10: printf("player 3"); break; case 13: printf("player 4"); break; case 21: printf("player 5"); break; case 22: printf("player 6"); break; case 24: printf("player 7"); break; case 25: printf("player 8"); break; case 31: printf("player 9"); break; case 32: printf("player 10"); break; case 33: printf("player 11"); break; default: printf("invalid player"); break; } printf(" %.1f ppg.\n",bestppg); homecoming 0; }

the main problem using array of char* in current construction don't track index of best player. if this, can create array , index it.

#include <stdio.h> int main(){ int i; 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}; const char* names[11] = { "jaylon tate","joseph bertrand","jaylon tate","tracy abrams","malcolm hill","maverick morgan","rayvonte rice","kendrick nunn","austin colbert","nnanna egwu","jon ekey" }; int bestplayer = 0; float bestppg = 0.0; float ppg [11] ; int bestindex = 0; (i=0; i<11; i++){ ppg[i] = (float)points [i] / (float)games [i] ; printf("%d \t %d \t %d \t %.1f ppg\n", player[i], games[i], points[i],ppg[i]); if (ppg[i]>bestplayer){ bestplayer = player[i]; bestppg = ppg[i]; bestindex = i; } } printf("\nthe player points per game #%d %s %.1f ppg.\n", bestplayer, names[bestindex], bestppg); homecoming 0; }

c arrays string

No comments:

Post a Comment