7 Replies - 515 Views - Last Post: 26 April 2012 - 10:53 PM Rate Topic: -----

#1 dnexquisite_12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-April 12

Trouble with two dimensional arrays

Posted 26 April 2012 - 08:04 PM

This is the what the program should contain in order to run. I'm posting this so it's clear on what the program is asking for.

I'm having trouble with the two-dimensional arrays and passing them as parameters. I understand the struct and its purpose, but the arrays are giving me difficulties. I know that two-dimesional arrays produce a table aspect rather than a list (single array). I'm not sure on how to read the data into the arrays or receive it and output it. I'll will attach my program and it has comments on the particular parts I'm having trouble with. Thank you.

//****************************************************************
Your brother has heard that you have learned to write computer programs. Assuming that you can do anything, he has told his friend that you can help him. His friend is the director of inter-dormitory sports here at Howard University. So, last night his friend called you and asked you to write a computer program that will answer the following questions:

1. What is the total amount of money spent on each sport ?

2. What is the total amount of money spent at each of the dormitories?

3. What is the total amount of money spent on each of the sports at each of the dormitories?

4. What is the total number students participating in each of the sports?

5. What is the total number students participating at each of the dormitories?

6. What is the total number students participating in each of the sports at each of the dormitories?


//****************************************************************
Requirements

Input

Read in a file of 25 records from an external file infile.txt that satisfy the following format:



TYPE OF DATA MEANING RANGES OF VALUES

character string student name up to 24 letters

2 characters dormitory {BT, DG, FT, TU, OP, LN}

integer Number of sports played by this student

character string 1st sport played {FOOTBALL, TENNIS, SOCCER, BASKETBALL}

by this student

character string 2nd sport played {FOOTBALL, TENNIS, SOCCER, BASKETBALL}

by this student (if applicable)

float amount of money {0} – $100.00

spent on this student

(divide by 2 for each sport if student has played 2 sports)


//****************************************************************
To test your program, create an external file named infile.txt and place in it a set of 25 records as described above and presented below.

Tony Danza TU 1 FOOTBALL 50.00

Trevor Pryce DG 2 FOOTBALL SOCCER 100.00

Shirley Taylor BT 1 TENNIS 50.00

Matthew Herring FT 2 FOOTBALL TENNIS 100.00

Geroge Watson LN 1 SOCCER 50.00

Gregory Cherry LN 1 SOCCER 50.00

Troy Glover BT 1 FOOTBALL 50.00

Tammy Price OP 1 TENNIS 50.00

Brandon Thompson FT 2 FOOTBALL SOCCER 100.00

Terrance Williams TU 1 FOOTBALL 50.00

Brian Willis OP 2 FOOTBALL TENNIS 100.00

Pamela Willis FT 1 TENNIS 50.00

Cathy Friem OP 1 SOCCER 50.00

Nathaniel Bentley LN 1 FOOTBALL 50.00

Hansel Brogavich BT 1 TENNIS 50.00

Randy Moss TU 2 BASKETBALL FOOTBALL 100.00

Al Wilson OP 1 FOOTBALL 50.00

Precious Jamison DG 1 TENNIS 50.00

DeShawn Jackson OP 2 SOCCER BASKETBALL 100.00

Matthew McConey TU 2 BASKETBALL FOOTBALL 100.00

Ashley Jones TU 2 BASKETBALL TENNIS 100.00

Clinton Portis DG 2 SOCCER FOOTBALL 100.00

Terrance Richards BT 1 SOCCER 50.00

Angel Lewis OP 2 BASKETBALL 50.00

Rashad Jenkins BT 2 FOOTBALL BASKETBALL 100.00


//****************************************************************
Your program should read in this data, store the data in a struct then pass this struct (record) to a function that adds to the appropriate element in a multi-dimensional array (also passed as a parameter).


Calculate the total amount in dollars and cents and the total number of students as described above.

Provide an output listing that is appropriately formatted and that presents the answers to the questions along with your input data.

Use must enumerated data type(s) where appropriate.

Use a void function to write out your name, course number and title, assignment number, and date, followed by a description of the problem. Use a function somewhere in your calculation that has a struct and a multi-dimensional array as parameters.

Program Output

First print out the your name, course number and title, assignment number, and date, followed by a description of the problem. Then print the input data with appropriate headings, followed by the answer to the problem. All money amounts must be printed as dollar and cents amounts.
//****************************************************************
So it's not letting me attach my cpp file so I have to copy and paste it. Hopefully this doesn't cause a problem. If there's another way to attach my file, please let me know. Thank you.

//****************************************************************
//This program answers corresponding questions
//I'm having output errors
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

//Enum variable declarations
enum Sport{FOOTBALL, BASKETBALL, TENNIS, SOCCER};
enum Dorm{BT, DG, FT, TU, OP, LN};

struct StudentRecord
//User defined struct to hold the student'd information
{
	string firstname;
	string lastname;
	Dorm dorm_name;
	int num_of_sports;
	Sport first_sport;
	Sport second_sport;
	float money_cost;
};
//Function prototypes
void heading();
StudentRecord Read_Student_Record(ifstream&, StudentRecord []);
float Compute_Total_Cost(int[][6], float[][6]);

int main()
{
	int table_count[4][6];
	float table_money[4][6], total_cost;
	int count=0;
	StudentRecord studentinfo[25];
	ifstream mystudents;
	mystudents.open("infile.txt");
	heading();
	while (mystudents)
	{
		studentinfo[count++] = Read_Student_Record(mystudents, studentinfo);
		
	}
	//Determine the table and set it to 0
	for (int sports = 0; sports < 4; sports++)
		for (int dorm = 0; dorm < 6; dorm++)
		{
			table_count[sports][dorm]=0;
			table_money[sports][dorm]=0.0;
		}
	total_cost = Compute_Total_Cost(table_count, table_money);
	cout<<total_cost<<endl;
	return 0;
}
StudentRecord Read_Student_Record(ifstream& mystudents, StudentRecord student_table[])
{
	string DORM, sport;
	int count;
	for (int count = 0; count < 25; count++)
	{
	//Read the data
	//Having problems with the binary operator
	mystudents>>student_table[count].firstname>>student_table[count].lastname>>student_table[count].dorm_name>>
		student_table[count].num_of_sports>>student_table[count].first_sport>>student_table[count].second_sport>>
		student_table[count].money_cost;
	if (DORM == "BT")
		student_table[count].dorm_name= BT;
	else if (DORM == "DG")
		student_table[count].dorm_name= DG;
		else if (DORM == "FT")
			student_table[count].dorm_name= FT;
			else if (DORM == "TU")
				student_table[count].dorm_name= TU;
				else if (DORM == "OP")
					student_table[count].dorm_name= OP;
					else if (DORM == "LN")
						student_table[count].dorm_name= LN;
	if (sport == "FOOTBALL")
		student_table[count].first_sport = FOOTBALL;
	else if (sport == "TENNIS")
		student_table[count].first_sport = TENNIS;
		else if(sport == "SOCCER")
			student_table[count].first_sport = SOCCER;
			else if (sport == "BASKETBALL")
				student_table[count].first_sport = BASKETBALL;
	}
	return student_table[count];
}

float Compute_Total_Cost(int students[4][6], float money[4][6])
{
	int count = 0;
	StudentRecord studentinfo[25];
	cout<<"Question 1: What is the total amount of money spent on each sport?"<<endl;
	if (studentinfo[count++].num_of_sports == 2)
	{
		return studentinfo[count++].money_cost / 2;
	}
	else 
	{
		return studentinfo[count++].money_cost;
	}
	cout<<"Football: $"<<endl;
	cout<<"Basketball: $"<<endl;
	cout<<"Tennis: $"<<endl;
	cout<<"Soccer: $"<<endl;
}



:code:

This post has been edited by r.stiltskin: 26 April 2012 - 08:08 PM
Reason for edit:: Added code tags. Please learn to use them.


Is This A Good Question/Topic? 0
  • +

Replies To: Trouble with two dimensional arrays

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Trouble with two dimensional arrays

Posted 26 April 2012 - 08:23 PM

Your biggest problem is that the istream extraction operator >> doesn't know how to read a string and store it in an enum. So I suggest that you modify your StudentRecord definition and change the type of dorm, first_sport and second_sport to string.
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Trouble with two dimensional arrays

Posted 26 April 2012 - 08:44 PM

You should realize that there is no easy, automatic translation from, for example, the string "FOOTBALL" to the FOOTBALL in your enum. FOOTBALL in your enum is an identifier -- a name, like a variable name, class name, etc. -- nothing at all like a character string or a std::string, which are values, or in other words, data.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Trouble with two dimensional arrays

Posted 26 April 2012 - 09:24 PM

It's not entirely clear what your instructor means by "where appropriate", since in this case it seems like more of a nuisance than anything else, but if you feel that you must use enums for those fields in the struct, you can read the char strings from the file into char[] or string variables, and then use a switch or a series of if statements to "translate" the strings into the enum identifiers.
Was This Post Helpful? 0
  • +
  • -

#5 dnexquisite_12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-April 12

Re: Trouble with two dimensional arrays

Posted 26 April 2012 - 09:28 PM

So are the if-else statements I have incorrect? Also, it's required that we use enum variables, so that's why I have named the particular variables within struct in relation to the enum declaration.
Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Trouble with two dimensional arrays

Posted 26 April 2012 - 09:34 PM

No, they're not wrong. What's wrong are these parts of your input beginning on line 63
//...
 >> student_table[count].dorm_name >> student_table[count].first_sport >> student_table[count].second_sport //...


Those inputs should be going into your string variables DORM and sport (and another one that you haven't yet declared, for the 2nd sport) first., and then your if-else statements can transfer the entries into the struct fields.

But you'll need a similar set of statements for the 2nd sport as well.

(Actually, I didn't notice before that you had already written those if-else statements. I stopped reading as soon as I saw the >> extractions.)

This post has been edited by r.stiltskin: 26 April 2012 - 09:38 PM

Was This Post Helpful? 0
  • +
  • -

#7 dnexquisite_12  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 26-April 12

Re: Trouble with two dimensional arrays

Posted 26 April 2012 - 09:48 PM

Thank you so much, it's making a little bit more sense. Now that I have successfully rad my data. I can now place it into the arrays correct?

This is where I get a bit confused. If I have the two-dimesional arrays called int table_count[4][6] and float table_money[4][6] (with the indices set at a value) which holds the corresponding data in that particular table, correct? I know the first index is the rows and the second is the columns.

How do you go from that step to the output stage. Trying to cout those tables doesn't print out the corresponding information that should print.
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Trouble with two dimensional arrays

Posted 26 April 2012 - 10:53 PM

You're not ready to print anything yet. After you have input the raw data into the studentinfo array, you have to use that data to calculate the values that you have to store in the table_count and table_money arrays. (Incidentally, you could probably come up with more descriptive names for those -- names that actually describe the info they will contain.)

I see that you're passing those 2D arrays to your Compute_Total_Cost function. That's good, but what about the studentinfo array? Don't you need that info to compute the costs and the counts? You shouldn't declare a new studentinfo array in Compute_Total_Cost. That new array contains no data. You have to pass the filled-in array as a parameter.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1