Help with arrays

Hi I'm new to this c++ programming and im struggling with this array program.
I need to get array looking like this
[11,12,13,14,15
21,22,23,24,25
31,32,33,34,35]
and I also need to solve it using two functions: Functions which gets me to these numbers, and fuction which prints out this multidimensional array. Then I have to put them in main function.

I Think i need to use something like this:
1
2
3
4
5
6
7
8
  for (n = 0; n < 3; n++)
	{ 
		for (m = 0; m < 5; m++)
		{
			matrica[n][m] = (n + 1) * (m+1);
			cout << matrica[n][m] << " ";
		}
	cout << endl;


Sorry I'm not native English speaker.

Last edited on
It should to be:

 
matrica[n][m] = (n + 1) * 10 + (m + 1);

And you should remove the cout statement from that loop since you say you want to have a separate function to print it.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
int main()
{
    int matrix[3][5];
    for(int i = 0; i < 3; ++i)
    {
        for(int j = 0; j < 5; ++j)
        {
            matrix[i][j] = (10*(i+1)+(j+1));
            std::cout << matrix[i][j] << " ";
        }
        std::cout.put('\n');
    }
}


If you need a seperate function that prints it out, just remove the calls to std::cout.
Last edited on
Thank you I got it. Now im having problems with another taks. I have to write a program which adds up matrices. I keep writing them while either entered row is 0 or column. When 1 of them is 0 I got to exit the loop and finish the program. If dimensions are different than 0 I have to add them up, print result and repeat input. This is how far I managed to come:
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
#include <iostream>
using namespace std;
#define N 100
#define M 100


int main()
{
	int i, j, k, l;
	int a[N][M];
	int b[N][M];
	int c[N][M];

	do 
	{
		cin >> k;
		cin >> l;
		a[k][l] = { 0 };
		b[k][l] = { 0 };
		c[k][l] = { 0 };
		for (i = 0; i < k; i++)
			for (j = 0; j < l; j++) 
			{
				cin >> a[i][j];
				cin >> b[i][j];
			}
		for (i = 0; i < k; i++)
			for (j = 0; j < l; j++)
				c[i][j] = a[i][j] + b[i][j];
		for (i = 0; i < k; i++)
		{
			for (j = 0;j < l; j++)
				cout << c[i][j] << " ";
			cout << "\n";
		}
	} while (k != 0 || l != 0);
	return 0;
}
Last edited on
Your loop header should be: while (k != 0 && l != 0);
Topic archived. No new replies allowed.