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

Description Code a program for a game store that reads and writes information ab

ID: 3581381 • Letter: D

Question

Description

Code a program for a game store that reads and writes information about sales figures for games in their inventory using binary files.

In this project, I need to write two programs:

Program 2-1 will create a binary file of game sales information

Program 2-2 will read this file, and perform some statistical analysis on the data.

This project builds on and modifies Program 1. ( I have included the code for Program 1 below.)

Program 2-1 – Creating the binary file

The first program for this assignment gets the data from the user and writes it out to a binary file. I need to use what I wrote for Program 1 as a beginning point for this program.

Requirements for Program 2-1

Ask the user for the number of games to enter (maximum of 25).

Then prompt the user to type in the title of each game, the number sold of that game, and a number indicating the game’s genre. All data must be read into a structure. Use the same structure you used in Program 1. You do not have to store each structure in an array, although you can if you want to.

I must use the get_game_info function from Program 1 to read the information for each game from the user. See the instructions for Program 1 for the requirements for this function.

Write the data out to a binary file called gamesales.dat. I could write the entire array out once (after all data is read from the user), or, if I am not using an array, I can write each individual structure out to the file as it is read from the user. It is my choice.

The binary file must ONLY contain game titles, number sold of each game, and the numbers indicating the genre of each game. Do not write anything else to this file, except the data entered by the user.

I don't have to display the data to the screen as I did in Program 1.

Program 2-2 – Reading the binary file & statistical analysis

THIS IS A COMPLETELY SEPARATE PROGRAM!

The second program for this assignment reads the binary file written in Program 2-1. Once all the data is read from the file, this program computes the average sales for all games, and then prints a list of only the games where their number sold is below the average.

Parts of this program may be very similar to what I wrote for Program 1.

Requirements for Program 2-2

I need to read the contents of the binary file into an array of game structures (the same structure and enum you used for Program 1). I may assume there will be no more than 25 games in the file, but my logic must read the file until the end of file.

I have to keep count of the number of games read from the file.

I must use the display_game_info function from Program 1 to display the contents of the games array. See the instructions for Program 1 for the requirements for this function. The output must be neatly formatted and lined up exactly as in the sample run.

I must use a function called compute_average to calculate the average number sold for all games, and print the average to the screen.

This function has two arguments: the array of game structures and the number of games in inventory. The average number sold is the return value from this function.

I must use a function called display_low_sellers to display a list of only those games where the number sold is less than the average.

This function has three input arguments: the array of game structures, the number of games in inventory, and the average computed earlier. It does not have a return value. The display only needs to include the name of the game and the number sold.

Sample Runs

Program 2-1:

Program 2-1 (continued):

Program 2-2:

Program 1 Code:

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

enum GENRE {Action,RPG,Simulation,Strategy,Sports};

struct gameinfo{
char name[100];
int quantity;
enum GENRE sam;

};

void get_game_info(struct gameinfo *current);   

void display_game_info(struct gameinfo gamesinfo[], int n);   


int main()
{
printf("How many games are there in inventory? ");
int n;
scanf("%d",&n);   

struct gameinfo gamesinfo [n];   

int k=0;
while(k<n)
{
get_game_info(&gamesinfo[k]);   

k++;
}
display_game_info(gamesinfo, n);   

return 0;
}

void get_game_info(struct gameinfo *current)
{
printf(" Title of game (maximum of 15 characters): ");
fflush(stdin);
fgets(current->name, 100, stdin);   
int length;
for(length=0; current->name[length]!=' ' ; length++)
{

}
current->name[length]=' ';
if(length>15)   
{
printf("Title will be truncated ");
}


printf("Number Sold: ");   
scanf("%d",&current->quantity);

printf("Genre (0-action, 1-rpg, 2-simulation, 3-strategy, 4-sports): ");
int genrevalue;
scanf("%d",&genrevalue);
while(genrevalue > sizeof(current->sam))   
{
printf("Genre not recognized, please reenter ");
printf("Genre (0-action, 1-rpg, 2-simulation, 3-strategy, 4-sports): ");   
scanf("%d",&genrevalue);
}
current->sam=genrevalue;   


}

void display_game_info(struct gameinfo gamesinfo[], int n)   
{
int i;   

printf(" Title ");   
printf("Quantity Sold ");
printf(" Genre ");
printf("===== ");
printf("============= ");
printf("===== ");


for(i=0;i<n;i++)   
{
printf("%.15s ",gamesinfo[i].name);   
printf("%d ",gamesinfo[i].quantity);
switch(gamesinfo[i].sam)
{
case 0:   
printf("Action");
break;
case 1:                                         
printf("RPG");
break;
case 2:                                      
printf("Simulation");
break;
case 3:   
printf("Strategy");
break;
case 4:   
printf("Sports");
break;
}
printf(" ");   

}
}

How many games are there in inventory (maximum 25) 9 Title of game (maximum of 15 characters): StarCraft Number sold: 41 Genre (0-action, 1-rpg, 2-simulation 3-strategy, 4-sports) 3 Title of game (maximum of 15 characters): Zoo Tycoon Number sold: 32 Genre (0-action, 1-rpg, 2-simulation 3-strategy, 4-sports) 2 Title of game (maximum of 15 characters): FIFA Number sold: 53 Genre (0-action, 1-rpg, 2-simulation 3-strategy, 4-sports): 4 Title of game (maximum of 15 characters): Call of Duty Number sold: 14 Genre (0-action, 1-rpg, 2-simulation 3-strategy, 4-sports): 0 Title of game (maximum of 15 characters): Civilization Number sold: 95 Genre (0-action, 1-rpg, 2-simulation 3-strategy, 4-sports) 3 Title of game (maximum of 15 characters): Grand Theft Auto Title will be truncated Number sold: 76 Genre (0-action, 1-rpg, 2-simulation, 3-strategy, 4-sports): 0

Explanation / Answer

I am adding two .c files. I have made some changes in get_game_info function. Please change it to its original form, if it is causing problem:

NOTE : In case something is missing, please comment here, and I will revert back ASAP

p2_1.c

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

enum GENRE {Action,RPG,Simulation,Strategy,Sports};

struct gameinfo
{                          
    char name[100];      
    int quantity;        
    enum GENRE sam;      

};

void get_game_info(struct gameinfo *current);   

int main()
{
    printf("How many games are there in inventory? ");
    int n;                                            
    scanf("%d",&n);                                   

    struct gameinfo gamesinfo [n];             

    int k=0;  

    //create binary file     
    FILE *myfile = fopen("gamesales.dat","wb");

    while(k<n)                                
    {
        get_game_info(&gamesinfo[k]);         
        //write this game info in binary file
        fwrite(&gamesinfo[k], sizeof(struct gameinfo), 1, myfile);
        k++;                                  
    }
    //close the file
    fclose(myfile);    
    return 0;
}

void get_game_info(struct gameinfo *current)          
{
    printf(" Title of game (maximum of 15 characters): ");
        fflush(stdin);            
        //fgets(current->name, 100, stdin);               
        scanf("%s",current->name);
        int length;
        for(length=0; current->name[length]!='' ; length++)
        {

        }
       if(length>15)                                       
       {
           printf("Title will be truncated ");            
       }


       printf("Number Sold: ");                           
       scanf("%d",&current->quantity);                    

       printf("Genre (0-action, 1-rpg, 2-simulation, 3-strategy, 4-sports): ");
       int genrevalue;
       scanf("%d",&genrevalue);
       while(genrevalue > sizeof(current->sam))           
       {
           printf("Genre not recognized, please reenter ");  
           printf("Genre (0-action, 1-rpg, 2-simulation, 3-strategy, 4-sports): ");   
           scanf("%d",&genrevalue);
       }
      current->sam=genrevalue;                             
}

p2_2.c

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

enum GENRE {Action,RPG,Simulation,Strategy,Sports};

struct gameinfo
{                          
    char name[100];      
    int quantity;        
    enum GENRE sam;      

};

void display_game_info(struct gameinfo gamesinfo[], int n);
double average_sales( struct gameinfo gamesinfo[], int n);
void display_low_sellers( struct gameinfo gamesinfo[], int n, double average);

int main()
{
    //read binary file     
    FILE *myfile = fopen("gamesales.dat","rb");
  
    struct gameinfo gamesinfo[25]; // assumption : wont be more than 25        

    int n=0;


    //fseek(ptr_myfile,sizeof(struct rec)*counter,SEEK_SET);
    while( fread(&gamesinfo[n],sizeof(struct gameinfo),1, myfile) != 0 )
   // while(k<n)                                
    {
        n++;                                  
    }
    //close the file
    fclose(myfile);    
  
    display_game_info( gamesinfo, n);
    printf(" ");
    double average = average_sales( gamesinfo, n );
    printf("Average sales = %f units ", average );
    display_low_sellers( gamesinfo,n, average );
    return 0;
}

void display_game_info(struct gameinfo gamesinfo[], int n)         
{
    int i;                                         

    printf(" Title            ");                 
    printf("Quantity Sold          ");
    printf(" Genre ");
    printf("=====            ");
    printf("=============           ");
    printf("=====          ");


    for(i=0;i<n;i++)                               
    {
        printf("%.15s ",gamesinfo[i].name);     
        printf("%d ",gamesinfo[i].quantity);    
        switch(gamesinfo[i].sam)                  
        {
        case 0:                                     
            printf("Action");
            break;
        case 1:                                       
            printf("RPG");
            break;
        case 2:                                        
            printf("Simulation");
            break;
        case 3:                                     
            printf("Strategy");
            break;
        case 4:                                     
            printf("Sports");
            break;
        }
        printf(" ");                     

    }
}

double average_sales( struct gameinfo gamesinfo[], int n){
double average = 0.0;
int i;
for(i = 0; i < n; i++){
    average = average + gamesinfo[i].quantity;
}
average = average/n;
return average;
}

void display_low_sellers( struct gameinfo gamesinfo[], int n, double average){
printf("Low Selling games in Inventory ");
int i;
for( i = 0; i < n; i++){
    if( gamesinfo[i].quantity < average ){
      printf("%s %d ", gamesinfo[i].name, gamesinfo[i].quantity );
    }
}
printf(" ");
}