The program I have works. Once I try to clean it up with functions I get totally lost. Can someone please review my code and help me understand what I'm doing wrong?
The program output needs to look like this (please note, this photo was taken prior to adding functions):
Ideally, after the update_level
loop, I would then print the totals.
Below is my code with functions that I don't understand at all.
#include <stdio.h>
int update_level(int player[],int healthpoint);
int display_levels();
int main(void)
{
int levels[6] = {1,2,3,4,5,6}; // levels is an array of 6 integers
int players[6] = {0,0,0,0,0,0}; // players is an array of 6 integers
int healthpoints; // initialize variable
printf("Enter total player health points (-1 to quit): "); // user input
scanf("%d", &healthpoints); // reads user input
for(j=0; j<=5; j++)
{
update_level(players[j],healthpoints);
}
return;
} // end main
int update_level(int player[],int healthpoint);
{
while (healthpoints != -1)
{
if ( healthpoints <= 9)
{
players[0] += 1;
} //end if
else if (healthpoints <= 19)
{
players[1] += 1;
} //end else if
else if (healthpoints <= 29)
{
players[2] += 1;
} //end else if
else if (healthpoints <= 39)
{
players[3] += 1;
} //end else if
else if (healthpoints <= 49)
{
players[4] += 1;
} //end else if
else
{
players[5] += 1;
} // end else
printf("Enter total player health points (-1 to quit): "); // user input
scanf("%d", &healthpoints); // reads user input
} // end while
return;
} // end update_level
int display_levels()
{
int i; // initialize variable
printf("T O T A L S\n\n");
for (i = 0; i < 6; ++i){
printf("Level %u%13d\n", levels[i], players[i]);
} // end for
return;
} // end display_levels
With some help, I was able to debug my program. Below is the correct code.
#include <stdio.h>
int update_level(int players[],int healthpoints);
int display_levels(int levels[],int players[]);
int main(void)
{
int levels[6] = {1,2,3,4,5,6}; // levels is an array of 6 integers
int players[6] = {0,0,0,0,0,0}; // players is an array of 6 integers
int healthpoints; // initialize variable
printf("Enter total player health points (-1 to quit): "); // user input
scanf("%d", &healthpoints); // reads user input
update_level(players,healthpoints);
display_levels(levels,players);
return;
} // end main
int update_level(int players[],int healthpoints)
{
while (healthpoints != -1)
{
if ( healthpoints <= 9)
{
players[0] += 1;
} //end if
else if (healthpoints <= 19)
{
players[1] += 1;
} //end else if
else if (healthpoints <= 29)
{
players[2] += 1;
} //end else if
else if (healthpoints <= 39)
{
players[3] += 1;
} //end else if
else if (healthpoints <= 49)
{
players[4] += 1;
} //end else if
else
{
players[5] += 1;
} // end else
printf("Enter total player health points (-1 to quit): "); // user input
scanf("%d", &healthpoints); // reads user input
} // end while
return;
} // end update_level
int display_levels(int levels[],int players[])
{
int i; // initialize variable
printf("T O T A L S\n\n");
for (i = 0; i < 6; ++i){
printf("Level %u%13d\n", levels[i], players[i]);
} // end for
return;
} // end display_levels
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.