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.
p2 -t teamFileName -g gameFileName
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
o For szContactName, it contains spaces and is terminated by
a new line character. You will have to use a bracket format code
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):
- 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.
- Print the current team information before processing any games using the new printTeams function.
- Print a heading showing Team 1, Team 2, Score 1, and Score 2.
- 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.
- 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.
- 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.
- 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:
- 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);
- 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?
- 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.)
- Modify getTeams to receive the array of teams and return the number of teams.
- Change your documentation to include:
- Change your documentation to include:
O Team teamM array of teams
Count of the number of teams
- 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)
- 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)
- 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)
- 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)
- exitUsage should show what switches it now needs.
- 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:
- 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):
- 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:
If you are in the main CS lab, instead you can copy text to the clipboard and then paste it.
gcc -g -o p2 p2abc123.c
./p2 -t p2Team.txt -g p2Game.txt
- 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 firstname.lastname@example.org
COM001 Comm Eagles 7 1 150.00 75.00
Mae King (210)555-2222 email@example.com
SOUTH1 Slam Dunk 5 3 120.00 75.00
Jerry Tall (210)555-3333 firstname.lastname@example.org
ALHGHT Cake Eaters 4 4 175.00 100.00
E Z Street (210)555-6666 email@example.com
UNKN01 Org New Blk 1 7 150.00 50.00
Bob Wire (210)555-1234 firstname.lastname@example.org
NEWB01 River Rats 0 8 120.00 75.00
Rock D Boat (210)555-4444 email@example.com
UNKN02 Hackers 3 5 150.00 75.00
Tom E Gunn (210)555-5555 firstname.lastname@example.org
UTSA01 NEWB01 55 12
COMM01 SOUTH1 17 15 *** team (COMM01) not found
SOUTH1 ALHGHT 66 3
UTSA01 SOUTH1 44 45