Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

8) write a program to compute various offensive ... Your question has been answe

ID: 3761039 • Letter: 8

Question

8) write a program to compute various offensive ... Your question has been answered! Rate it below. Let us know if you got a helpful answer. . Question 8) Write a program to compute various offensive statistics on baseball. The following information will be available on each player: Number of Walks (BB), Strike Outs (SO), Hit by Pitch (HP), Sac Flys (SF), Singles, Doubles (2B), Triples (3B), and Home Runs (HR) as well as Number of At Bats (AB). Based on this information, develop a set of functions within your program that will compute the following: Total Bases, Batting Average, Home Run Ratio, Strike Out Ratio, On Base Percentage, and Slugging Average. You do not need to be a baseball fan to do this ... All the information you need in terms of the formulas and explanations can be found at: http://www.baseball-almanac.com/stats.shtml Note: Number of hits is: singles + doubles + triples + home runs If you want to check your work, see compiled stats of the 2014 Boston Red Sox at: http://www.baseball-reference.com/teams/BOS/2014.shtml

Explanation / Answer

#include <stdio.h>
#include <stdlib.h>             // for malloc
#include <string.h>
#include <ctype.h>

struct Baseball
{
    char first_name [40];       // Player First Name
    char last_name [40];        // Player First Name
    double bb;                  // Number of Walks
    double so;                  // Strike Outs
    double hp;                  // Hit by Pitch
    double sf;                  // Sac Flys
    double sngl;                // Singles
    double dbl;                 // Doubles
    double trpl;                // Triples
    double hr;                  // Home Runs
    double ab;                  // Number of At Bats
    double t_bases;             // Total Bases
    double b_avg;               // Batting Average
    double hr_ratio;            // Home Run Ratio
    double so_ratio;            // Strike Out Ratio
    double ob_prct;             // On Base Percentage
    double s_avg;               // Slugging Average
  
    struct Baseball *next;
};

double total_bases (struct Baseball * data_ptr);
double batting_avg (struct Baseball * data_ptr);
double home_run_ratio (struct Baseball * data_ptr);
double strike_out_ratio (struct Baseball * data_ptr);
double on_base_percentage (struct Baseball * data_ptr);
double slugging_avg (struct Baseball data []);
void calc_data (struct Baseball data []);
void get_data (struct Baseball data []);
void print_data (struct Baseball data []);

int main()
{
  
    char answer[1000];          // to see if the user wants to add more employees
    int more_data = 1;          // flag to check if another employee is to be processed
    char value;                 // gets the first character of answer
  
    struct Baseball *current_ptr, // pointer to current node
    *head_ptr;                  // always points to first node
  
    // Set up storage for first node
    head_ptr = (struct Baseball *) malloc (sizeof(struct Baseball));
    current_ptr = head_ptr;
  
    while (more_data)
    {
        // Call Function to prompt and retreive data
        get_data (current_ptr);
      
        // Call Function to calculate data
        calc_data (current_ptr);
      
        // Call Function to print data
        print_data (current_ptr);
      
        printf(" %s " "%s ", current_ptr -> first_name, current_ptr -> last_name);
        printf("%5.0f ", current_ptr -> bb);
        printf("%5.0f ", current_ptr -> so);
        printf("%5.0f ", current_ptr -> hp);
        printf("%5.0f ", current_ptr -> sf);
        printf("%5.0f ", current_ptr -> sngl);
        printf("%5.0f ", current_ptr -> dbl);
        printf("%5.0f ", current_ptr -> trpl);
        printf("%5.0f ", current_ptr -> hr);
        printf("%5.0f ", current_ptr -> ab);
        printf("%5.0f ", current_ptr ->t_bases);
        printf("%3.3f ", current_ptr -> b_avg);
        printf("%3.3f ", current_ptr -> hr_ratio);
        printf("%3.3f ", current_ptr -> so_ratio);
        printf("%3.3f ", current_ptr -> ob_prct);
        printf("%3.3f ", current_ptr -> s_avg);
      
        printf(" Would you like to add another player? (y/n): ");
        scanf("%s", answer);
      
        // Ask user if they want to add another employee
        if ((value = toupper(answer[0])) != 'Y')
        {
            current_ptr->next = (struct Baseball *) NULL;
            more_data = 0;
          
        } // if
      
        else
        {
            // set the next pointer of the current node to point to the new node
            current_ptr->next = (struct Baseball *) malloc (sizeof(struct Baseball));
          
            // move the current node pointer to the new node
            current_ptr = current_ptr->next;
          
        } // else
      
      
    } // while
  
}

double total_bases (struct Baseball * data_ptr)
{
    double total;
  
    total = (data_ptr -> sngl + (2 * data_ptr -> dbl) + (3 * data_ptr -> trpl) + (4 * data_ptr -> hr));
  
    return (total);
  
}

double batting_avg (struct Baseball * data_ptr)
{
    double avg;
  
    avg = (data_ptr -> sngl + data_ptr -> dbl + data_ptr -> trpl + data_ptr -> hr)/data_ptr -> ab;
  
    return (avg);
  
}

double home_run_ratio (struct Baseball * data_ptr)
{
    double avg;
  
    avg = data_ptr->hr/data_ptr->ab;
  
    return (avg);
  
}

double strike_out_ratio (struct Baseball * data_ptr)
{
    double avg;

    avg = data_ptr->so/data_ptr->ab;
  
    return (avg);
  
}

double on_base_percentage (struct Baseball * data_ptr)
{
    double ob_pct;
  
    ob_pct = ((data_ptr->sngl + data_ptr->dbl + data_ptr->trpl + data_ptr->hr) + data_ptr->bb + data_ptr->hp)/(data_ptr->ab + data_ptr->bb + data_ptr->hp + data_ptr->sf);
  
    return (ob_pct);
  
}

double slugging_avg (struct Baseball * data_ptr)
{
    double avg;
  
    avg = (data_ptr -> sngl + (2 * data_ptr -> dbl) + (3 * data_ptr -> trpl) + (4 * data_ptr -> hr))/data_ptr->ab;
  
    return (avg);
  
}

void calc_data (struct Baseball * data_ptr)
{
    data_ptr->t_bases = total_bases (data_ptr);
    data_ptr->b_avg = batting_avg (data_ptr);
    data_ptr->hr_ratio = home_run_ratio (data_ptr);
    data_ptr->so_ratio = strike_out_ratio (data_ptr);
    data_ptr->ob_prct = on_base_percentage (data_ptr);
    data_ptr->s_avg = slugging_avg (data_ptr);
  
}


void get_data (struct Baseball * data_ptr)
{
    // Prompt for Player Name
    printf(" Enter player first & last name: ");
    scanf("%s" "%s", data_ptr -> first_name, data_ptr -> last_name);
  
    // Prompt for Number of Walks
    printf(" Enter Number of Walks: ");
    scanf("%lf", & data_ptr -> bb);
  
    // Prompt for Strike Outs
    printf(" Enter Strike Outs: ");
    scanf("%lf", & data_ptr -> so);
  
    // Prompt for Hit by Pitch
    printf(" Enter Hit by Pitch: ");
    scanf("%lf", & data_ptr -> hp);
  
    // Prompt for Sac Flys
    printf(" Enter Sac Flys: ");
    scanf("%lf", & data_ptr -> sf);
  
    // Prompt for Singles
    printf(" Enter Singles: ");
    scanf("%lf", & data_ptr -> sngl);
  
    // Prompt for Doubles
    printf(" Enter Doubles: ");
    scanf("%lf", & data_ptr -> dbl);
  
    // Prompt for Triples
    printf(" Enter Triples: ");
    scanf("%lf", & data_ptr -> trpl);
  
    // Prompt for Home Runs
    printf(" Enter Home Runs: ");
    scanf("%lf", & data_ptr -> hr);
  
    // Prompt for Number of At Bats
    printf(" Enter Number of At Bats: ");
    scanf("%lf", & data_ptr -> ab);
}

void print_data (struct Baseball * data_ptr)
{
    printf(" %s " "%s ", data_ptr -> first_name, data_ptr -> last_name);
    printf("%5.0f ", data_ptr -> bb);
    printf("%5.0f ", data_ptr -> so);
    printf("%5.0f ", data_ptr -> hp);
    printf("%5.0f ", data_ptr -> sf);
    printf("%5.0f ", data_ptr -> sngl);
    printf("%5.0f ", data_ptr -> dbl);
    printf("%5.0f ", data_ptr -> trpl);
    printf("%5.0f ", data_ptr -> hr);
    printf("%5.0f ", data_ptr -> ab);
    printf("%5.0f ", data_ptr ->t_bases);
    printf("%3.3f ", data_ptr -> b_avg);
    printf("%3.3f ", data_ptr -> hr_ratio);
    printf("%3.3f ", data_ptr -> so_ratio);
    printf("%3.3f ", data_ptr -> ob_prct);
    printf("%3.3f ", data_ptr -> s_avg);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote