Answer to Question #273323 in C++ for Adil

Question #273323

Write a C++ program that takes the input (or initializes randomly) in 2-dimensional square matrix of odd rows and columns. Your

program should calculate the sum of all values except the center value. Store the sum in center element of array. The program must then

display the resultant array. The program must work for all odd number square matrices. Do not write code for fixed size matrix.


1
Expert's answer
2021-11-30T00:36:41-0500
#include <iostream>


using namespace std;


int main()
{
    int n, sm = 0;
    cin >> n;
    int arr[n][n];
    for(int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> arr[i][j];
            if (!(i == j && i == n/2)) {
                sm += arr[i][j];
            }
        }
    }
    arr[n/2][n/2] = sm;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cout << arr[i][j] << ' ';
        }
        cout << '\n';
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS