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...
my code is not printing the output #include <stdio.h> #include<stdbool.h> int main(){ // variable declarations   bool...
my code is not printing the output #include <stdio.h> #include<stdbool.h> int main(){ // variable declarations   bool binary[12] = {false,false, false, false,false,false,false,false,false,false,false, false};   int powerTwo[12] = {2048.1028,576, 256, 128, 64, 32, 16, 8 , 4, 2, 1};   int oneZero[9]= {0,0,0,0,0,0,0,0,0};   int tempVal = 0;   double decimal = 0;   int i = 0;   int j = 0;   // Intialization   printf("Starting the CPSC 1011 Decimal to Binary Converter!\n");   printf("Please enter a positive whole number (or EOF to quit): \n");   scanf("%lf", &decimal);   printf("%lf", decimal);...
#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...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include...
I NEED THIS CODE FOR C++ USING MONITORS PLEASE #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define THREADS 10 // Number of Thread //bridge declared with array of character and integer value void Bridge(char array[], int value); // Global Variable int North = 1; //For North Number int South = 1; //For South Number pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; // Setting Up MUTEX for lock //Thread for North farmer void NorthFarmer(){ pthread_mutex_lock(&mutex1); char array[5] = "North"; // North printf("%s Tunbridge...
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....
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t...
example_thread.c #include <stdio.h> #include <stdlib.h> #include <pthread.h> int shared= 0; void race(void); int main(){     pthread_t player1, player2, player3;     pthread_create(&player1, NULL, (void *)race, NULL);     pthread_create(&player2, NULL, (void *)race, NULL);     pthread_create(&player3, NULL, (void *)race, NULL);     pthread_join(player1, NULL);     pthread_join(player2, NULL);     pthread_join(player3, NULL);     printf("Total Number = %d\n", shared);     return 0; } void race(void) {     long i,tmp;     for(i=1; i<=200000; i++) {         tmp = shared;         tmp = tmp + 1;         shared =...
#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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT