Init array

Hello, the problem is that i cant deal with initialization of 2 - n array.
1) It there a way to have input const variable by user?
2) What is wrong with my program? It should allow user to input numbers, then output it.


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
 #include "stdafx.h"
#include <iostream>
using namespace std;

void InputGroups(int (**setOfGroups[4][5]), int groups, int numbersIn);
void OutputGroups(int (**setOfGroups[4][5]), int groups, int numbersIn);

int main()
{
	unsigned const int groups = 4;
	unsigned const int numbersIn = 5;
	int **setOfGroups[groups][numbersIn] = {0};
	
	InputGroups(**setOfGroups[4][5], groups, numbersIn);
        OutputGroups(**setOfGroups[4][5], groups, numbersIn)

    return 0;
}

void InputGroups(int (*setOfGroups[4][5]), int groups, int numbersIn)
{
	for (int i = 0; i <= groups; i++)
	{
		for (int k = 0; k <= numbersIn; k++)
		{
			cin >> (*setOfGroups)[i][k];
		}
	}
}

void OutputGroups(int *setOfGroups[4][5], int groups, int numbersIn)
{
	for (int i = 0; i <= groups; i++)
	{
		cout << "" << endl;
		for (int k = 0; k <= numbersIn; k++)
		{
			cout << (*setOfGroups[i][k]) << " ";
		}
	}
}
Last edited on


1) It there a way to have input const variable by user?

No, array sizes must be compile time constants.

2) What is wrong with my program?

What is the purpose of all of those pointers?

Your function prototypes should look more like:
1
2
void InputGroups(int setOfGroups[4][5], int groups, int numbersIn);
void OutputGroups(int setOfGroups[4][5], int groups, int numbersIn);


And your function calls:
1
2
	InputGroups(setOfGroups, groups, numbersIn);
    OutputGroups(setOfGroups, groups, numbersIn);


You shouldn't need any of those pointers any where in your code.

Anyway it does not work without any pointers :(

(14 line) error C2664: 'void InputGroups(int [][5],int,int)': cannot convert argument 1 from 'int' to 'int [][5]'
(15 line) error C2664: 'void OutputGroups(int [][5],int,int)': cannot convert argument 1 from 'int' to 'int [][5]'
Last edited on
Post your modified code.

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
#include "stdafx.h"
#include <iostream>
using namespace std;

void InputGroups(int setOfGroups[4][5], int groups, int numbersIn);
void OutputGroups(int setOfGroups[4][5], int groups, int numbersIn);

int main()
{
	unsigned const int groups = 4;
	unsigned const int numbersIn = 5;
	int setOfGroups[groups][numbersIn] = {0};
	
	InputGroups(setOfGroups[4][5], groups, numbersIn);
	OutputGroups(setOfGroups[4][5], groups, numbersIn);

    return 0;
}

void InputGroups(int setOfGroups[4][5], int groups, int numbersIn)
{
	for (int i = 0; i <= groups; i++)
	{
		for (int k = 0; k <= numbersIn; k++)
		{
			cin >> setOfGroups[i][k];
		}
	}
}

void OutputGroups(int setOfGroups[4][5], int groups, int numbersIn)
{
	for (int i = 0; i <= groups; i++)
	{
		cout << "" << endl;
		for (int k = 0; k <= numbersIn; k++)
		{
			cout << setOfGroups[i][k] << " ";
		}
	}
}
Last edited on
You didn't remove the array size information from your function calls as shown in my first post. All you need is the name of the array in your function call.


line (26) error C2109: subscript requires array or pointer type
line (38) error C2109: subscript requires array or pointer type


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
#include "stdafx.h"
#include <iostream>
using namespace std;

void InputGroups(int setOfGroups, int groups, int numbersIn);
void OutputGroups(int setOfGroups, int groups, int numbersIn);

int main()
{
	unsigned const int groups = 4;
	unsigned const int numbersIn = 5;
	int setOfGroups[groups][numbersIn] = {0};
	
	InputGroups(setOfGroups[4][5], groups, numbersIn);
	OutputGroups(setOfGroups[4][5], groups, numbersIn);

    return 0;
}

void InputGroups(int setOfGroups, int groups, int numbersIn)
{
	for (int i = 0; i <= groups; i++)
	{
		for (int k = 0; k <= numbersIn; k++)
		{
			cin >> setOfGroups[i][k];
		}
	}
}

void OutputGroups(int setOfGroups, int groups, int numbersIn)
{
	for (int i = 0; i <= groups; i++)
	{
		cout << "" << endl;
		for (int k = 0; k <= numbersIn; k++)
		{
			cout << setOfGroups[i][k] << " ";
		}
	}
}
Do you understand the difference between a function call and a function implementation?

You need to remove the array size information in the function call, lines 14 and 15. (see the second snippet in my first post).

The function implementations (lines 20 and 31) require the size information. Please re-read my first post. The function implementations should look the same as the function prototypes (minus the trailing semicolon of course).
Topic archived. No new replies allowed.