Question

In: Computer Science

please fix code #include <stdio.h> #include <stdlib.h> #include <string.h> // function declarations int getValidJerseyNumber(); int getValidRating();...

please fix code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// function declarations
int getValidJerseyNumber();
int getValidRating();
int main()
{
// declaring variables
int size = 5;
int jerseyNo[size];
int rating[size];
int i = 0, jno, rate;
char option;

/* Getting the inputs entered by the user
* and populate the values into arrays
*/
for (i = 0; i < size; i++)
{
printf("Enter player %d's jersey number:", i + 1);
jerseyNo[i] = getValidJerseyNumber();

printf("Enter player %d's rating:\n", i + 1);
rating[i] = getValidRating();
}

/* This while loop continues to execute
* until the user enters a valid option or 'q/Q'
*/
while (1)
{
// displaying the menu
printf("\nMENU\n");
printf("u - Update player rating\n");
printf("a - Output players above a rating\n");
printf("r - Replace player\n");
printf("o - Output roster\n");
printf("q - Quit\n");
// getting the choice entered by the user
printf("Choose an option:");
scanf("%c", &option);

// Based on the user choice the corresponding case will be executed
switch (option)
{
case 'u':
case 'U':
{
int flag = 0;
printf("Enter a jersey number:");
jno = getValidJerseyNumber();
printf("Enter a new rating for player\n");
rate = getValidRating();
for (i = 0; i < size; i++)
{
if (jerseyNo[i] == jno)
{
rating[i] = rate;
flag = 1;
}
}
if (flag == 0)
{
printf("** Player not found **\n");
}

continue;
}
case 'a':
case 'A':
{

printf("Enter a rating \n");
rate = getValidRating();
printf("Above%d\n", rate);
for (i = 0; i < size; i++)
{
if (rating[i] > rate)
printf("Player %d -- Jersey Number:%d. Rating:%d\n", i + 1, jerseyNo[i],
rating[i]);
}
continue;
}
case 'R':
case 'r':
{
int flag = 0, newjNo;
printf("Enter a jersey number:");
jno = getValidJerseyNumber();
for (i = 0; i < size; i++)
{
if (jerseyNo[i] == jno)
{
printf("Enter a new jersey number:\n");
newjNo = getValidJerseyNumber();
jerseyNo[i] = newjNo;
printf("Enter a rating for the new player:");
rate = getValidRating();
rating[i] = rate;
}
}
continue;
}
case 'o':
case 'O':
{
printf("\nROSTER\n");
for (i = 0; i < size; i++)
{
printf("Player %d -- Jersey Number:%d. Rating:%d\n", i + 1, jerseyNo[i],
rating[i]);
}
continue;
}
case 'Q':
case 'q':
{

break;
}
default:
{
printf("** Invalid Option **\n");
continue;
}
}
break;
}


return 0;
}
// This function will get the valid jersey number from the user
int getValidJerseyNumber()
{
int jerseyNum;
while (1)
{
scanf("%d", &jerseyNum);
if (jerseyNum < 0 || jerseyNum > 99)
{
printf("Invalid must be between 0-99\n");
printf("Re-Enter:\n");

continue;
}
else
{
break;
}
}
return jerseyNum;
}
// This function will get the valid rating from the user
int getValidRating()
{
int rate;
while (1)
{
scanf("%d", &rate);
if (rate < 1 || rate > 9)
{
printf("Invalid must be between 1-9\n");
printf("Re-Enter:\n");

continue;
}
else
{
break;
}
}
return rate;
}

-------------------------------------------------------------

This program will store roster and rating information for a soccer team. Coaches rate players during tryouts to ensure a balanced team.

(1) Prompt the user to input five pairs of numbers: A player's jersey number (0 - 99) and the player's rating (1 - 9). Store the jersey numbers in one int array and the ratings in another int array. Output these arrays (i.e., output the roster). (3 pts)

Ex:

Enter player 1's jersey number:
84
Enter player 1's rating:
7

Enter player 2's jersey number:
23
Enter player 2's rating:
4

Enter player 3's jersey number:
4
Enter player 3's rating:
5

Enter player 4's jersey number:
30
Enter player 4's rating:
2

Enter player 5's jersey number:
66
Enter player 5's rating:
9

ROSTER
Player 1 -- Jersey number: 84, Rating: 7
Player 2 -- Jersey number: 23, Rating: 4
...

(2) Implement a menu of options for a user to modify the roster. Each option is represented by a single character. The program initially outputs the menu, and outputs the menu after a user chooses an option. The program ends when the user chooses the option to Quit. For this step, the other options do nothing. (2 pt)

Ex:

MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit

Choose an option:

(3) Implement the "Output roster" menu option. (1 pt)

Ex:

ROSTER
Player 1 -- Jersey number: 84, Rating: 7
Player 2 -- Jersey number: 23, Rating: 4
...

(4) Implement the "Update player rating" menu option. Prompt the user for a player's jersey number. Prompt again for a new rating for the player, and then change that player's rating. (1 pt)

Ex:

Enter a jersey number:
23
Enter a new rating for player:
6

(5) Implement the "Output players above a rating" menu option. Prompt the user for a rating. Print the jersey number and rating for all players with ratings above the entered value. (2 pts)

Ex:

Enter a rating:
5

ABOVE 5
Player 1 -- Jersey number: 84, Rating: 7
...

(6) Implement the "Replace player" menu option. Prompt the user for the jersey number of the player to replace. If the player is in the roster, then prompt again for a new jersey number and rating. Update the replaced player's jersey number and rating. (2 pts)

Ex:

Enter a jersey number:
4
Enter a new jersey number:
12
Enter a rating for the new player:
8

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
PLACE AN EXTRA SPACE WHILE SCANNING FOR INPUT, AS THIS, WILL TERMINATE THE EXISTING SCANNING AND ALLOWS YOU TO ENTER, DUE TO THIS THE MENU ISSUE WAS HAPPENING

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// function declarations
int getValidJerseyNumber();
int getValidRating();
int main()
{
  // declaring variables
  int size = 5;
  int jerseyNo[size];
  int rating[size];
  int i = 0, jno, rate;
  char option;

  /* Getting the inputs entered by the user
* and populate the values into arrays
*/
  for (i = 0; i < size; i++)
  {
    printf("Enter player %d's jersey number:", i + 1);
    jerseyNo[i] = getValidJerseyNumber();

    printf("Enter player %d's rating:\n", i + 1);
    rating[i] = getValidRating();
  }
  char c;
  /* This while loop continues to execute
* until the user enters a valid option or 'q/Q'
*/
  while (1)
  {
    // displaying the menu
    printf("\nMENU\n");
    printf("u - Update player rating\n");
    printf("a - Output players above a rating\n");
    printf("r - Replace player\n");
    printf("o - Output roster\n");
    printf("q - Quit\n");
    // getting the choice entered by the user
    printf("Choose an option:");
    scanf(" %c", &option);

    // Based on the user choice the corresponding case will be executed
    switch (option)
    {
    case 'u':
    case 'U':
    {
      int flag = 0;
      printf("Enter a jersey number:");
      jno = getValidJerseyNumber();
      printf("Enter a new rating for player\n");
      rate = getValidRating();
      for (i = 0; i < size; i++)
      {
        if (jerseyNo[i] == jno)
        {
          rating[i] = rate;
          flag = 1;
        }
      }
      if (flag == 0)
      {
        printf("** Player not found **\n");
      }

      continue;
    }
    case 'a':
    case 'A':
    {

      printf("Enter a rating \n");
      rate = getValidRating();
      printf("Above%d\n", rate);
      for (i = 0; i < size; i++)
      {
        if (rating[i] > rate)
          printf("Player %d -- Jersey Number:%d. Rating:%d\n", i + 1, jerseyNo[i],
                 rating[i]);
      }
      continue;
    }
    case 'R':
    case 'r':
    {
      int flag = 0, newjNo;
      printf("Enter a jersey number:");
      jno = getValidJerseyNumber();
      for (i = 0; i < size; i++)
      {
        if (jerseyNo[i] == jno)
        {
          printf("Enter a new jersey number:\n");
          newjNo = getValidJerseyNumber();
          jerseyNo[i] = newjNo;
          printf("Enter a rating for the new player:");
          rate = getValidRating();
          rating[i] = rate;
        }
      }
      continue;
    }
    case 'o':
    case 'O':
    {
      printf("\nROSTER\n");
      for (i = 0; i < size; i++)
      {
        printf("Player %d -- Jersey Number:%d. Rating:%d\n", i + 1, jerseyNo[i],
               rating[i]);
      }
      continue;
    }
    case 'Q':
    case 'q':
    {

      break;
    }
    default:
    {
      printf("** Invalid Option **\n");
      continue;
    }
    }
    break;
  }

  return 0;
}
// This function will get the valid jersey number from the user
int getValidJerseyNumber()
{
  int jerseyNum;
  while (1)
  {
    scanf("%d", &jerseyNum);
    if (jerseyNum < 0 || jerseyNum > 99)
    {
      printf("Invalid must be between 0-99\n");
      printf("Re-Enter:\n");

      continue;
    }
    else
    {
      break;
    }
  }
  return jerseyNum;
}
// This function will get the valid rating from the user
int getValidRating()
{
  int rate;
  while (1)
  {
    scanf("%d", &rate);
    if (rate < 1 || rate > 9)
    {
      printf("Invalid must be between 1-9\n");
      printf("Re-Enter:\n");

      continue;
    }
    else
    {
      break;
    }
  }
  return rate;
}

Related Solutions

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;...
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> int main(int argc, char *argv[]) {     FILE *myFile;     char fname[20];     //int sum = 0;     int i, j, k, tmp =0;     int num = 0;     int mass = 0;     int count = 0;     int fuel = 0;     int total = 0;     int M[1000];     char ch;     char buffer[32];     printf(" Input the filename to be opened : ");     scanf("%s",fname);     myFile = fopen(fname, "r");     if(myFile == NULL)     {         printf("Can't open file\n");     } while(1)     {         ch =...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main(...
Please implement the 5 questions in source code: #include <stdio.h> #include <stdlib.h> #include <math.h> int main( int argc, char* argv[] ) { // Size of vectors int n = 10000; // Input vectors double *restrict a; double *restrict b; // Output vector double *restrict c; // Size, in bytes, of each vector size_t bytes = n*sizeof(double); /* Q1: Allocate memory for vector a (10 points)*/ /* Q2: Allocate memory for vector b (10 points)*/ /* Q3: Allocate memory for vector...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the...
#include <stdio.h> #include <stdlib.h> int play_game(int *); // Returns 0 if player won, 1 if the computer won, 2 if there is a tie, and -1 if the player decides to quit int menu(int *); // Displays choices to user // Receives score array int main() { srand(42); // Seeding Random with 42 int score[3]; // Array keeping Player, Computer, and Tie Scores score [0] = 0; // Player - initialized to Zero score [1] = 0; // Computer -...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter...
CODE A #include<stdio.h> #include<math.h> #include<stdlib.h> #define PI 3.14159265358979323846 int main(){ int diameter; printf("Enter value of diameter between 8 to 60 inches: "); scanf("%d",&diameter); // if(diameter>60 || diameter<=8){ // printf("Error! invalid input"); // exit(0); // } // else{ // float radius = diameter/2; // float volume = (4/3)*PI*radius*radius*radius; // printf("%.2f",volume); // } //check through the while loop if it is valid or in valid while(diameter>60 || diameter<=8){ printf("Invalid input Enter again: "); scanf("%d",&diameter); }    //caluclate the volume of sphere float...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc...
#include <stdio.h> #include <unistd.h> #include <stdlib.h> int main(int argc, char **argv) { int count; if ((argc != 2) || (sscanf(argv[1],"%d",&count) != 1)) { fprintf(stderr,"Usage: %s <integer>\n", argv[0]); exit(1); } pid_t pid1, pid2; while (count > 0) { pid1 = fork(); if (pid1 > 0) { pid2 = fork(); count = count - 2; } else if (pid1 == 0) { count = count - 1; } } exit(0); } Question #1 [2 pts] If the command-line argument passed to this...
Use the given Strings.c and Strings.h module: Strings.c: #include "Strings.h" #include <string.h> #include<stdlib.h> #include<stdio.h> char* substring(char*...
Use the given Strings.c and Strings.h module: Strings.c: #include "Strings.h" #include <string.h> #include<stdlib.h> #include<stdio.h> char* substring(char* str, int iPos){ if(iPos > strlen(str)||iPos < 0)return (char*)NULL; char* substr; substr = &str[iPos]; return substr; } int charPosition(char* str, char c){ char* string = (char*)malloc(strlen(str)+1); int i; for(i = 0; i < strlen(str)+1; i++) { if(str[i] == c) { return i; } } return -1; } Strings.h: #include <string.h> /* substring - return a pointer to the substring beginning at the iPos-th position....
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid;...
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/time.h> int main(int argc, char **argv) { pid_t pid; // Main process's PID=42 pid = fork(); // creates process with PID=36 if (pid == 0) { pid_t pid2 = fork(); // creates process with PID=99 sleep(10); if (pid2 > 0) { sleep(10); exit(0); } else { sleep(30); printf("** ONE **\n"); exit(0); } } else { pid_t pid3 = fork(); // creates process with PID=71 if (pid3 == 0) { sleep(30); exit(0); } pid_t...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double...
Can you translate this C code into MIPS assembly? #include <stdio.h> #include <math.h> #include <stdlib.h> double fact (double); void main () { int angle_in_D; double term, angle_in_R; float sine = 0; unsigned int i = 1; double sign = 1; int n = 1000; printf ("Please enter an angle (Unit: Degree): "); scanf ("%d", &angle_in_D); angle_in_R = angle_in_D * M_PI / 180.0; do { term = pow(-1,(i-1)) * pow (angle_in_R, (2*i - 1)) / fact (2*i - 1); sine =...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h>...
how would i change the for loops to while loops in the code below #include<stdio.h> #include<stdlib.h> int main() { int seed; // Taking seed value as input from the user printf("Enter a seed value (0 to quit): \n"); scanf("%d", &seed); // Running the loop until user enters 0 to quit // count array will count frequency of 0 , 1 , 2 ,3 int count[4]; for (int i = 0; i < 4; i++) count[i] = 0; while (seed !=...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value;...
Please debug the code and answer the questions: #include <stdio.h> typedef struct node { int value; struct node *next; } node; int ll_has_cycle(node *first) { node * head = first; while (head->next) { head = head->next; if (head == first) return 1; } return 0; } void test_ll_has_cycle(void) { int i,j; node nodes[5]; for(i=0; i < sizeof(nodes)/sizeof(node); i++) { nodes[i].next = NULL; nodes[i].value = i; } nodes[0].next = &nodes[1]; nodes[1].next = &nodes[2]; nodes[2].next = &nodes[1]; printf("Checking first list for cycles....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT