Question

In: Computer Science

Confused about going through this C program. Thank you Program Specifications: *********************************************** ** MAIN MENU **...

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.

Solutions

Expert Solution

#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:


Related Solutions

(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main()...
(In C++) Bank Account Program Create an Account Class Create a Menu Class Create a main() function to coordinate the execution of the program. We will need methods: Method for Depositing values into the account. What type of method will it be? Method for Withdrawing values from the account. What type of method will it be? Method to output the balance of the account. What type of method will it be? Method that will output all deposits made to the...
Design c++ program so that it correctly meets the program specifications given below.   Specifications: Create a...
Design c++ program so that it correctly meets the program specifications given below.   Specifications: Create a menu-driven program that finds and displays areas of 3 different objects. The menu should have the following 4 choices: 1 -- square 2 -- circle 3 -- right triangle 4 -- quit If the user selects choice 1, the program should find the area of a square. If the user selects choice 2, the program should find the area of a circle. If the...
C++ Write a menu based program for the pet rescue. There should be 2 menu options...
C++ Write a menu based program for the pet rescue. There should be 2 menu options -Add a pet -View pets -If the user chooses option 1, you will open a data file for writing without erasing the current contents, and write a new pet record to it. The file can be formatted any way that you choose but should include the pet's name, species, breed, and color. You my also include any additional information you think is appropriate. -If...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
Language: Java Create a TryIt.java program with a main method. You are going to use this...
Language: Java Create a TryIt.java program with a main method. You are going to use this program to demonstrate some things about how Java works. Make your methods here private, because they are not intended to be called from outside the program. For each test below, make sure that you print to the console: 1) What is being tested 2) The desired output 3) The actual output Question to be answered: Should you use == or the String method equals...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement:...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1...
In this assignment you are going to use the menu you created in Assignment 1 to...
In this assignment you are going to use the menu you created in Assignment 1 to test both your Double and Integer classes. You should add functionality to the menu to allow you test the add, sub, mul, div functions for instances of both classes You are going to have make a modification to your menu class. Currently it uses an array to hold a fixed amount of menu items. While this may be OK for most applications we want...
in c++ please In this program you are going to have several files to turn in...
in c++ please In this program you are going to have several files to turn in (NOT JUST ONE!!) hangman.h – this is your header file – I will give you a partially complete header file to start with. hangman.cpp – this is your source file that contains your main function functions.cpp – this is your source file that contains all your other functions wordBank.txt – this is the file with words for the game to use.  You should put 10...
Code in C++ please You are going to write a program for Computer test which will...
Code in C++ please You are going to write a program for Computer test which will read 10 multiple choice questions from a file, order them randomly and provide the test to the user. When the user done the program must give the user his final score
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT