How to pass 2d arrays

Hello
i have a problem with passing the two dimensional array into printEmployee function and how would that work to get the average of age and salary, can you help please

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 #include <iostream>
#include <string>
#include <stdio.h>

using namespace std;



class Employee
{
private:
	int age;
	int id;
	float salary;
public:
	Employee()
	{
		age = 0;
		id = 0;
		salary = 0;	
	};										 // default constructor: age=0, id=0, and salary=0
	void setAge(int x)
	{
		age = x;
	}									    // let age = x
	void setId(int x)
	{
		id = x;
	}										//  let id = x
	void setSalary(float x)
	{
		salary = 0;
	}										// salary = x
	int getAge() { return age; }            // return age
	int getId() { return id; }              // return id
	float getSalary() { return salary; }    //  return salary
};

void printEmployee(int arg1[][3],int arg2,int arg3)
{
	
	float totalAge,totalSalary,avgSalary, avgAge;


	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			totalAge += ???????
			totalSalary += ?????

		}


	}


}
void main()
{
	Employee x[2][3];

	x[0][0].getAge = 30; x[0][0].getId = 111; x[0][0].getSalary= 30000;
	x[0][1].getAge = 31; x[0][1].getId = 112; x[0][1].getSalary = 31000;
	x[0][2].getAge = 32; x[0][2].getId = 113; x[0][2].getSalary = 32000;
	x[1][0].getAge = 33; x[1][0].getId = 114; x[1][0].getSalary = 33000;
	x[1][1].getAge = 34; x[1][1].getId = 115; x[1][1].getSalary = 34000;
	x[1][2].getAge = 35; x[1][2].getId = 116; x[1][2].getSalary = 35000;

	printEmployee(x,2,3 );


	system("Pause");
}

Line 63 to line 68.
I think you were meant to call setAge() and not getAge().

 
void printEmployee(int arg1[][3],int arg2,int arg3)

The array that you passed into printEmployee() is of type Employee, so arg1 should be of type Employee.
What does arg2 and arg3 do?

Also, why not make an array of 6?

1
2
3
4
5
	
void setSalary(float x)
{
	salary = 0;
}

Think you mean salary = x;

void main() is illegal in C++
should be
int main()
Last edited on
fixed the mistake you mentioned :) thanks

arg2 = how long is the first dimension
arg3 = how long is the second dimension

I created 2d arrays because the question asked for it, its an assignment;

how can i loop through all the values and get the average ?

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
#include <stdio.h>

using namespace std;



class Employee
{
private:
	int age;
	int id;
	float salary;
public:
	Employee()
	{
		age = 0;
		id = 0;
		salary = 0;	
	};										 // default constructor: age=0, id=0, and salary=0
	void setAge(int x)
	{
		age = x;
	}									    // let age = x
	void setId(int x)
	{
		id = x;
	}										//  let id = x
	void setSalary(float x)
	{
		salary = x;
	}										// salary = x
	int getAge() { return age; }            // return age
	int getId() { return id; }              // return id
	float getSalary() { return salary; }    //  return salary
};

void printEmployee(int arg1[][3],int arg2,int arg3)
{
	
	float totalAge,totalSalary,avgSalary, avgAge;


	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			totalAge += ???????
			totalSalary += ?????

		}


	}


}
int main()
{
	Employee x[2][3];

	x[0][0].setAge = 30; x[0][0].setId = 111; x[0][0].setSalary= 30000;
	x[0][1].setAge = 31; x[0][1].setId = 112; x[0][1].setSalary = 31000;
	x[0][2].setAge = 32; x[0][2].setId = 113; x[0][2].setSalary = 32000;
	x[1][0].setAge = 33; x[1][0].setId = 114; x[1][0].setSalary = 33000;
	x[1][1].setAge = 34; x[1][1].setId = 115; x[1][1].setSalary = 34000;
	x[1][2].setAge = 35; x[1][2].setId = 116; x[1][2].setSalary = 35000;

	printEmployee(x,2,3 );


	system("Pause");
	return 0;
}

Last edited on
There are many mistakes, step by step, we will fix the code and I suggest you program that way in the future.

First, the Class set up should be like this:


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

class Employee
	{
	private:
		int Age, ID;
		float Salary;
	public:
		Employee(); // constructor
		void setAge(int x);
		void setID(int x);
		void SetSalary(int x);
		int getAge();
		int getID();
		float getSalary();
		void printEmployee(int arg1[1][2], int arg2, int arg3);
	};


you can't leave void printEmployee out of the class.


after that, you can write your code inside of the functions or just outside like in this example:

1
2
3
4
5
6
7
8
9
10
11
class Rectangle {
    int width, height;
  public:
    void set_values (int,int);
    int area() {return width*height;}
};

void Rectangle::set_values (int x, int y) {
  width = x;
  height = y;
}


so it will be like this:

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
#include <iostream>
#include <string>
using namespace std;

class Employee
	{
	private:
		int Age, ID;
		float Salary;
	public:
		Employee(); // constructor
		void setAge(int x);
		void setID(int x);
		void SetSalary(int x);
		int getAge();
		int getID();
		float getSalary();
		void printEmployee(int arg1[1][2], int arg2, int arg3);
	};

Employee::Employee(){
Age = 0, ID = 0, Salary = 0;
}

void Employee::setAge(int x)
{
Age = x;
}


and so on..

2,

I think you forgot a '2' in the function parameter void printEmployee(int arg1[2][3],int arg2,int arg3)

3,

and now when you want to loop through all values to get the average Salary you need two for loops just like you did:

1
2
3
4
5
6
7
8
9
10
11
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			totalAge = 
			totalSalary = 

		}


	}


4,

in this part:

x[0][0].setAge = 30; x[0][0].setId = 111; x[0][0].setSalary= 30000;

you forgot to add () in the end of every function, setAge, setId, setSalary.

Those are functions and they're 100% of the time written with a (). You can't write a function like that.

5,

when dealing with classes , you need to declare an object inside int main() and then work on your functions based on that.

For example,

printEmployee(x,2,3) shouldn't be like this because it's inside the class.. so we need an object so the class knows that it's his function.

1
2
Employee obj1
obj1.printEmployee(x,2,3)
Last edited on
To loop through the array do this:
1
2
3
4
5
6
7
8
for (int i = 0; i < 2; i++)
{
	for (int j = 0; j < 3; j++)
	{
		totalAge += arg1[i][j].getAge();
		totalSalary += arg1[i][j].getSalary();
	}
}


x[0][0].setAge = 30;
You need to use function notation here.
x[0][0].setAge(30);
Last edited on
Okay, as an exercise, I did the code. I have two problems with it though:

1) in line 41 and 42, it says "expression must have class type"

2) in line 69, the type "int" of x[2][3] is incompatible with Employee x[2][3].

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
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;

class Employee
	{
	private:
		int Age, ID;
		float Salary;
	public:
		Employee(); //constructor
		void setAge(int x) { Age = x; };
		void setID(int x) { ID = x; };
		void setSalary(int x) { Salary = x; };
		int getAge() { return Age; };
		int getID() { return ID; };
		float getSalary() { return Salary; };
		void printEmployee(int arg1[1][2], int arg2, int arg3);
	};

Employee::Employee()
	{
	Age = 0;
	ID = 0;
	Salary = 0.0;
	}

void Employee::printEmployee(int arg1[1][2], int arg2, int arg3)
	{

	int totalAge = 0;
	int totalSalary = 0;
	float AverageSalary = 0;
	float AverageAge = 0;

	for (int i = 0; i < arg2; i++)
		{
		for ( int j = 0; j < arg3; j++)
			{
			totalAge = totalAge + arg1[i][j].getAge();
			totalSalary = totalSalary + arg1[i][j].getSalary();
			}
		}


	AverageAge = (totalAge/6);
	AverageAge = (totalSalary/6);

	cout << "Average Age is: " << AverageAge << endl;
	cout << "Average Salary is: " << AverageSalary << endl;


	}

int main()
	{

	Employee x[2][3];
	Employee obj1;

	x[0][0].setAge(30); x[0][0].setID(111); x[0][0].setSalary(30000);
	x[0][1].setAge(31); x[0][1].setID(112); x[0][1].setSalary(31000);
	x[0][2].setAge(32); x[0][2].setID(113); x[0][2].setSalary(32000);
	x[1][0].setAge(33); x[1][0].setID(114); x[1][0].setSalary(33000);
	x[1][1].setAge(34); x[1][1].setID(115); x[1][1].setSalary(34000);
	x[1][2].setAge(35); x[1][2].setID(116); x[1][2].setSalary(35000);

	obj1.printEmployee(x,2,3);
	
	system("PAUSE");
	return 0;
	}
Topic archived. No new replies allowed.