Code a program in C that prints a list of levels in a game, along with a count o
ID: 3930344 • Letter: C
Question
Code a program in C that prints a list of levels in a game, along with a count of how many players are at each level. The level will be determined by the amount of health points, using this guide:
Level
Points
Level 1
0 – 9
Level 2
10 – 19
Level 3
20 – 29
Level 4
30 – 39
Level 5
40 – 49
Level 6
Anything 50 or over
Player points are entered by the user. Your program must use a sentinel-controlled loop to process the data typed by the user, until the user types -1.
After all the data has been entered, display the final counts in a nicely-formatted chart.
Requirements
The number of players at each level is to be stored in an array. So the first element in the array stores the count for Level 1, the second element in the array stores the count for Level 2, etc.
You must use a function called updateLevel to update the count for a level. This function needs two arguments: the points for a player (input argument) and the array (output argument – but remember, it’s an array). Within the function, use a selection statement to determine which element of the array should be incremented.
You must use a function called displayLevels to display the contents of the array after all the data has been entered. This function needs one argument (the array). Line up the output neatly. You must use a loop in this function to display the output.
The results must line up at the decimal point.
Level
Points
Level 1
0 – 9
Level 2
10 – 19
Level 3
20 – 29
Level 4
30 – 39
Level 5
40 – 49
Level 6
Anything 50 or over
Explanation / Answer
#include<stdio.h>
int main()
{
void updateLevel(int, int []);
void displayLevels(int []);
int point, c;
//Initialized all levels to zero
int Level[6];
for(c = 0; c < 6; c++)
Level[c] = 0;
//Loops till -1 is entered
do
{
//Accepts Point
printf(" Enter Player points: ");
scanf("%d", &point);
//If -1 is entered stop data accept
if(point == -1)
break;
updateLevel(point, Level);
}while(1);
displayLevels(Level);
}
//Updates the level based on criteria
void updateLevel(int p, int lev[])
{
if(p >= 0 && p <= 9)
lev[0]++;
else if(p >= 10 && p <= 19)
lev[1]++;
else if(p >= 20 && p <= 29)
lev[2]++;
else if(p >= 30 && p <= 39)
lev[3]++;
else if(p >= 40 && p <= 49)
lev[4]++;
else
lev[5]++;
}
//Displays the Level with counter
void displayLevels(int L[])
{
int c;
for(c = 0; c < 6; c++)
{
printf(" Level = %d Counter = %d ",c+1, L[c]);
}
}
Output:
Enter Player points: 9
Enter Player points: 5
Enter Player points: 11
Enter Player points: 12
Enter Player points: 23
Enter Player points: 22
Enter Player points: 34
Enter Player points: 35
Enter Player points: 36
Enter Player points: 41
Enter Player points: 40
Enter Player points: 55
Enter Player points: 58
Enter Player points: 80
Enter Player points: -1
Level = 1 Counter = 2
Level = 2 Counter = 2
Level = 3 Counter = 2
Level = 4 Counter = 3
Level = 5 Counter = 2
Level = 6 Counter = 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.