|
|
|
#1 (permalink) |
|
New to Overclock.net
|
i was wondering if you guys could help me with an assignment for class i started it but now im completely lost as to what to do
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#define NUM_PLAY 30
using namespace std;
int buildArrays(int[], int[], int[]);
void printArrays(string[], int[], int[], int[], int);
void sortArrays(string[], int[], int[], int[], int);
int main()
{
int numPlayers = 0;
int goals[NUM_PLAY];
int assists[NUM_PLAY];
int shots[NUM_PLAY];
string temp;
string players[NUM_PLAY] = {"Martin Havlat", "Patrick Kane", "Jonathan Toews",
"Kris Versteeg", "Brian Campbell", "Andrew Ladd",
"Dave Bolland", "Patrick Sharp", "Duncan Keith",
"Cam Barker", "Dustin Byfuglien", "Brent Seabrook",
"Troy Brouwer", "Sammy Pahlsson", "Colin Fraser",
"Ben Eager", "Matt Walker", "Adam Burish",
"Aaron Johnson", "Niklas Hjalmarsson", "Brent Sopel",
"Jack Skille", "Jordan Hendry", "Pascal Pelletier",
"Tim Brent", "Jacob Dowell" };
numPlayers = buildArrays(goals, assists, shots);
sortArrays(players, goals, assists, shots, numPlayers);
printArrays(players, goals, assists, shots, numPlayers);
system ("pause");
return 0;
}
int buildArrays(int goals[NUM_PLAY], int assists[NUM_PLAY], int shots[NUM_PLAY])
{
ifstream inFile;
int num;
int i;
inFile.open("players.txt");
if ( inFile.fail() )
{
cout << "input file did not open";
exit(0);
}
while (inFile)
{
for(i = 0; i < NUM_PLAY; i++)
{
inFile >> num;
goals[i] = num;
inFile >> num;
assists[i] = num;
inFile >> num;
shots[i] = num;
if(inFile.fail())
break;
}
}
return i;
}
void printArrays(string names[], int goals[], int assists[], int shots[], int numPlayers)
{
int i;
int points = 0;
float shotPercent = 0;
cout << fixed << showpoint << setprecision(1) << left;
cout << '\t' << "Chicago Blackhawks 2008-2009 Player Stats";
cout << endl;
cout << endl;
cout << setw(25) << "Players" << setw(10)<< "Goals" << setw(10) << "Assists" << setw(10) << "Points" << setw(10) << "Shots" << setw(8) << "Shooting %";
cout << endl;
cout << "---------------------------------------------------------------------------";
cout << endl;
for(i = 0; i < numPlayers; i++)
{
points = goals[i] + assists[i];
shotPercent = ((float) goals[i] / shots[i]) * 100;
if (shots[i] == 0)
{
cout << setw(25) << names[i] << setw(10) << goals[i] << setw(10) << assists[i] << setw(10) << points << setw(10) << shots[i] << 0.0 << setw(8) << endl;
}
else
{
cout << setw(25) << names[i] << setw(10) << goals[i] << setw(10) << assists[i] << setw(10) << points << setw(10) << shots[i] << shotPercent << setw(8) << endl;
}
}
}
void sortArrays(string names[], int goals[], int assists[], int shots[], int numPlayers)
{
int top;
int temp;
int ssf;
int ptr;
for(top = 0; top < numPlayers; top++)
{
for(ptr = top, ssf = top; ptr <= numPlayers; ptr++)
{
if(names[ptr] < names[ssf])
ssf = ptr;
}
names[temp] = names[top];
names[top] = names[ssf];
names[ssf] = names[temp];
}
}
For this assignment, you are to write a program that will process a data set that represents the players that were with the Chicago Blackhawks during the 2008 - 2009 regular season. The information in the file will be needed for later processing, so it will be stored in a set of arrays that will be displayed, sorted, and displayed (again). For the assignment, you will need to declare four arrays, each of which will hold a maximum of 30 elements: * one array of integers to hold the goals * one array of integers to hold the assists * one array of integers to hold the attempted shots * one array of strings to hold the player names The four arrays will be parallel arrays. This means that the information for one player can be found in the same "spot" in each array. This also means that any change that is applied to one array must also be applied to the other three in order to maintain data integrity. The array of strings should be declared (and initialized as follows): string players[NUM_PLAY] = { "Martin Havlat", "Patrick Kane", "Jonathan Toews", "Kris Versteeg", "Brian Campbell", "Andrew Ladd", "Dave Bolland", "Patrick Sharp", "Duncan Keith", "Cam Barker", "Dustin Byfuglien", "Brent Seabrook", "Troy Brouwer", "Sammy Pahlsson", "Colin Fraser", "Ben Eager", "Matt Walker", "Adam Burish", "Aaron Johnson", "Niklas Hjalmarsson", "Brent Sopel", "Jack Skille", "Jordan Hendry", "Pascal Pelletier", "Tim Brent", "Jacob Dowell" }; The name of the string array and the name of the symbolic constant (to be discussed later) may be changed, but the initial values MUST remain the same. Suggested Logic for main(): NOTE: all of the functions mentioned in this logic are described below. * Fill the three integer arrays by calling the buildArrays() function. * Display the four arrays by calling the printArrays() function. * Sort the four arrays by calling the sortArrays() function. * Display the sorted arrays by calling the printArrays() function. Input The input for this program will be read from a disk file named players.txt. The file consists of a set of records for the players that were with the Chicago Blackhawks during the 2008 - 2009 season. Each record represents one player and is made up of three values: the first is the number of goals the player scored, the second is the number of assists the player recorded, and the third is the number of shots the player attempted. The file resembles the following: 29 48 249 25 45 254 34 35 195 22 31 139 7 45 108 NOTE: You may assume that if there is a number of goals in record, then there will be a number of assists and number of shots. Functions to write and use int buildArrays( int goals[], int assists[], int shots[] ) This function will read the file of data and fill the three arrays. It takes as its arguments the three integer arrays. It returns the number of valid players. Suggested logic: Declare an input file stream (see "buildArrays notes" below) and any other variables that you may need Open the input file and make sure that it opened correctly (see "buildArrays notes" below) Initialize a subscript to the beginning of the array Get the first number of goals from input (see "buildArrays notes" below) While there are records in the file (see "buildArrays notes" below) Put the number of goals into the appropriate array Get a number of assists from input Put the number of assists into the appropriate array Get a number of shots attempted from input Put the number of shots attempted into the appropriate array Increment the subscript to the next spot in the array Get the next number of goals from input Endwhile Close the input file (see "buildArrays notes" below) Return the number of players in an array (think about the subscript) buildArrays notes: * The data is being read from an input file. Before the file can be processed, you must: 1. declare an input file stream (see "Programming Note 1" below) ifstream inFile; 2. open the file and make sure that it opened correctly inFile.open( "averages.txt" ); if ( inFile.fail() ) { cout << "input file did not open"; exit(0); } The above code will open the file and then make sure that the file did indeed open correctly. Note the TWO backslashes!! You must code TWO for every ONE in the path of the file. * To read from a file, use the name of the ifstream in place of cin. For example: int num; inFile >> num; * To test if there are records in the file, simply use the name of the input file stream as a boolean variable. For example: while ( inFile ) As long as there are records in the file, the name of the input file stream name will resolve to true. Once all of the records have been read from the file, the input file stream name will resolve to false. * Once a value has been read from a file, it can be put into the appropriate array element: someArray[i] = num; * After all of the data has been read from the file, the file must be closed. To do this, execute the following: inFile.close(); void printArrays( string names[], int goals[], int assists[], int shots[], int numPlayers ) This function will display the information for the Chicago Blackhawks players. For each player, display their name, number of goals scored, number of assists recorded, number of points, number of shots, and shooting percentage. This function takes as its arguments the four arrays and the number of elements in the arrays. Use the following formulas for the calculated values: Number of Points = Number of goals scored + Number of assists Shooting Percentage = Number of Goals Scored / Number of shots taken * 100 Use the following as a basic format for the output: Chicago Blackhawks 2008-2009 Player Stats Player Goals Assists Points Shots Shooting % ------------------------------------------------------------------ Martin Havlat 29 48 77 249 11.6 Patrick Kane 25 45 70 254 9.8 Jonathan Toews 34 35 69 195 17.4 Kris Versteeg 22 31 53 139 15.8 Brian Campbell 7 45 52 108 6.5 Andrew Ladd 15 34 49 195 7.7 NOTE 1: It's possible that a player didn't attempt any shots during the season. If that's the case, the shooting percentage should be 0.0%. NOTE 2: The player names should be left justified. All of the numeric values should be right justified. The shooting percentage should be displayed with exactly 1 digit after the decimal point. void sortArrays( string names[], int goals[], int assists[], int shots[], int numPlayers ) This function will sort the arrays in ASCENDING order based on the player names. Use the selection sort algorithm presented in lecture. This function takes as its arguments the four arrays and the number of elements in the arrays. It's important to note that the four arrays are parallel arrays, meaning that elements in each array that have the same subscript correspond. Therefore, every time the algorithm swaps two elements in the player names array, it must also swap the corresponding elements in the goals, assists and shots arrays. Programming Notes: 1. Add #include <fstream> at the top of your program. 2. Each array should be able to hold 30 elements. Use a symbolic constant to represent the maximum size of an array (NUM_PLAY in the example above). 3. Each array has the capability to hold 30 elements, however, that does not mean that they will all be used. This is the reason that the number of elements in the array is being passed to the sort and print functions. This value is the return value from buildArrays. 4. Copy the input file to your hard disk and write your program so that it reads the data from the current directory (ie. don't specify a file path). Last edited by hugglebot : 3 Weeks Ago at 09:15 PM |
|
|
|
|
|
#2 (permalink) | |||||||||||||
|
"Ghetto Solutions"
![]() |
Tl;dr
People here will be glad to help, but we're not going to do your homework for you. Break it down, ask for help in specific areas
__________________
Do you really think the iPhone isn't that big? Think Again
|
|||||||||||||
|
|
|
|
#3 (permalink) |
|
New to Overclock.net
|
ok thanks well i was did a little more on it and now when ever i run it the output isn't sorted correctly by that i mean there is a name missing (Martin Havlat) i can't figure out why that is. and also why all the names other then Troy Brouwer are sorted right and that one is displayed up top followed by the blank name slot for Martin Havlat
|
|
|
|
|
|
#4 (permalink) | ||||||||||||||
|
"Ghetto Solutions"
![]() |
Quote:
Also, please format your post with correct punctuation and grammar, it makes everyone happy
__________________
Do you really think the iPhone isn't that big? Think Again
|
||||||||||||||
|
|
|
|
#5 (permalink) |
|
New to Overclock.net
|
Thanks for your help. I forgot to set it as 0
|
|
|
|
|
|
#6 (permalink) | |||||||||||||
|
"Ghetto Solutions"
![]() |
__________________
Do you really think the iPhone isn't that big? Think Again
|
|||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|