In: Computer Science
Confused about going through this C program. Thank you
Program Specifications:
***********************************************
** MAIN MENU **
***********************************************
A) Enter game results
B) Current Record (# of wins and # of losses and # of ties)
C) Display ALL results from all games WON
D) Display ALL results ordered by opponent score from low to high.
E) Quit
Your program will have a menu similar to the example above. The game
results will simply be the score by your team and the score by your
opponent. The user will enter a single game result at a time. This
assignment requires the use of a 2D array. You will not need the date of
the game or the names of the teams. All cases will be written within user
defined function. Functions should be used as much as possible.
YOU CANNOT:
Use global variables, in this or any program ever.
Use goto statement(s), in this or any program ever.
#include<stdlib.h>
#include<stdio.h>
#define MAX_GAME 100
void print_menu()
{
printf("***********************************************\n");
printf("** MAIN MENU **\n");
printf("***********************************************\n");
printf("A) Enter game results\n");
printf("B) Current Record(# of wins and # of losses
and # of ties)\n");
printf("C) Display ALL results from all games
WON\n");
printf("D) Display ALL results ordered by opponent
score from low to high\n");
printf("E) Quit\n");
}
int new_entry(int arr[MAX_GAME][3],int n)
{
int your_team;
int opponent_team;
printf("Please enter score of both team : your_score
opponent score ");
scanf("%d %d", &your_team,
&opponent_team);
arr[n][0] = your_team;
arr[n][1] = opponent_team;
arr[n][2] = your_team > opponent_team ? 1 :
your_team < opponent_team ? 2 : 0;
// tie :0 , loss : 2 , win : 1
return n + 1;
}
void current_record(int arr[MAX_GAME][3], int n)
{
int count[3] = { 0, 0, 0 };
for (int i = 0; i < n; i++)
{
count[arr[i][2]]++;
}
printf("%d of wins and %d of losses and %d of ties
\n", count[1], count[2], count[0]);
}
void print_game(int a,int b)
{
printf(" %d\t %d \n",
a,b);
}
void won_game(int arr[MAX_GAME][3], int n)
{
printf("YourScore OpponentScore\n");
for (int i = 0; i < n; i++)
{
if (arr[i][2] == 1)
print_game(arr[i][0],arr[i][1]);
}
}
void print_sort(int arr[MAX_GAME][3], int n)
{
int *opponent = (int *)malloc(sizeof(int)*n);
int *your_score = (int *)malloc(sizeof(int)*n);
opponent[0] = arr[0][1];
your_score[0] = arr[0][0];
for (int i = 1; i < n; i++)
{
int j;
int flag = 1;
for (j = i - 1; j >= 0;
j--)
{
if (arr[i][1]
< opponent[j])
{
opponent[j + 1] = opponent[j];
your_score[j + 1] = your_score[j];
flag = 0;
}
else
break;
}
opponent[j +
1] = arr[i][1];
your_score[j +
1] = arr[i][0];
}
printf("YourScore OpponentScore\n");
for (int i = 0; i < n; i++)
print_game(your_score[i],
opponent[i]);
}
int main()
{
int arr[MAX_GAME][3];
int game = 0;
print_menu();
char mode;
while (1)
{
printf("Provide mode according to
menu: ");
scanf("%c", &mode);
while (mode < 'A' || mode >
'E')scanf("%c", &mode);
if (mode == 'E') break;
else if (mode == 'A') game =
new_entry(arr, game);
else if (mode == 'B')
current_record(arr, game);
else if (mode == 'C') won_game(arr,
game);
else if (mode == 'D')
print_sort(arr, game);
}
return 0;
}
Sample input and output: