Arrays (40 points) Solution

$30.00

Description

This is a continuation of program #1. This program demonstrates the use of arrays, functions, and parameters.

In this program, you will read the team data into an array, print the contents of the array before processing game data, read and process game data, and print the contents of the array after that processing. Copy your p1abc123.c to p2abc123.c so that you can modify it.

Command:

p2 -t teamFileName -g gameFileName

Input:

Team Data (same format as in program #1):

Stream input file which contains many teams. There are two different kinds of lines of data for each team:

– Team Identification Information:

o One line per team (separated by spaces)

szTeamId iWins iLosses dFeeAmount dPaidAmount

6s d d lf lf

– Team Contact Information:

o One line per team (separated by commas)

szTeamNm szEmail szPhone szContactName

12s 30s 13s 20s

o Although szTeamNm is a maximum of 12 characters, it may

contain spaces; therefore, you cannot simply use %12s.

For szFullName, you will have to use a bracket format

code using %12[^,]. The next two values are also terminated by

commas.

o For szContactName, it contains spaces and is terminated by

a new line character. You will have to use a bracket format code

using %20[^\n]

Game Data:

Stream input file which contains many games:

o Only one type of data record (spearated by spaces):

szTeamId1 szTeamId2 iScore1 iScore2

6s 6s d d

Files Larry provided:

cs1713p2_h.txt include file for this program; rename this to cs1713p2.h

p2Team.txt input file containing team information

p2Game.txt input file containing game information

cs1713p2Provided.txt code for printTeams which you must copy into your new p2abc123.c file.

Process (see the details of the code changes later in this document):

  1. Read the team information into an array using a modified getTeams function which is passed an array of teams and returns the count of the teams. Most of this was done in program1; however, you must place the information into that array.
  2. Print the current team information before processing any games using the new printTeams function.
  3. Print a heading showing Team 1, Team 2, Score 1, and Score 2.
  4. Invoke a new processGameFile function which is passed the array of teams and the count of teams. Process the games data by reading the p2Game.txt file until EOF. For each game
  • Read a line of game data.
  • Invoke your processGame function passing that one game, the array of teams, and the count of teams.
  1. Print the team array after all of the games have been processed using the printTeams function. You should notice changes in the number of losses and the number of wins.

processGame function:

  • Print the game information.
  • Lookup team 1 in the team array using your findTeam function. If not found, print a warning (e.g., team (UTSA01) not found) and return.
  • Lookup team 2 in the team array using your findTeam function. If not found, print a warning (e.g., team (XYZ001) not found) and return.
  • If the two teams are the same team, print a warning (e.g., same team) and return.
  • If the scores are the same, print a warning (e.g., game was a tie) and return.
  • Adjust the number of wins and losses for each of the teams based on which score is higher.

Error Processing:

  • There are some expected warnings (e.g., team ID not found, game was a tie) which should be printed to stdout and not cause execution to terminate.
  • Errors such as bad filename and incorrectly formatted input file should cause the program to be exited (use exitError) as was done in Program #1. Note that it is unlikely this will happen for our data file.

Modifications of Program#1:

  1. Above the main function:
  • change from including “cs1713p1.h” to “cs1713p2.h”
  • add this declaration:

FILE *pFileGame; // stream input for games

  • change the prototype declaration for processCommandSwitches to

void processCommandSwitches(int argc, char *argv[]

, char **ppszTeamFileName

, char **ppszGameFileName);

  1. Modify the main function as necessary:
  • Declare Team teamM[MAX_TEAMS];
  • Declare a variable for the count of the number of entries in the teamM array.
  • Declare a variable for the game file name:

char *pszGameFileName = NULL;

  • Just like the code for opening the Team file, add code to check the Game file name and open the Game file.
  • Change the call to getTeams to pass the teamM array and assign the function results into your variable for the count of teams.
  • Print the original contents of the team array before processing the game data by calling printTeams passing the team array and the count of teams.
  • Invoke processGameFile, passing the teamM array and the count of the number of entries in that array
  • Print the resulting contents of the team array using the printTeams function.
  • Anything else?
  1. processCommandSwitches needs to be modified to return multiple filenames (one for the team file and one for the game file). (See the prototype declaration above.)
  2. Modify getTeams to receive the array of teams and return the number of teams.
    • Change your documentation to include:
      Parameters:

O Team teamM[] array of teams

Returns:
Count of the number of team
s

  • Since you already have code to read the data into a simple structure named team, simply copy it into the array:
    teamM[i] = team; // You also need to declare i at the beginning of this function
  • Return the count of teams as its functional value.
  • The prototype for getTeams is now:

int getTeams(Team teamM[])

  1. Add a new function processGameFile which is passed an array of teams and team count as parameters. It should read data from the game file until EOF. For each game read, it should invoke processGame passing the game, array of teams, and team count. Its prototype is

void processGameFile(Team teamM[], int iTeamCnt)

  1. Add a new function processGame which processes a game as described above. It is passed a game, the array of teams, and the team count. Its prototype is
    void processGame(Game game, Team teamM[], int iTeamCnt)
  2. Add a new function findTeam which finds the specified Team Id in the teamM array and returns its subscript. If not found, it should return -1.

int findTeam(Team teamM[], int iTeamCount, char szTeamId[])

  1. exitUsage should show what switches it now needs.
  2. Copy the text from cs1713p2Provided.txt into your p2abc123.c file. See how to do that below.

Copying text from one file into another

For this assignment, you will need to copy the text from cs1713p2Provided.txt into your p2abc123.c file. There are many ways to do that. I will mention one that allows you to select particular text lines and then paste them into your file.

  • Edit both files

$ vi cs1713p2Provided.txt p2abc123.c

  • Yank the text you want into vi buffer “a. Since you want the entire contents:

:1,$y a

  • What that meant:

: get into command mode

1,$ tells it to use lines 1 to the last line

y yank those lines

a place in the buffer named a

  • Now, continue with editing the next file (which is your p2abc123.c):

:n

  • Inside that file, you may want to go to the bottom of the file:

:$

  • Paste the contents of the a buffer where you want it:

“ap

If you are in the main CS lab, instead you can copy text to the clipboard and then paste it.

Compiling and creating an executable named “p2”

gcc -g -o p2 p2abc123.c

Executing

./p2 -t p2Team.txt -g p2Game.txt

Grading

  • Your program must be written according to my programming standards.
  • Your program should use the provided include file “cs1713p2.h”.
  • You must turn in your .c file and your program output which is based on the input file that I provide.
  • Code which is mostly not working will receive less than 50% credit.

Sample Output (Partial):

Original Team Information

Id Team Name Wins Loss Fee Amt Paid Amt

Contact Name Phone Email

UTSA01 Armadillos 8 0 150.00 80.00

Jean E Us (210)555-1111 utsa@xyz.com

COM001 Comm Eagles 7 1 150.00 75.00

Mae King (210)555-2222 maeking@xyz.com

SOUTH1 Slam Dunk 5 3 120.00 75.00

Jerry Tall (210)555-3333 slamdunk@gmail.com

ALHGHT Cake Eaters 4 4 175.00 100.00

E Z Street (210)555-6666 sliverspoon@xyz.com

UNKN01 Org New Blk 1 7 150.00 50.00

Bob Wire (210)555-1234 bobwire@xyz.com

NEWB01 River Rats 0 8 120.00 75.00

Rock D Boat (210)555-4444 riverrat@xyz.com

UNKN02 Hackers 3 5 150.00 75.00

Tom E Gunn (210)555-5555 cyber@gmail.com

Processing Games

UTSA01 NEWB01 55 12

COMM01 SOUTH1 17 15 *** team (COMM01) not found

SOUTH1 ALHGHT 66 3

UTSA01 SOUTH1 44 45