2d array [] []

hi guys i have an issues with my project. I have declared an 2darray filled it with random numbers and there is a task wehere I have to add to each element on the periphery of the matrix a number entered from the keyboard. BTW matrix is 10x7(rowXcolumns).

for (int i = 0; i < m; i++){
matrix[0][i] = matrix[0][i]+c;
for (int j = 0; j < n; j++){
matrix[j][0] = matrix[j][0]+c;
}
}

here is the code but it isnt working...Thanks for help "
what do you mean by
periphery of the matrix
and what is your full program?
for example
here is matrix

1 2 3 4 5 6
6 5 4 3 2 1
5 1 4 8 9 5

and the periphery
1 2 3 4 5 6
6 x x x x 1
5 1 4 8 9 5
Last edited on
whats your code so far?
PM mate :-)
its unreadable without code tags.
its better you post here.
1
2
3
4
5
6
7
8
void plusCOnPeriphery(int matrix[M][N], int m, int n, int c){
	for (int i = 0; i < m; i++){
		matrix[0][i]+=c;
		for (int j = 0; j < n; j++){
			matrix[j][0] +=c;
		}	
	}	
}


1
2
3
4
5
6
7
8
void replaceRowWithArray(int matrix[M][N], int m, int n, unsigned short R, int newRow[N]){
	for (int i = R; i == R; i++){
			for (int j = 0; j < n; j++){
				matrix[i][0] = newRow[N];
		}
		
	}
}

at the first function i have to add entered number C to the periphery of the matrix
second funcition entered number R represents a single row in a matrix which i have to change with a new row (newRow[])
Last edited on
i have made newRow array filled up also with random numbers but issues are with algorithm
instead of nested loop, you can use 4 linear loops for four sides.
guys how do I replace a row in a matrix with another array ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void replaceRowWithArray(int matrix[M][N], int m, int n, unsigned short R, int newRow[N]){
	for (int i = R; i == R; i++){
			for (int j = 0; j < n; j++){
				matrix[i][0] = newRow[N];
		}
		
	}
}









you can do in several ways
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
// send 2d array to functions
#include <iostream>
using namespace std;

template <typename TwoD>
void function1(TwoD Array, int row, int col){
     Array[row-1][col-1] = 50;  
}

void function2(int Arr[], int size){
	for(int i=0; i<size; i++){
		Arr[i]= 10+i;
	}
}

void function3(int Ar[2][3]){ //fixed size !
	Ar[0][0] = 1;
}

int main(){
	int arr[2][3] = 
	{
		{1,2,3,},
		{4,5,6,}
	};

	function1(arr, 2, 3);	
	cout << arr[1][2] << "\n"; // 50

	function2(arr[1], 3);
	for(int i=0; i<3; i++){
		cout << arr[1][i] << " "; // 10 11 12
	}
	cout << "\n";	
	
	function3(arr);
	cout << arr[0][0] << "\n"; // 1

return 0;
}
Topic archived. No new replies allowed.