Question:
need help with a 2-dimensional array (C++)?
?
2015-12-02 19:50:02 UTC
I asked a question about this once before but I wasn't clear enough.


using a 2-dimensional array, I need it to ask the user what day it is and then select the array for that day.

then the program needs to ask for the employees name and how many hours they worked and store both in the array.

After it has collected the data the program needs to send the data back out to the user.

I need the end result to look something like this:
John 10

Harry 10

Bruce 10


I'm not asking for the exact code that I need to write, but I need an example of what I need to do to get started. I've emailed my instructor to hopefully get more clear directions, but as of this post, he hasn't answered yet. Won't be having the class for a couple days. and unfortunately, this is due in 2 days
Three answers:
?
2015-12-02 21:21:38 UTC
Maybe you should post the instructions that are on the original assignment sheet. As far as I can tell, you've named 3 dimensions:

- Day of Week

- Employee Name

- Number of Hours Worked
cja
2015-12-03 08:27:29 UTC
Using C++ containers you might create a data structure like this:



map< string, vector* > payroll;



You can simulate that with a 2-D array, but the code won't be as nice. In your 2-D array you have a row for each day of the week, and each row is an array of struct, where each struct contains employee info. Here's an example to get you started:



#include



using namespace std;



struct employee {

. . employee() : name(""), hours(0) { }

. . employee(const string &n, int h) : name(n), hours(h) { }

. . string name;

. . int hours;

};

const size_t N(10); // number of employees

typedef enum { Sun, Mon, Tue, Wed, Thu, Fri, Sat, DaysInWeek } Day;

const string days[] = {

. . "Sunday", "Monday", "Tuesday", "Wednesday",

. . "Thursday", "Friday", "Saturday" };



int main(int argc, char *argv[]) {

. . employee *payroll[DaysInWeek];



. . // Allocate employee info arrays for each day

. . for (size_t i = 0; i < DaysInWeek; i++) {

. . . . payroll[i] = new employee[N];

. . }



. . // will prompt for payroll input; for now hardcode a few as an example

. . payroll[Sun][0] = employee("Jack", 8);

. . payroll[Sun][1] = employee("Jill", 9);

. . payroll[Fri][0] = employee("Joe", 7);

. . // ...



. . for (size_t i = 0; i < DaysInWeek; i++) {

. . . . cout << days[i] << ": " << endl;

. . . . for (size_t j = 0; j < N; j++) {

. . . . . . if (payroll[i][j].name != "") {

. . . . . . . . cout << ". " << payroll[i][j].name << ": " << payroll[i][j].hours << " hours" << endl;

. . . . . . }

. . . . }

. . . . cout << endl;

. . }

. . return 0;

}



#if 0



Program output:



Sunday:

. Jack: 8 hours

. Jill: 9 hours



Monday:



Tuesday:



Wednesday:



Thursday:



Friday:

. Joe: 7 hours



Saturday:



#endif
?
2015-12-03 07:31:10 UTC
I would use an array of struct. That way you can store both the name and the hours..



#include



int main(void){

struct emp_data {

string name;

int hours;

};



emp_data e_data[100];



e_data[0].name="roger";

e_data[0].hours=11;

cout << e_data[0].name<< " "<< e_data[0].hours << endl;

return 0;

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...