
Rubayet A.
asked 05/18/21computer science
Define a structure named Gamer which will have the following elements: Number_of_ favorite_games (int), List_of_favorite_games (2D string). Now declare a structure array of Gamer for 5 gamers and take inputs for them. Now generate a rank list of the games. (Hint: The game which appeared most in the favorite games list will be the top game. In case of tie, print the game which comes alphabetically before)
1 Expert Answer

Patrick B. answered 05/18/21
Math and computer tutor/teacher
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#define GAME_FILENAME ("E:\\games.dat")
#define GAME_NAME_LENGTH (55)
typedef struct Gamer
{
char ** List_of_favorite_games;
int Number_of_favorite_games;
} _TGamer;
typedef _TGamer* TGamer;
#define GAMER_SIZE (sizeof(_TGamer))
typedef struct _TGameFrequency
{
int freq;
int iIndexPos;
} * TGameFrequency;
#define GAME_FREQ_SIZE (sizeof(struct _TGameFrequency))
int CompareGameFreq(const void * rec1, const void * rec2)
{
TGameFrequency gameFreqRecPtr1 = (TGameFrequency)rec1;
TGameFrequency gameFreqRecPtr2 = (TGameFrequency)rec2;
int iReturn = gameFreqRecPtr1->freq - gameFreqRecPtr2->freq;
if (iReturn==0)
{
iReturn = gameFreqRecPtr1->iIndexPos-gameFreqRecPtr2->iIndexPos;
}
return(iReturn);
}
//reads game list from file
void ReadGameFile(TGamer gameList)
{
FILE * fptr = fopen(GAME_FILENAME,"r");
int N,iLoop;
char buff[55];
if (fptr!=NULL)
{
fscanf(fptr,"%d",&gameList->Number_of_favorite_games);
N = gameList->Number_of_favorite_games;
fgets(buff,55,fptr); //eats the newline
gameList->List_of_favorite_games = (char**)malloc(N*sizeof(char*));
for (iLoop=0; iLoop<N; iLoop++)
{
gameList->List_of_favorite_games[iLoop]=(char*)malloc(GAME_NAME_LENGTH);
fgets(gameList->List_of_favorite_games[iLoop],GAME_NAME_LENGTH,fptr);
int strLen = strlen(gameList->List_of_favorite_games[iLoop]);
gameList->List_of_favorite_games[iLoop][strLen-1]=0; //replaces the new line with null
printf(" incoming game >%s< \n",gameList->List_of_favorite_games[iLoop]);
}
fclose(fptr);
}
else
{
gameList->Number_of_favorite_games=-1;
}
}
//frees the memory
void GameList_Destroy(TGamer gameList)
{
int iLoop;
for (iLoop=0; iLoop<gameList->Number_of_favorite_games; iLoop++)
{
if (gameList->List_of_favorite_games[iLoop]!=NULL)
{
free(gameList->List_of_favorite_games[iLoop]);
}
}
free(gameList->List_of_favorite_games);
gameList->Number_of_favorite_games=0;
}
//returns the ZER0 based index of the game selected by the user
int GameList_Menu(TGamer gameList)
{
int iLoop;
int gameNum=-1;
int N = gameList->Number_of_favorite_games;
while (gameNum<0 || gameNum>N)
{
for (iLoop=0; iLoop<N; iLoop++)
{
printf(" %d %s \n",(iLoop+1),gameList->List_of_favorite_games[iLoop]);
if ((iLoop+1)%30==0) { printf("\n press key for more games... "); getch(); printf("\n"); }
}
printf(" Input your selection :>");
scanf("%d",&gameNum);
}
return(gameNum-1);
}
int AskAnother()
{
int iReturn=-1;
while (iReturn!=0 && iReturn!=1)
{
printf(" Would you like another game??? 1=YES 0=NO ");
scanf("%d",&iReturn);
}
return(iReturn);
}
//creates subset "favoriteGames" of the gameList, per user selection; updates the frequencies
void BuildGameList(TGamer gameList,TGamer favoriteGames,int * gameFreq)
{
int gameNum;
int iIndexPos=0;
int iLoop;
int done_flag=0;
favoriteGames->Number_of_favorite_games=0;
//gets # of games in the game list and clears the boolean flags to "unmarked";
int N = gameList->Number_of_favorite_games;
int * boolFlags = (int*)malloc(N*sizeof(int));
memset(boolFlags,0,N*sizeof(int));
printf(" # of games in gamelist = %d \n",N);
do
{
gameNum = GameList_Menu(gameList);
boolFlags[gameNum]=1;
favoriteGames->Number_of_favorite_games++;
done_flag = (AskAnother()==0);
}
while (done_flag==0);
favoriteGames->List_of_favorite_games = (char**)malloc(favoriteGames->Number_of_favorite_games*sizeof(char*));
iIndexPos=0;
for (iLoop=0; iLoop<N; iLoop++)
{
if (boolFlags[iLoop])
{
favoriteGames->List_of_favorite_games[iIndexPos] = (char*)malloc(GAME_NAME_LENGTH);
strcpy(favoriteGames->List_of_favorite_games[iIndexPos],gameList->List_of_favorite_games[iLoop]);
iIndexPos++;
gameFreq[iLoop]++;
}
}
free(boolFlags);
}
int MainMenu()
{
int iReturn=-1;
while (iReturn<0 || iReturn>2)
{
printf("**********************************************\n");
printf(" <1> INPUT ANOTHER SURVEY BALLOT \n");
printf(" <2> SHOW CURRENT STATS \n");
printf("**********************************************\n");
printf(" Please input your selection or ZER0 to QUIT :>");
scanf("%d",&iReturn);
}
return(iReturn);
}
void GameList_Dump(TGamer gameList, char * strMsg)
{
int iLoop;
if (strMsg!=NULL)
{
printf("********************************************");
printf(strMsg);
printf("\n");
}
printf("********************************************");
for (iLoop=0; iLoop<gameList->Number_of_favorite_games; iLoop++)
{
printf(gameList->List_of_favorite_games[iLoop]);
printf("\n");
}
}
GameFreqReport(TGamer gameList,int * freq, int nMax)
{
int iLoop;
int N = gameList->Number_of_favorite_games;
TGameFrequency gameFreq = (TGameFrequency) malloc(GAME_FREQ_SIZE*N);
for (iLoop=0; iLoop<N; iLoop++)
{
gameFreq[iLoop].iIndexPos=iLoop;
gameFreq[iLoop].freq = freq[iLoop];
}
qsort(gameFreq,N,GAME_FREQ_SIZE,CompareGameFreq);
if (nMax>N) { nMax=N; }
for (iLoop=N-1; iLoop>=nMax; iLoop--)
{
_TGameFrequency curGameFreqRec = gameFreq[iLoop];
printf(" %s %d \n",gameList->List_of_favorite_games[curGameFreqRec.iIndexPos],curGameFreqRec.freq);
}
}
main()
{
_TGamer gameList;
_TGamer myFavoriteGames;
int iLoop;
//reads the game list from file and intializes the frequencies to zero
ReadGameFile(&gameList);
int * gameFreq = (int*)malloc(gameList.Number_of_favorite_games*sizeof(int));
memset(gameFreq,0,gameList.Number_of_favorite_games*sizeof(int));
int iMenuChoice=-1;
while (iMenuChoice!=0)
{
iMenuChoice = MainMenu();
switch (iMenuChoice)
{
case 1:
{
BuildGameList(&gameList,&myFavoriteGames,gameFreq);
GameList_Dump(&myFavoriteGames,(char*)"GAMES SELECTED");
GameList_Destroy(&myFavoriteGames);
break;
}
case 2:
{
GameFreqReport(&gameList,gameFreq,20);
break;
}
case 0:
{
break;
}
} //switch
} //while
free(gameFreq);
GameList_Destroy(&myFavoriteGames);
GameList_Destroy(&gameList);
}
> cat Games.dat
5
Berzerk
Crazy Climber
Froger
Jungle King
Tron
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
The names of each game must be consistent or else it will throw off the frequency counts. I suggest using a numbering system instead and selecting from a menu. For example... The original Pac-Man and pac-man are technically the same game, but shall be treated as 2 different entries in the frequency table.05/18/21