Add integer to submatrix

Hello,

First post, literally made the account to ask this as the assignment is due tomorrow night and I hope that someone can help me. I have had no experience with programming before this class and I'm drowning so any help is appreciated.

The problem asks me to take a predefined 7x7 matrix, ask for a user defined integer, and a user defined sub matrix (so ask them for the beginning row and column offset and the ending row and column offset) that they want to modify. I have absolutely no idea how to do that. All I have so far is pasted below, but it creates an entirely new matrix and adds the whole thing to the original matrix.

#include <iostream>
using namespace std;

int main()
{
int r=6, c=6, b[7][7], sum[7][7], i, j,value;
int a[7][7]{1,0,2,0,-3,0,4,0,2,-3,0,4,0,-5,0,-3,0,4,0,-5,0,-3,0,4,0,-5,0,6,0,4,0,-5,0,6,0,4,0,-5,0,6,0,7};


cout << endl << "Enter integer " << endl;
cin>>value;

for(int i=0; i<6; i++)
{
for(int j=0; j<6; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}


for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
b[i][j]={value};
}


for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
sum[i][j] = a[i][j] + b[i][j];


cout << endl << "Sum of two matrix is: " << endl;
for(i = 0; i < r; ++i)
for(j = 0; j < c; ++j)
{
cout << sum[i][j] << " ";
if(j == c - 1)
cout << endl;
}

return 0;
}

To clarify, I need to make it so that the user defined integer only appears in the user defined matrix size, while the rest of the numbers are 0s.

so if the user wanted to add the integer 5 to the matrix, from [1][0] to [4][2]. the user created matrix would have to look like this:

0 5 5 5 5 0 0--------------------1 0 2 0 -3 0 4
0 5 5 5 5 0 0--------------------0 2 0 -3 0 4 0
0 5 5 5 5 0 0--------------------2 0 -3 0 4 0 -5
0 0 0 0 0 0 0--------------------0 -3 0 4 0 -5 0
0 0 0 0 0 0 0------------------- -3 0 4 0 -5 0 6
0 0 0 0 0 0 0---------------------0 4 0 -5 0 6 0
0 0 0 0 0 0 0---------------------4 0 -5 0 6 0 7

I assume that is how it would have to work since to add matrices they have to be the same size right?


edit: I tried indenting the code so it's easier to read but for some reason I can't get it working.

edit: I tried indenting the code so it's easier to read but for some reason I can't get it working. The dashes were the only thing I could do for spacing
Last edited on
1
2
3
4
5
6
7
8
for(int i = 0; i < r; ++i)
	for(int j = 0; j < c; ++j)
		b[i][j]={value};


for(int i = 0; i < r; ++i)
	for(int j = 0; j < c; ++j)
		sum[i][j] = a[i][j] + b[i][j];


> I assume that is how it would have to work since to add matrices they have to
> be the same size right?
you may do
1
2
b[rbeg:roff, cbeg:coff] = value
s = a + b
where `b' has the same size than a and has 0 outside the range
or may do
1
2
s = a
s[rbeg:roff, cbeg:coff] += b
where `b' is a smaller matrix and you only operate on the ROI.

To traverse the [rbeg:roff, cbeg:coff] range you need to modify the limits of the loop
1
2
for(int i = rbeg; i < rbeg+roff; ++i)
	for(int j = cbeg; j < cbeg+coff; ++j)



> I tried indenting the code so it's easier to read but for some reason I can't get it working.
[code]
Put the code you need help with here.
[/code]
¿sounds familiar?
Here is the 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
44
45
46
47
48
49
#include<iostream>


using namespace std;



int main()
{
	int myMatrix[7][7]{1,0,2,0,-3,0,4,0,2,-3,0,4,0,-5,0,-3,0,4,0,-5,0,-3,0,4,0,-5,0,6,0,4,0,-5,0,6,0,4,0,-5,0,6,0,7};
	const int rsize(7),csize(7);
	int newMatrix[7][7];
	int value,x,y,a,b;
	int sum[i][j];


	for(int i=0; i<6; i++)    
	{
		for(int j=0; j<6; j++) 
		{
			cout << myMatrix[i][j]  << "  ";
		}
		cout << endl;
	}
	
	cout<<"Enter an integer value: ";
	cin>> value;
	cout<<"Enter the beginning row and column offset you would like to modify: ";
	cin>>x>>y;
	cout<<"Enter the ending row and column offset: ";
	cin>>a>>b;
	
	for(int k=0; k<a; k++)    
	{
		for(int l=0; l<y; l++) 
		{
			newMatrix[k][l]=value;
			sum= newMatrix[k][l]+myMatrix[k][l];
			
			cout<< sum<<endl;
		}
		
	}

	
	

return(0);
}


I will be honest though, I have never seen anything even remotely close to your solution there. We're not allowed to use advanced programming (meaning anything we haven't covered) and that we definitely haven't looked at.
that was pseudocode.
m[rbeg:rend, cbeg:cend] is not valid c++, just wanted to say to traverse that range of the matrix
rows from rbeg till rend
columns from cbeg till cend

1
2
3
4
	cout<<"Enter the beginning row and column offset you would like to modify: ";
	cin>>x>>y;
	cout<<"Enter the ending row and column offset: ";
	cin>>a>>b;
use meaningful names.
also, you ask twice for column offset.
¿what are the ranges heres? ¿[x:y, x:y], [x:a, y:b], [x:x+a, y:y+b]?

1
2
3
	for(int k=0; k<a; k++)    
	{
		for(int l=0; l<y; l++) 
¿where are `a' and `b'? ¿why do you start at 0?
you may notice that `newMatrix' is not needed and may just use `value'


here's another way
1
2
3
4
5
6
7
int n=7;
for(int K=0; K<n; ++K)
   for(int L=0; L<n; ++L)}
      sum[K][L] = myMatrix[K][L];
      if(between(rbeg, K, end) and between(cbeg, L, cend))
         sum[K][L] += value;
   }
Topic archived. No new replies allowed.