+ 1

Why my code is not getting accepted

Create a structure Matrix having member interger type variables, 2d pointer, rows, columns. Write function to initialize and remove dyanmic memory. Write functions to read and print values. Print function is to print all elements in matrix form. Read function will read values and place into Matrix. Write function to multiply ith row of matrix with some element, note here i starts from 1 not from 0. Write function to interchange ith column with jth column. Finally, write main to demonstrate Matrix. Read rows & columns and initialize Matrix. Next read (rows x columns) elements and place in matrix row wise. Print matrix. Next read two integers. First is row number (again starting row is 1, be careful), second is number for multiplication. Multiply all elements of specific row with the number. Lastly, read two more integers representing column numbers. Interchange columns accordingly. Final print matrix again. What I have done so far to solve this Problem. Input Format 3 2 2 6 4 4 3 1 2 2 1 3 Constraints No Constraint Output Format 2 6 4 4 3 1 4 6 2 2 3 8

25th Sep 2020, 10:14 PM
Arslan Iftikhar
Arslan Iftikhar - avatar
1 Resposta
0
I am writing my code but this is not the answer this is my fail attempt #include<iostream> using namespace std; struct matrix { int rows, col, idx; int **array; void init (void) { int idx; array = new int*[rows]; for(idx = 0; idx < rows; idx++) { array[idx] = new int[col]; } } void remove(void) { for(int idx = 0; idx < rows; idx++) { delete[] array[idx]; } delete[] array; //THis is my week point i was not removing it properly. } void reader(void) { int idx, kdx; for(idx = 0; idx < rows; idx++) { for(kdx = 0; kdx < col; kdx++) { cin>>array[idx][kdx]; } } } void printer(void) { int idx, kdx; for(idx = 0; idx < rows; idx++, cout<<"\n\n") { for(kdx = 0; kdx < col; kdx++) { cout<<array[idx][kdx]<<" "; } } } void multiply(int row_index, int number) { row_index = row_index - 1; for(int kdx = 0; kdx < col; kdx++) { array[row_index][kdx] = number * array[row_index][kdx]; } } void col_interchange(int col_i, int col_j) { int temp; col_i = col_i - 1; col_j = col_j - 1; for(idx = 0; idx < rows; idx++) { temp = array[idx][col_j]; array[idx][col_j] = array[idx][col_i]; array[idx][col_i] = temp; } } }; int main(void) { int mul, index, col_i, col_j; matrix m1; cin>>m1.col>>m1.rows; m1.init(); m1.reader(); m1.printer(); cin>>index>>mul; m1.multiply(index, mul); cin>>col_i>>col_j; m1.col_interchange(col_i, col_j); m1.printer(); m1.remove(); return 0; }
25th Sep 2020, 10:18 PM
Arslan Iftikhar
Arslan Iftikhar - avatar