In: Computer Science
#C language
array must store at least 50 inputs
Game Scores Assignment
Outcomes:
Program Specifications:
Your program will read in "game scores" from the user. Each score consists of a score for YOUR team and a score for the OTHER team. You don't need names or dates for the games. Just the scores. You'll use a 2D array to store the scores. The following menu options all need to be implemented:
***********************************************
** MAIN MENU **
***********************************************
A) Enter game result
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
Create a menu similar to the example above. The user will enter a single game result at a time. All menu options should be implemented within programmer-defined function. Functions should be used as much as possible.
Submission Requirements:
Requirements will be same as the first assignment which will be the same for all future assignments.
YOU MAY NOT
CODE:
#include<stdio.h>
void current_record(int game_scores[50][2],int index);
void game_won(int game_scores[50][2],int index);
void opplow_high(int game_scores[50][2],int index);
void display_all(int game_scores[50][2],int index);
void main()
{
int game_scores[50][2];
int a,index=0,quit=0;
while(1){
printf("\n1) Enter game result");
printf("\n2) Current Record (# of wins and # of losses and # of
ties)");
printf("\n3) Display ALL results from all games WON");
printf("\n4) Display ALL results ordered by opponent score from low
to high");
printf("\n5) Quit");
printf("\nEnter your option:");
scanf("%d",&a);
switch(a){
case 1:
printf("\nEnter your and other team score:");
scanf("%d%d",&game_scores[index][0],&game_scores[index][1]);
index++;
break;
case 2:
current_record(game_scores,index);
break;
case 3:
game_won(game_scores,index);
break;
case 4:
opplow_high(game_scores,index);
display_all(game_scores,index);
break;
case 5:
quit=1;
break;
default:
printf("\nEnter proper input...\n");
}
if(quit==1)
break;
}
}
void current_record(int game_scores[50][2],int index)
{
int i,w=0,l=0,t=0;
for(i=0;i<index;i++){
if(game_scores[i][0]==game_scores[i][1])
t++;
else if(game_scores[i][0]>game_scores[i][1])
w++;
else
l++;
}
printf("\nwin = %d\nlose = %d\ntie = %d",w,l,t);
}
void game_won(int game_scores[50][2],int index)
{
int i;
printf("\nwinning results:\n");
for(i=0;i<index;i++){
if(game_scores[i][0]>game_scores[i][1])
printf("\nyour score=%d opponent
score=%d",game_scores[i][0],game_scores[i][1]);
}
}
void opplow_high(int game_scores[50][2],int index)
{
int i,j,temp[2];
for(i=0;i<index;i++){
for(j=i+1;j<index;j++){
if(game_scores[i][1]>game_scores[j][1]){
temp[0]=game_scores[j][0];
temp[1]=game_scores[j][1];
game_scores[j][0]=game_scores[i][0];
game_scores[j][1]=game_scores[i][1];
game_scores[i][0]=temp[0];
game_scores[i][1]=temp[1];
}
}
}
}
void display_all(int game_scores[50][2],int index)
{
int i,j;
for(i=0;i<index;i++){
printf("\nyour score=%d opponent
score=%d",game_scores[i][0],game_scores[i][1]);
}
}
MENU DRIVEN PROGRAM:
For a given menu driven task ,we have multiple tasks to be solve .So simply we use switch class to solve the perticular task.For iterate the tasks simply we use while loop outside the switch class for multiple iterations.Each of the menu item is solved in each of the switch case.
2D ARRAY:
we can simply declare a 2 dimensional array in c like,
int array[20][20]; //we have to mention the size of the array.
if we want to initialize the array simply initalizing by using array indices .ex:array[1][1]=5;
If we want to access the array elements then called it by using array indices ex:array[2][2]
USE OF FUNCTIONS:
Functions to used to perform the same task multiple times in diffent situations.If any task which is known to execute multiple times then that code must write in a function.if we want execute that perticular code then we simply call the function.
current_record(int game_scores[50][2],int index) :display the
number of wins,loses,ties.
void game_won(int game_scores[50][2],int index) :display scores
which you won.
void opplow_high(int game_scores[50][2],int index) :sort the scored
based on opponent score.
void display_all(int game_scores[50][2],int index) :display all the
scores.
GOOD PROGRAMMING STYLE:
A good programmer must build an Algorithm before writing the code.Algorithm means a step by step process to complete a perticular task.
For Example:For a menu driven program,first programmer read the required inputs like 2d array .next Write the functions for each menu Item task .Those Functions calls put in each of switch case.For multiple iterations use while loop.