# C++ 2D array help please!

Hey guys! I am taking a level 1 C++ course online and have gotten to two-dimensional arrays. I have come across a problem I cant solve and was hoping to get some help. The prompt is as follows:

"(Compute the weekly hours for each employee) Suppose the weekly hours for all employees are stored in a two-dimensional array. Each row records and employee's seven-day work hours with seven columns. For example, the following array stores the work hours for eight employees. Write a program that displays employees and their total hours in decreasing order of the total hours."

I have gotten everything to work except the sorting it into decreasing order. If anyone can help I would greatly appreciate it! Here is the code I currently have: https://docs.google.com/document/d/1-k58PwV2N-rH4sBCECBsrcQ-qUtpfxiwSphjvg9FoMo/edit?usp=sharing

EDIT: Didn't realize it was seeing it as formatting. I'll upload it to somewhere and link it.

Can you put it in pastebin or something? Google doc isn't really formatting it like code.

A brief glance and I saw

if(j == NUMEMPLOYEES) inside a for loop defined for(int j = i; j < NUMEMPLOYEES; j++)

j will never == NUMEMPLOYEES b/c you're stopping the loop before it can occur.

# Pro Tip

You can actually display code using the following... ``````#include <iostream>
using namespace std;

const int NUM_OF_EMPLOYEES = 8;
const int NUM_OF_WEEK_DAYS = 7;

//Function to sum up the hours worked by a employee
double sumHours(const double m[][NUM_OF_WEEK_DAYS], int empNum)
{
double sum = 0;
for (int i = 0; i < NUM_OF_WEEK_DAYS; i++)
{
sum += m[empNum][i];
}
return sum;
}

int main()
{
//Declare variables
double matrix[NUM_OF_EMPLOYEES][NUM_OF_WEEK_DAYS];
double empHoursWorked[NUM_OF_EMPLOYEES];
double maxHours = 0;
double maxEmp;
double tempEmp;
double tempHours;
int maxIndex;

//Set employee numbers into empHoursWorked array
for (int i = 0; i < NUM_OF_EMPLOYEES; i++)
{
empHoursWorked[i] = i;
}

cout << "Enter a " << NUM_OF_EMPLOYEES << "-by-" << NUM_OF_WEEK_DAYS << " matrix row by row where each row represents a employee and each column represents" << "\n" <<"the hours worked by that employee on each day of the week starting with Sunday \n";

//Cin matrix array
for (int row = 0; row < NUM_OF_EMPLOYEES; row++)
{
for (int column = 0; column < NUM_OF_WEEK_DAYS; column++)
{
cin >> matrix[row][column];
}
}

//Add the hours worked and set them into the empHoursWorked array
for (int i = 0; i < NUM_OF_EMPLOYEES; i++)
{
empHoursWorked[i] = sumHours(matrix, i);
}

//This is the part that is supposed to put it in the correct order but I can't get it to work
for (int i = 0; i < NUM_OF_EMPLOYEES; i++)
{
for (int j = i; j < NUM_OF_EMPLOYEES; j++)
{
if (empHoursWorked[j] > maxHours)
{
maxEmp = empHoursWorked[j];
maxHours = empHoursWorked[j];
maxIndex = j;
if (j == NUM_OF_EMPLOYEES)
{
tempEmp = empHoursWorked[i];
tempHours = empHoursWorked[i];
empHoursWorked[i] = maxEmp;
empHoursWorked[i] = maxHours;
empHoursWorked[maxIndex] = tempEmp;
empHoursWorked[maxIndex] = tempHours;
maxHours = 0;
maxEmp = 0;
}
}
}
}

//Print the employee number followed by the number of hours worked by that employee
for (int i = 0; i < NUM_OF_EMPLOYEES; i++)
{
cout << "Emp: " << empHoursWorked[i] << " Hours worked: " << empHoursWorked[i] << "\n";
}

system("pause");
return 0;
}``````

# Just a couple things...

• `double` really isn't necessary here. (unless you need to track the employee's time down to the nano second. (use `float`)
• You should use pointers when calling functions with arrays instead of putting the whole array onto the stack. Try this...

``````float sumHours(float *week)
{
float sum = 0;
for (int day = 0; day < NUM_OF_WEEK_DAYS; day++)
sum += *week[day];
return sum;
}``````

and call it like this...

``empHoursWorked[i] = sumHours( &matrix[empl] );``
• You can use `#define` instead of the `const int` at the top of the document.
• avoid one letter variables in your loops (makes it easier to select and replace later)
• is that insert sort? The best way to fix a sort is to go step by step manually and write down what is in the array as you go. (basically, debug carefully)

Thats good to know.

I used `double` because I just wanted to make sure that numbers didn't get truncated when added if they contained a decimal.

I don't know anything about pointers yet and I don't know if the class I'm in will go over them, unless they are part of the object oriented part of it.

I didn't know that but good to know.

I usually don't but I was trying to get something that functioned and then clean it up.

I don't know what kind of sort it is. I was just trying to think of something that would work. The idea was that the first loop would change the start row for the second loop each iteration and the second loop and if statement would find the largest number and then move it and its corresponding employee to the position the loop started in. ninerdelta pointed out where my problem might be. I'll change if(j==NUM_OF_EMPLOYEES) to if(j==NUM_OF_EMPLOYEES-1) and see what happens.

1 Like

Trust me... you're fine with `float`. A double is a "double precision float" and is 64bit. A regular 32bit float is capable of holding 7(average) significant digits. That's more than enough precision for counting seconds in a year.

Ok. One thing at a time then. Just be aware than pointers are an integral and powerful part of C code. (no, they aren't an object-oriented-programming thing.)

Sorting algorithms are pretty well established in the comp-sci world. Just pic one and find some pseudo code to guide you.

List of algorithms
* https://en.wikipedia.org/wiki/Sorting_algorithm

Some pseudo code
* http://c.happycodings.com/sorting-searching/index.html

Visualising sorting algorithms

Sorting funnies
*

Thanks for your help. I finally got it to work. There is some really useful information in what you provided and I'll be sure to look over it some more. I'm trying to get my associates in system administration right now and had to take a level 1 programming course. Seeing as I want to learn how to make games in Unreal Engine I though C++ would be a good choice. So far most of it hasn't been any harder that HTML or CSS but I'm sure it'll get much more complex. Here is the code I finished with(changed doubles to floats, I also tried using `#define` but it didn't like it.):

``````#include <iostream>
using namespace std;

const int NUM_OF_EMPLOYEES = 8;
const int NUM_OF_WEEK_DAYS = 7;

//Function to sum up the hours worked by a employee
float sumHours(const float m[][NUM_OF_WEEK_DAYS], int empNum)
{
float sum = 0;
for (int i = 0; i < NUM_OF_WEEK_DAYS; i++)
{
sum += m[empNum][i];
}
return sum;
}

int main()
{
//Declare variables
float matrix[NUM_OF_EMPLOYEES][NUM_OF_WEEK_DAYS];
float empHoursWorked[NUM_OF_EMPLOYEES];

//Set employee numbers into empHoursWorked array
for (int i = 0; i < NUM_OF_EMPLOYEES; i++)
{
empHoursWorked[i] = i;
}

cout << "Enter a " << NUM_OF_EMPLOYEES << "-by-" << NUM_OF_WEEK_DAYS << " matrix row by row where each row represnts a employee and each column repesents" << "\n" <<"the hours worked by that employee on each day of the week starting with Sunday \n";

//Cin matrix array
for (int row = 0; row < NUM_OF_EMPLOYEES; row++)
{
for (int column = 0; column < NUM_OF_WEEK_DAYS; column++)
{
cin >> matrix[row][column];
}
}

//Add the hours worked and set them into the empHoursWorked array
for (int i = 0; i < NUM_OF_EMPLOYEES; i++)
{
empHoursWorked[i] = sumHours(matrix, i);
}

//Sort array into decreasing order
for (int i = 0; i < NUM_OF_EMPLOYEES - 1; i++)
{
float currentMin = empHoursWorked[i];
float currentMinEmp = empHoursWorked[i];
int currentMinIndex = i;

for (int j = i; j < NUM_OF_EMPLOYEES; j++)
{
if (currentMin < empHoursWorked[j])
{
currentMin = empHoursWorked[j];
currentMinEmp = empHoursWorked[j];
currentMinIndex = j;
}
}

if (currentMinIndex != i)
{
empHoursWorked[currentMinIndex] = empHoursWorked[i];
empHoursWorked[currentMinIndex] = empHoursWorked[i];
empHoursWorked[i] = currentMinEmp;
empHoursWorked[i] = currentMin;
}
}

//Print the employee number followed by the number of hours worked by that employee
for (int i = 0; i < NUM_OF_EMPLOYEES; i++)
{
cout << "Emp: " << empHoursWorked[i] << " Hours worked: " << empHoursWorked[i] << "\n";
}

system("pause");
return 0;
}``````
1 Like

Nice work!

Oh... you have to do it like this...

``````#define NUM_OF_EMPLOYEES 8
#define NUM_OF_WEEK_DAYS 7``````

This is called a "Pre-Processor" command ... aka a macro.
When the compiler reads the code file and once it parses these lines, it always replaces "NUM_OF_EMPLOYEES" with "8" in the code before compiling. I makes it just like as if you had typed "8" down in your code. It's very efficient.

Oh okay. That sounds like a lot better alternative to constants.

Yea, mostly just because it saves memory and compiles down a bit better.