### 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
 ``123456`` ``````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
 ``12345678910`` `````` 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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081`` ``````#include #include #include #include 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
Last edited on
Topic archived. No new replies allowed.