Bowling 7 c++

User Generated

zvxr_zvxr123

Programming

Description

Bowling lab based on the pdf ive shared below but you cannot use victors.

Beginner program


Bowling7.cpp


// Header files for I/O and string

#include <iostream>

#include <iomanip>

#include <fstream>

#include <string>

using namespace std;

// declaration of functions sumArray() and printArray()

// declaration of main program

int main()

{

// 1) connect to the input file

ifstream fin("bowling.txt");

// declare arrays below

string Team, Member;

int Score;

// 2) initialize array accumulators to zero

// 3) display a descriptive message

cout << "This program reads the lines from the file bowling.txt to determine\n"

<< "the winner of a bowling match.The winning team, members and scores\n"

<< "are displayed on the monitor.\n";

// 4) attempt to input the first line of the file

fin >> Member >> Team >> Score;

// 5) test ifstream.eof() condition

while (!fin.eof())

{

// 6) test team color is blue

// 7) then store blue member and score

// 8) increase blue array accumulator

// 9) else store white member and score

// 10) increase white array accumulator

// 11) attempt to input next line from file

fin >> Member >> Team >> Score;

}

// 12) if blue team score is larger

// 13 then display blue team as winner with the team

// 14) else display white team as winner with the team

}

// implement function sumArray() below

/* 1. initialize accumulator to 0

2. loop over initialized array indices

3. increase accumulator by indexed array element

4. return accumulator

*/

// implement function printArray() below

/* 1. displaythe team name as the winner

2. loop over initialized array indices

3. display member and score for that array index

*/


Unformatted Attachment Preview

Fred Blue 20 Harry Blue 35 Tony White 43 Hilda Blue 12 Paul White 34 Tom White 20 Parallel Array Processing Quick Start once always mkdir labs cd labs mkdir 7 cd 7 cp /samples/csc/155/labs/7/* . emacs Bowling7.cpp & Compile step make -k bowling7 Execute step bowling7 Submit step submit csc155abc 7 Problem Statement and Specifications This assignment is designed to introduce you to some of the aspects of data base management systems. Traditional data bases have some elementary operations that are implemented to meet user’s needs appropriately. First of all, records must be added to the data base. This will be part of our input operation. As we read the data in from an input file, we’ll be adding it to storage for later reference. Secondly, we’ll need to search the data base for those data values that are of interest to us. This could represent a search for a particular data record or it could be a search whereby we generate a subset of the data all with data subfields equal to some value. Finally, we will need to delete records or modify records in a data base. Our current exercise will provide us with the opportunity to demonstrate some of these features of data base management. Assignment 7 Statement: Write a program in a file named Bowling7.cpp that uses parallel arrays to store information that is on each line of the input file bowling.txt. Each line of that file contains a bowling team member’s first name (string), one of two bowling team names (string data equal to either "Blue" or "White"), and that bowling team member’s score in a bowling match (int). Each line of the file contains a unique bowling team member so that you do not need to worry about repeated team member’s names or embedded blanks within the field. Also, each team is guaranteed to have the same number of team members. Your program should use a function to sum the scores of each of the two teams. Once you determine the maximum score, your program should display on the monitor a message as to which team won and then use a function to display each of the winning team member’s name and score. The parallel array strategy implies that you will need 2 arrays of type string for the team member’s name and 2 arrays of type int for the team member’s score. 4∇4 1/2 lab extra credit 4∇4 Copy your solution to a file named Bowling77.cpp and further modify the program to produce identical output to Bowling7.cpp but use 2-dimensional arrays of type string and int to process your data. Choose one row of the array to represent the 1 CSC 155 - Assignment 7 Bowling White team and the other row of the same array to represent the Blue team. Passing a row of a multi-dimensional array to a function that expects a 1-dimensional array of the same data type will allow you to reuse the functions that you wrote for this exercise. Use the commands make -k bowling77, bowling77 and submit csc155abc 77 to compile, execute and submit your assignment, respectively. We’ll use a strategy whereby after we input each line of data from the input file, a selection statement applied to the Team will allow us to determine which pair of the arrays to store the Member and Score fields in. This will allow us to insert them in sequential order of storage as they appear in the input file. In other words, one of the string arrays will contain all of the Members of the White team as they appear in the input file and the other string array will contain all of the Members of the Blue team. Similarly, the appropriate int array will contain all of the Scores of the White team as they appear in the input file and the other int array will contain all of the Scores of the Blue team as they appear in the input file. Consequently, we can use the same index into either of the White team arrays to access the Member or the Score for a particular bowler. Similarly, the same index can be used to access fields from the Blue arrays for members of those team. This is the basis of using parallel arrays to process data. Assignment 7 Specifications: Process connect to the input file bowling.txt Input read a bowling Member, Team, and Score from each line Process if the Team is "Blue" add the Member to the end of the blue members array and add the Score to the end of the blue scores array else add the Member to the end of the white members array and add the Score to the end of the white scores array Output the team with the largest value of team scores sum Output that team’s Member and Score for the entire team Object Analysis and Algorithmic Development As is always the case when using functions, much of the main() function will simply involve the invocation of the other functions. Let’s start our analysis with the required functions for this code. Since the function to sum the bowling scores for an array does not necessarily use the entire array (we have no knowledge as to how many of the array elements have been initialized based upon the values in the file bowling.txt) we’ll need to pass the length of the initialized array elements with the array as a parameter. Arrays are naturally reference parameters if we pass the name of the array as an argument. The number of initialized elements of the array should be a value parameter. The function sumArray() will simply perform a List Reduction Algorithm by summing the elements of the array. Note that lines that are displayed in blue represent areas of code that must be written or modified for you to complete the assignment. Pseudo Code for Function sumArray() Statement Data objects 1) initialize accumulator to 0 int variable 2) loop over initialized array indices int variables, parameters and constants 3) increase accumulator by indexed array element int variables and array parameter 4) return accumulator int variable In addition, we are required to code a function to display a team and their member’s scores. The function printArray() will simply loop over the initialized array elements and display this formatted information on the monitor. Since we know the Team as we need to know which arrays to send as parameters, we’ll include that string variable as a parameter as well. 2 CSC 155 - Assignment 7 Bowling Pseudo Code for Function printArray() Statement Data objects 1) display the Team as the winner string parameter & constants 2) loop over initialized array indices int variables, parameters and constants 3) display Member & Score for int variables and int & string array parameters that index As we indicated earlier, our main() function hides much of the details that are buried in the implementation of the invoked functions. Pseudo Code for Function main() Statement Data objects 1) connect the input file object to bowling.txt ifstream objects and string constants 2) initialize array accumulators to 0 int objects 3) display a descriptive message string constants 4) attempt to input the first line from the file ifstream, string & int objects 5) test the ifstream.eof() condition bool function and ifstream object 6a) test Team is "Blue" string variable and constant 7) then store Member & Score in "Blue" arrays string & int variables and arrays 8) increase "Blue" array accumulator int variable 6b) else 9) store Member & Score in "White" arrays string & int variables and arrays 10) increase "White" array accumulator int variable 11) attempt to input the next line from the file ifstream, string & int objects 12a) if sumArray of "Blue" team > sumArray of "White" team function sumArray() 13) display "Blue" team blue members & blue scores function printArray() 12b) else 14) display "White" team white members & white scores function printArray() 15) disconnect from the input file ifstream method object and close() Just to clarify matters, Step 6 through Step 11 are in the while loop determined at Step 5 . Also, Steps 7-8 are on the true path of the selection statement at Step 6 and Steps 9-10 represent the false path of the statement. Coding for Compilation Logon to your csc.oakton.edu account and create a new subdirectory of the labs directory called 7 to work on your assignment. cd labs mkdir 7 cd 7 The input file bowling.txt and the startup file Bowling7.cpp can be copied over to your file with the following command. cp /samples/csc/155/labs/7/* . Begin by editing the start up file which connects to the input file bowling.txt and contains documentation. 3 CSC 155 - Assignment 7 Bowling emacs Bowling7.cpp & Declaration of Variables Other than the variables for file manipulation, all variables and constants used in this program are either int or string. Of specific interest is the declaration of 1-dimensional arrays of these data types. In general, the format of such an array declaration in C++ appears as follows. data type array name[array length]; Each of your bowling teams is guaranteed to have fewer than 10 members on it, so the following array declarations would be appropriate. string int blue members[10], white members[10]; blue scores[10], white scores[10]; As you store data into your arrays, place the first element into position 0 and store the data into consecutive array elements. Then, if you maintain an int variable for the size of each team, you’ll know how many of the array elements to process in your loops. Functions with Array Parameters As is always the case with functions, we’ll need to know how to code the array parameters in the prototype, the invocation and the implementation. Arrays are naturally reference parameters, which is the most efficient means to pass them. In other words, when changes occur to the array parameter in the function, those changes are reflected back in the main(). Our functions will not make any changes to array parameters in this exercise. In the function prototype, an array parameter is identified using the following syntax. return type function name( data type [] ); Other parameters are listed as usual. In the function invocation, you simply list the array name. In the function implementation, the array parameter is identified using the following syntax. return type function name( data type array name[] ) If you wish for the array to act like a value parameter, place the const keyword in front of the data type in both the function prototype and implementation. That is the case with our array functions, each will not alter the contents of the arrays. For example, in the sumArray() function, we would prototype the array as follows. Note that the second parameter indicates the number of items in the array that have been initialized and will be used by our loops to process the data in the array. int sumArray(int [], int); The invocation of the function is coded by listing the function name followed in parentheses by the array name and the array end where array end is an int that indicates how many of the array elements have been initialized. What follows below is the function implementation which implements a list reduction algorithm to sum the array elements. 4 CSC 155 - Assignment 7 Bowling int sumArray(int array name[], int array end) { int sum = 0; for (int i = 0; i < array end; i++) sum += array name[i]; return sum; } Code this function as well as the function printArray() into the bottom of your Bowling7.cpp file and we’ll address the remainder of the main() program. Follow the Pseudo Code Step 1 // connect the input file object to bowling.txt This has been provided for you in the start up file. We have declared an initialized the fin ifstream variable to serve this purpose. The code is reproduced below. ifstream fin("bowling.txt"); Step 2 // initialize accumulators to 0 The accumulators for this lab are the int variables that tell us how many elements of the arrays have been initialized based upon the data stored in bowling.txt. You’ll need one for each team. Step 3 // display a descriptive message An output statement to the monitor that describes the program’s purpose, as usual. Step 4 // attempt to input the first line from the file An input statement that reads the Member, the Team and the Score from the file bowling.txt. The input string values have no embedded blanks, so we can use our usual input operator, >>. Step 5 // test the end-of-file condition This has been provided for you in the start up file. Use the ifstream variable fin for this purpose. The code is reproduced below. while(!fin.eof()) Step 6 // test if Team is "Blue" To determine the Team, use an if statement. 5 CSC 155 - Assignment 7 Bowling Steps 7-8 // store Member and Score in "Blue" arrays // increase "Blue" accumulator by 1 This represents storage of the record into the "Blue" arrays and preparing us for the next "Blue" item that comes along. Steps 9-10 // store Member and Score in "White" arrays // increase "White" accumulator by 1 This represents storage of the record into the "White" arrays and preparing us for the next "White" item that comes along. Step 11 // attempt to read another input line This is no different than that which was done for Step 4 and should be repeated. Step 12 // if "Blue" scores > "White" scores The function sumArray() can be invoked within the if statement twice to compare both scores. Step 13 // display "Blue" team name, team member and member score This is an invocation of the printArray() function for the "Blue" team. Step 14 // else display "White" team name, team member and member score This is an invocation of the printArray() function for the "White" team. Step 15 // disconnect from the input file This has been provided for you in the start up file and references the initialized the fin ifstream variable. The code is reproduced below. fin.close(); Checkpoint We’re done, at this point, so save your program to disk by choosing the Save command from the Files menu of your emacs session. Then, compile your program by choosing the Compile. . . command from the Tools menu and change the make -k that is displayed to make -k bowling7. Compiler errors can be parsed with the keystroke C-x ‘ and need to be repaired before your program can execute. When your program has compiled, click on your xterm window to access your command line prompt, and issue the command bowling7. Once again, your output should be identical to the sample output . 6 CSC 155 - Assignment 7 Bowling Testing for errors The types of errors that can occur in this program have to do with the input, the updates to the accumulators, or the agreement between the argument and parameter lists of your functions and the passing of arrays as parameters. The most common type of error is misspelling the name of the input file bowling.txt when the ifstream object fin is initialized. You might also want to use the ls command to make sure that bowling.txt is in the directory that you’re working in. If your input file bowling.txt is not in your current directory, issue the following command. cp /samples/csc/155/labs/7/bowling.txt . Additional areas where errors can occur have to do with the inappropriate placement of the input statements or the incorrect updates to the accumulator variables. Make sure that your code follows the algorithm that we’ve discussed to correct any logic errors. Also, the logic associated with the identification of the Team should be examined to ensure that you’re manipulating the team that you think that you are. In addition, the agreement between the function argument list and the function parameter list is critical to the correct execution of your program. Finally, if you see a Segmentation fault, this is typically the error when your array references are beyond the memory declared for their storage. That means that one or both of your accumulators is negative or greater than 9. Check how their values are changing by inserting some output statements so that you can see how they are being manipulated. Printing and submitting Once you are satisfied with the correctness of your program, print it as you did with previous assignments by using the following command that assumes that you are working in the room 1234 at Oakton. Retrieve your copy from the printer. printer 1234 Bowling7.cpp Finally, submit your program with the following command that assumes that you are registered in section abc of CSC 155. submit csc155abc 7 7
Purchase answer to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Explanation & Answer

Here is lab 7. There are no f...

Related Tags