Multiply 2 array

I am trying to multiply two arrays one being hours the other being wages and showing results into salary. I have tried different things but am not getting it to multiply hours[0] to wage[0] then move onto hours[1]*wage[1]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

int main()
{
	int n = 3;
	int wage[]{ 10,5,20 };
	int hours[]{ 40,50,60 };
	int salary[]{ 0 };

	for (int h = 0; h < n; h++)
	{
		for (int g = h; g < n; g++)

		
			for (int i = 0; i < n; i++)
			{
				salary[i] = wage[h] * hours[g];
				cout << salary[i] << endl;
			}
	}
	
	return 0;
}
Alright I got closer it is multiplying and getting right totals into salary but I am getting a "Run-Time Check Failure #2 - Stack around the variable 'salary' was corrupted."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int main()
{
	int n = 3;
	int wage[]{ 10,5,20 };
	int hours[]{ 40,50,60 };
	int salary[]{ 0 };

	for (int h = 0; h < n; h++)
	{
		salary[h] = wage[h] * hours[h];
		cout << salary[h] << endl;
		//for (int g = h; g < n; g++)
		
			/*for (int i = 0; i < n; i++)
			{
				salary[h] = wage[h] * hours[h];
				cout << salary[h] << endl;
			}*/
	}
	
	return 0;
}
I figured it out....


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main()
{
	int n = 3;
	int wage[]{ 10,5,20 };
	int hours[]{ 40,50,60 };
	int salary[3];

	for (int h = 0; h < n; h++)
	{
		salary[h] = wage[h] * hours[h];
		cout << salary[h] << endl;
		
	}
	
	return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
    const int n = 3; // <--
    int wage[]{ 10,5,20 };
    int hours[]{ 40,50,60 };
    int salary[n]{ 0 }; // <---
    
    for (int h = 0; h < n; h++)
    {
        salary[h] = wage[h] * hours[h];
        cout << salary[h] << endl;
    }
    
    return 0;
}
Thanks againtry I thought I have it figured out and I did on that one but when I plug it into the real problem I am doing it goes back to only doing one calculation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <iomanip>

using namespace std;

void displayEmployeeInfo(const int, const int[], const double[]);
void employeePay(const int, int[], double[], double[], double[]);


int main()
{
    const int NUMBER_OF_EMPLOYEES = 7;
    int empId[] = { 1234567,
                    2345678,
                    3456789,
                    4567890,
                    5679801,
                    6789012,
                    7890123 };
    double wages[NUMBER_OF_EMPLOYEES];
    double hours[NUMBER_OF_EMPLOYEES]{ 40,35,50,100,34,39,44 };
    double payRate[]{ 15,12,13,14,11,10,15 };
    employeePay(NUMBER_OF_EMPLOYEES, empId, hours, payRate, wages);
    displayEmployeeInfo(NUMBER_OF_EMPLOYEES, empId, wages);
   

    return 0;
} // end of int main()


void employeePay(const int NUMBER_OF_EMPLOYEES,
    int empId[],
    double hours[],
    double payRate[],
    double wages[])
{
    for (int i = 0; i < sizeof(empId) / sizeof(int); i++)
    {
        wages[i] = payRate[i] * hours[i];
       
    }
}//end of void employeePay

void displayEmployeeInfo(const int NUMBER_OF_EMPLOYEES,
    const int empId[],
    const double wages[])
{
    cout << setprecision(2) << fixed;
    cout << "Employee ID number and wages below: " << endl;
    for (int i = 0; i < NUMBER_OF_EMPLOYEES; i++)
       
    {
        
        cout << "Wages for Employee #" << empId[i]
            << " = $"
            << wages[i]
            << endl;
    }

}//end of displayEmployeeInfo 




Employee ID number and wages below:
Wages for Employee #1234567 = $600.00
Wages for Employee #2345678 = $-92559631349317830736831783200707727132248687965119994463780864.00
Wages for Employee #3456789 = $-92559631349317830736831783200707727132248687965119994463780864.00
Wages for Employee #4567890 = $-92559631349317830736831783200707727132248687965119994463780864.00
Wages for Employee #5679801 = $-92559631349317830736831783200707727132248687965119994463780864.00
Wages for Employee #6789012 = $-92559631349317830736831783200707727132248687965119994463780864.00
Wages for Employee #7890123 = $-92559631349317830736831783200707727132248687965119994463780864.00
Figured out it. This line of code wasn't acting like it did when it was in the int main()

 
for (int i = 0; i < sizeof(empId) / sizeof(int); i++)



changed to this:

 
for (int i = 0; i < NUMBER_OF_EMPLOYEES; i++)
Yep, FWIW the array size needs to be specified in each case. That was the 'arrowed' cause of the error I showed in your cut-down version. salary only had one element, not 3 as with the other two.

There are a number of solutions but specifying a constant NUMBER_OF_EMPLOYEES = 7 at the start of main() and carrying taht all the way through all the arrays to maintain consistency works, otherwise you need to create the arrays dynamically (See new/delete and pointers )

That's the problem with C-style arrays - ready to bite at a moments notice.

And, you could check array size compatibilty by creating a function that checks the sizeof/sizeof calculation for each array and ensure that it has exactly NUMBER_OF_EMPLOYEES elements :)
Last edited on
Thanks againtry, I will try to remember that in future. Slowly working my way into harder problems will start taking a look into dynamic arrays.
@briancb2004, all good and take your time.

The tutorials on this site are useful and here's the one on new/delete for a dull moment in the coming weeks/months etc.

https://www.cplusplus.com/doc/tutorial/dynamic/
:)
Topic archived. No new replies allowed.