2D Array programming questions

first time here just like to intoduce myself, my name is chris. im taking a programming class and obviously im stuck lol. im creating a program that asks the user to enter the integer number to add into a section of a 7x7 matrix that they designate. the program should e add the given
integer number into the section of the matrix only. How do i create a matrix that has all zeros except for where the user designates? Say the user wants to add the number 5 from the cells in row, 5 column 5 to row 6, column 6. how would i make this matrix [0,0,0,0,0,0,0] ?
[0,0,0,0,0,0,0]
[0,0,0,0,0,0,0]
[0,0,0,0,0,0,0]
[0,0,0,0,0,0,0]
[0,0,0,0,0,5,5]
[0,0,0,0,0,5,5]
Last edited on
1
2
3
4
5
6
int matrix[7][7] = {};

matrix[5][5] = 5;
matrix[5][6] = 5;
matrix[6][5] = matrix[5][5]'
matrix[6][6] = matrix[5][6]; 
that was an example of what the user would input. They could put in any combination of numbers and specify which cells to put them in. i would have to code for every combination? isnt there something i could do like a do/while statement.
Yes, use a loop, whose body asks for row, column, and value.
ok thank you keskiverto! now im having trouble coding it so it shows up correctly
1
2
3
4
5
6
7
8
9
10
	while(row1<=row2 && column1<=column2)
	{
		matrixB[row1][column1] = number;
		++row1;
		while(column1<=column2)
		{
			matrixB[row1][column1] = number;
			++column1;
		}
	}

doesnt displays an array filled with ones but this
[1,0,0,0,0,0,0]
[1,1,1,1,1,1,1]
[0,0,0,0,0,0,0]
[0,0,0,0,0,0,0]
[0,0,0,0,0,0,0]
[0,0,0,0,0,0,0]
[0,0,0,0,0,0,0]
Last edited on
alright i seriously cant fill the array can someone please help. Here is what i have so far. im having trouble filling the array correctly and then adding it to the first matrix
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>
using namespace std;



int main ()
{

	int i, j, number, row1, row2, column1, column2;
	int matrixA[7][7] = {{2,5,6,3,5,9,1},{3,0,8,5,6,1,2},{6,4,9,6,5,2,3},
						{1,-4,2,5,1,0,4},{4,9,6,2,2,7,5},{7,1,6,9,8,9,6},
						{-2,-3,-4,5,-6,-7,7}};
	int matrixB [7][7] = {0};
	int sum[7][7] = {0};
	//Section 1
	cout << "Section #1 : Displaying the matrix\n\n";
	//displays the matrix ;array on screen
	for(i=0; i<7; ++i)
	{
		for(j=0; j<7; ++j)
		{
			
			cout << setw(6) << matrixA[i][j];
		}
		cout << "\n";
	}
	//Section 2
	// asks for user to input the number they wish to add to the cells
	cout << "\n\nSection #2 : Get input from the user\n\n" << "	Enter the number you would like to add : ";
	cin >> number;
	cout << endl;
	// asks user for the starting cell
	cout << "	Enter the starting cell\n" << "		Row : ";
	cin >> row1;
	cout << "		Column : ";
	cin >> column1;
	cout << "\n	Enter the ending cell\n" << "		Row : ";
	cin >> row2;
	cout << "		Column : ";
	cin >> column2;
	// creating new matrix
	while( row1<row2)
	{
		matrixB[row1][column1] = number;
		++row1;
		while(column1<=column2)
		{
			matrixB[row1][column1] = number;
			++column1;
		}
	}


	// displays matrixB
		for(i=0; i<7; ++i)
	{
		for(j=0; j<7; ++j)
		{
			
			cout << setw(6) << matrixB[i][j];
		}
		cout << "\n";
	}
	//Section 3
	cout << "\n\nSection #3 : Displaying new matrix\n\n";
	//Adds the matrices 
	sum[7][7]=matrixA[7][7]+matrixB[7][7];
	//displays the new matrix
	for(i=0; i<7; ++i)
	{
		for(j=0; j<7; ++j)
		{
			
			cout << setw(6) << sum[i][j];
		}
		cout << "\n";
	}
}
Last edited on
Topic archived. No new replies allowed.