Hello, I am currently having an issue with a project I am working on. The rest of my project is fine, I am just having an issue with one logical portion of it, and I was hoping someone could help me.
I also have to use 1D arrays to solve this problem, I can also make use of parallel 1D arrays

Basically the problem that I am solving is: there are 8 departments within a business and each of the 8 departments have a list of ($/per hour) and the (hours) that each individual within that department worked for the week.

I need to be able to output the gross pay for each department. I have a text file that is split up like so:
Department number(1-8), wage, hours worked

Everything is already split and converted accordingly so that I have three parallel arrays. So department[1], wage[1], hours[1] all refer to one individual (which department they work in, what their wage is, and how many hours they worked.

Since ultimately I have to have each of the 8 departments’ gross pay, I knew I needed an array with 8 elements each of which could hold an accumulation of gross pay for each department. [o] element holds gross pay for department 1, [1] holds gross pay for department 2, etc.
I’m not sure exactly what I am doing wrong but here were my ideas that I have attempted so far. (The rest of my project works fine, I have narrowed down the issue to the logic):

So the departments array holds all of the departments, the hours array holds all the hours worked, and the salary array holds all the wages, the total array is a 8 element array that has been initialized to 0 and is ready to hold the accumulations

So basically the program would start at the first element of departments (each element of departments can only be 1-8) and search it against a counter until they matched. Once the department number for that particular element is discovered, then we could use it with the other parallel arrays (wage and hours), multiply them to get a gross pay, and then store it in a total accumulation array.

I would appreciate any help.

Note this is just the logic module:

static void grossPay(double[] departments, double[] hours, double[] salary, double[] total)
            double hourly = 0;
            double wage = 0;
            double totalPay = 0.0;
            int count = 1;

            //Initialize total
            for (int index = 0; index < total.Length; index = index + 1)
                total[index] = 0;
            }//end for initialization

            foreach (int element in departments)
                while (count <= 8)
                    if (element == count)
                        hourly = hours[element];
                        wage = salary[element];
                        totalPay = hourly * wage;
                        total[element] = total[element] + totalPay;
                        count = count + 1;


Recommended Answers

All 2 Replies

Parallel arrays should rarely be used, and this isn't one of the rare occations.

Learn to use classes, you have an employee who has a department, hours and wage. Sounds like three properties to me. You can even add a property to give total pay for that employee.

That said, your logic for summing the deparments is confusing. You just need to loop through each 'record' and add the value to the total:

for (int i = 0; i < departments.length; i++) {
    total[departments[i]] += hours[i] * salary[i];

That's all you need for your lines 15-29, make sure you initialize total to be one larger than the number of deparments (so it will go from 0 to number-of-departments) rather than one less :) Ignore the zero element in the total array.

If that really bothers you, subtract one from departments[i].

if you did want to make your existing logic work try this:

foreach (int element in departments)
    total[element] += hours[element] * salary[element];
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.