Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7609545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:11:50+00:00 2026-05-31T01:11:50+00:00

A text file holds information about a softball team. Each line has data arranged

  • 0

A text file holds information about a softball team. Each line has data arranged as follows:

4 Jessie Joybat 5 2 1 1

The first item is the player’s number, conveniently in the range 0–18. The second item is the player’s first name, and the third is the player’s last name. Each name is a single word. The next item is the player’s official times at bat, followed by the number of hits, walks, and runs batted in (RBIs). The file may contain data for more than one game, so the same player may have more than one line of data, and there may be data for other players between those lines. Write a program that stores the data into an array of structures. The structure should have members to represent the first and last names, the at bats, hits, walks, and RBIs (runs batted in), and the batting average (to be calculated later). You can use the player number as an array index. The program should read to end-of-file, and it should keep cumulative totals for each player.

The world of baseball statistics is an involved one. For example, a walk or reaching base on an error doesn’t count as an at-bat but could possibly produce an RBI. But all this program has to do is read and process the data file, as described next, without worrying about how realistic the data is.

The simplest way for the program to proceed is to initialize the structure contents to zeros, read the file data into temporary variables, and then add them to the contents of the corresponding structure. After the program has finished reading the file, it should then calculate the batting average for each player and store it in the corresponding structure member. The batting average is calculated by dividing the cumulative number of hits for a player by the cumulative number of at-bats; it should be a floating-point calculation. The program should then display the cumulative data for each player along with a line showing the combined statistics for the entire team.


team.txt (text file I’m working with):

4 Jessie Joybat 5 2 1 1
4 Jessie Joybat 7 3 5 3
7 Jack Donner 6 3 1 2
11 Martin Garder 4 3 2 1
15 Jaime Curtis 7 4 1 2
2 Curtis Michel 3 2 2 3
9 Gillan Morthim 9 6 6 7
12 Brett Tyler 8 7 4 3
8 Hans Gunner 7 7 2 3
14 Jessie James 11 2 3 4 
12 Brett Tyler 4 3 1 3

Since I’m a beginner in C, either I misinterpreted the task from what was asked originally or it’s unfairly complex (I believe the former is the case). I’m so lost that I can’t think of the way how could I fill in by the criteria of index (player number) every piece of data, keep track of whether he has more than one game, calculate and fetch bat average and then print.

What I have so far is:

#define LGT 30
struct profile {
     int pl_num;
     char name[LGT];
     char lname[LGT];
     int atbat[LGT/3];
     int hits[LGT/3];
     int walks[LGT/3];
     int runs[LGT/3];
     float batavg;
};

//It's wrong obviously but it's a starting point
int main(void)
{
    FILE *flx;
    int i,jc,flow=0;
    struct profile stat[LGT]={{0}};

    if((flx=fopen("team.txt","r"))==NULL) {
        fprintf(stderr,"Can't read file team!\n");
        exit(1);      
    }

    for( jc = 0; jc < 11;  jc++) {
        fscanf(flx,"%d",&i);
        stat[i].pl_num=i;

        fscanf(flx,"%s",&stat[i].name);
        fscanf(flx,"%s",&stat[i].lname);

        fscanf(flx,"%d",&stat[i].atbat[flow]);
        fscanf(flx,"%d",&stat[i].hits[flow]);
        fscanf(flx,"%d",&stat[i].walks[flow]);
        fscanf(flx,"%d",&stat[i].runs[flow]);
        flow++;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:11:52+00:00Added an answer on May 31, 2026 at 1:11 am

    Advice 1: don’t declare arrays like atbat[LGT/3].

    Advice 2: Instead of multiple fscanf you could read the whole line in a shot.

    Advice 3: Since the number of players is limited and the player number has a good range (0-18), using that player number as an index into the struct array is a good idea.

    Advice 4: Since you need cumulative data for each player (no need to store his history points), then you don’t need arrays of integers, just an integer to represent the total.

    So:

    #include <stdio.h>
    
    #define PLAYERS_NO  19
    
    typedef struct  
    {
         char name[20+1];
         char lastName[25+1];
         int atbat;
         int hits;
         int walks;
         int runs;
         float batavg;
    } Profile;
    
    
    
    int main(int argc, char** argv)
    {
        Profile stats[PLAYERS_NO];
        int i;
        FILE* dataFile;
    
        int playerNo;
        Profile tmpProfile;
        int games = 0;
    
    
        for(i=0; i<PLAYERS_NO; ++i)
        {
            stats[i].name[0] = '\0';
            stats[i].lastName[0] = '\0';
            stats[i].atbat = 0;
            stats[i].hits = 0;
            stats[i].walks = 0;
            stats[i].runs = 0;
        }
    
        dataFile = fopen("team.txt", "r");
        if ( dataFile == NULL )
        {
            fprintf(stderr, "Can't read file team!\n");
            exit(1);    
         }
    
         for(i=0; i<PLAYERS_NO && !feof(dataFile); ++i, ++games)
         {
            fscanf(dataFile, "%d", &playerNo);
            if ( playerNo <0 || playerNo > PLAYERS_NO )
            {
               fprintf(stderr, "Player number out of range\n");
               continue;
            }
    
            fscanf(dataFile, "%s %s %d %d %d %d", 
                &tmpProfile.name,
                &tmpProfile.lastName,
                &tmpProfile.atbat,
                &tmpProfile.hits,
                &tmpProfile.walks,
                &tmpProfile.runs);
    
            printf("READ: %d %s %s %d %d %d %d\n", 
                playerNo,
                tmpProfile.name,
                tmpProfile.lastName,
                tmpProfile.atbat,
                tmpProfile.hits,
                tmpProfile.walks,
                tmpProfile.runs);
    
    
            strcpy(stats[playerNo].name, tmpProfile.name);
            strcpy(stats[playerNo].lastName, tmpProfile.lastName);
    
            stats[playerNo].atbat += tmpProfile.atbat;
            stats[playerNo].hits += tmpProfile.hits;
            stats[playerNo].walks += tmpProfile.walks;
            stats[playerNo].runs += tmpProfile.runs;
        }
    
        /* exercise: compute the average */
        fclose(dataFile);
    
        for(i=0; i<PLAYERS_NO; ++i)
        {
            if ( stats[i].name[0] == '\0' )
                continue;
    
            printf("%d %s %s %d %d %d %d\n",
                i,
                stats[i].name,
                stats[i].lastName,
                stats[i].atbat,
                stats[i].hits,
                stats[i].walks,
                stats[i].runs);
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file with x amount of lines. each line holds a
I have a text file where each odd line holds an integer number (String
I'm trying to read a text file line by line, and add each line
Hi I have a bunch of data Im writing to a text file, each
The text file has hundreds of these entries (format is MT940 bank statement) {1:F01AHHBCH110XXX0000000000}{2:I940X
I've got a text file full of records where each field in each record
I have a text file where I want to change only the first line
I have a text file of URLs, about 14000. Below is a couple of
I have a CSV file that holds about 200,000 - 300,000 records. Most of
I have a file txt file that holds 3 values each seperated by a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.