Tutorial number 40

+1 Baba Iskandarov · December 30, 2015
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>

int main() {
int i;
int player[5] = {66, 68, 59, 71, 87};
int goals[5] = {39, 28, 31, 25, 19};
int gamesPlayed[5] = {30, 30, 28, 30, 26};
float ppg[5];
float bestPpg = 0.0;
float worstPpg = 1.0;
int bestPlayer, worstPlayer;

for (i = 0; i < 5; i++) {
ppg = (float)goals / (float)gamesPlayed;
printf("%d \t %d \t %d \t %.2f \n", player, goals, gamesPlayed, ppg);

if (ppg > bestPpg) {
bestPpg = ppg;
bestPlayer = player;
}
if (ppg < worstPpg) {
worstPpg = ppg;
worstPlayer = player;
}
}

printf("\nThe best player is %d\n", bestPlayer);
printf("\nHe scores %.2f\n", bestPpg);

printf("\nThe best player is %d\n", worstPlayer);
printf("\nHe scores %.2f\n", worstPpg);

return 0;
}

i added worsplayer for fun.
can anyone explain me why if statement is using there and how it works ??

Post a Reply

Replies

Oldest  Newest  Rating
+1 Ossama Memon · December 30, 2015
If statement is used there to check out the best player and in your program the worst player as well.
The for loop goes through each player and checks if the current player score is the greatest score of all other players. If yes then the program will print out according to greatest score found = current player score.
0 Baba Iskandarov · December 31, 2015
Ossama Memon thank you for answer but the site ignored some characters
  • 1

C

107,193 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator