Multidimesnional Array

Make a program that will ask for the number of rows and column to output a two-dimensional array using pointers. The program must contains two user-defined function called multimdimensional() to enter the elements of rows and columns and result() to output the two-dimensioanal array.

Sample Output:
Enter your rows and your columns: 2 3

Enter 3 number<s> for your row number 0: 1 2 3
Enter 3 number<s> for your row number 1: 3 2 1

Two Dimensional Board:
1 2 3
3 2 1

//I don't know how to do it. Can you please help me?
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
  #include<iostream>
using namespace std;

void multidimensional();
void result();

int main()
{
	multidimensional();

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

void multidimensional()
{
	int row, column;
	cout << "Enter your rows and your columns: ";
	cin >> row >> column;
	cout << endl;
	for (int i = 0; i < row; i++){
		cout << "Enter " << column << " number<s> for row number " << i << " : ";
		for (int j = 0; j < row; j++)
			cin >> row;
		cout << endl;
	}
}
I have my second try but has a lot of errors
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
#include<iostream>
using namespace std;

int multidimensional(int a[10][10], int, int);
void result();
int array[10][10];

int main()
{
	multidimensional(a[10][10], row, column);

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

int multidimensional(int array[10][10], int row, int column)
{
	cout << "Enter your rows and your columns: ";
	cin >> row >> column;
	cout << endl;
	for (int i = 0; i < row; i++){
		cout << "Enter " << column << " number<s> for row number " << i << " : ";
		for (int j = 0; j < row; j++)
			cin >> row;
		cout << endl;
	}
}

void result()
{

}
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
int main()
{
    int m,n;    //dimension;
    //get m and n from user

   //allocate memory for matrix of mxn
   int **matrix = new int[m][n];

   //call multidimensional on matrix
   multidimensional(matrix,m,n);

   //call result
   result(matrix,m,n);

  //end
}
void multidimensional(int **matrix, int m, int n)
{
    //fill the matrix
    for(int i = 0; i < m; i++)
        for(int j = 0, element; cin << element && j < n; j++)
            matrix[i][j] = element;
}
void result(int **matrix, int m, int n)
{
    //print result
    for(int i = 0; i < m; i++)
    {
        cout<<endl;
        for(int j = 0; j < n; j++)
            cout<<matrix[i][j]<<" ";
    }
}


1
2
3
4
int m, n;    
	cout << "Enter your rows and your columns: ";
	cin >> m >> n;
	int **matrix = new int[m][n]; //the [n] errors 

Last edited on
Here's my full 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
#include<iostream>
using namespace std;

void multidimensional(int **, int, int);
void result(int **, int, int);

int main()
{
	int m, n;    
	cout << "Enter your rows and your columns: ";
	cin >> m >> n;
	int **matrix = new int[m][n]//errors;
	multidimensional(matrix, m, n);
	result(matrix, m, n);
}

void multidimensional(int **matrix, int m, int n)
{
	for (int i = 0; i < m; i++){
		cout << "Enter " << n << " number<s> for your row number : " << m;
		for (int j = 0, element; cin >> element && j < n; j++){
			matrix[i][j] = element;
		}
	}
}

void result(int **matrix, int m, int n)
{
	for (int i = 0; i < m; i++)
	{
		cout << endl;
		for (int j = 0; j < n; j++)
			cout << matrix[i][j] << " ";
	}
}
int **matrix = new int[m][n] should be
1
2
3
int **matrix = new int*[m];
for(int i = 0; i < m ; i++)
    matrix[i] = new int[n];

and clean up will be

1
2
3
for(int i = 0; i < m; i++)
   delete[] matrix[i];
delete[] matrix;
It inputs only this:
Enter your rows and your columns: 2 3

Enter 3 number<s> for your row number 0: 1 2 3

And i believe that here's the problem, where did i do wrong in this area?
1
2
3
4
5
6
7
8
9
void multidimensional(int **matrix, int m, int n)
{
	for (int i = 0; i < m; i++){
		cout << "Enter " << n << " number<s> for your row number " << i << " : ";
		for (int j = 0, element; cin >> element && j < n; j++){
			matrix[i][j] = element;
		}
	}
}
I fixed it!
for (int j = 0, element; cin >> element && j + 1 < n; j++){
The only problem is the result();.
It outputs the
1 2 -2344733//random numbers
3 2 -127127123789//random numbers
Last edited on
Well i fixed it by myself! Here's the whole 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
42
43
#include<iostream>
using namespace std;

void multidimensional(int **, int, int);
void result(int **, int, int);

int main()
{
	int m, n;    
	cout << "Enter your rows and your columns: ";
	cin >> m >> n;
	int **matrix = new int*[m];
	for (int i = 0; i < m; i++)
		matrix[i] = new int[n];
	multidimensional(matrix, m, n);
	result(matrix, m, n);

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

void multidimensional(int **matrix, int m, int n)
{
	for (int i = 0; i < m; i++){
		cout << "Enter " << n << " number<s> for your row number " << i << " : ";
		for (int j = 0, element; j < n; j++){
			cin >> element;
			matrix[i][j] = element;
		}
	}
}

void result(int **matrix, int m, int n)
{
	cout << endl;
	cout << "Two dimensional board \n";
	for (int i = 0; i < m; i++)
	{
		cout << endl;
		for (int j = 0; j < n; j++)
			cout << matrix[i][j] << " ";
	}
}
Good!
Topic archived. No new replies allowed.