
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Fill missing entries of a magic square in C++
Suppose we have one 3x3 matrix, whose diagonal elements are empty at first. We have to fill the diagonal such that the sum of row, column and the diagonal will be same. Suppose a matrix is like −
0 | 3 | 6 |
5 | 0 | 5 |
4 | 7 | 0 |
After filling, it will be −
6 | 3 | 6 |
5 | 5 | 5 |
4 | 7 | 4 |
Suppose the diagonal elements are x, y, z. The values will be −
- x = (M[2, 3] + M[3, 2])/ 2
- z = (M[1, 2] + M[2, 1])/ 2
- y = (x + z)/2
Example
#include<iostream> using namespace std; void displayMatrix(int matrix[3][3]) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) cout << matrix[i][j] << " "; cout << endl; } } void fillDiagonal(int matrix[3][3]) { matrix[0][0] = (matrix[1][2] + matrix[2][1]) / 2; matrix[2][2] = (matrix[0][1] + matrix[1][0]) / 2; matrix[1][1] = (matrix[0][0] + matrix[2][2]) / 2; cout << "Final Matrix" << endl; displayMatrix(matrix); } int main() { int matrix[3][3] = {{ 0, 7, 6 }, { 9, 0, 1 }, { 4, 3, 0 }}; cout << "Given Matrix" << endl; displayMatrix(matrix); fillDiagonal(matrix); }
Output
Given Matrix 0 7 6 9 0 1 4 3 0 Final Matrix 2 7 6 9 5 1 4 3 8
- Related Articles
- Magic Square
- In a magic square each row, column, and diagonal have the same sum. Check which of the following is a magic square.
- In a “magic square”, the sum of the numbers in each row, in each column and along the diagonals is the same. Is this a magic square?
- Fill missing numeric values in a JavaScript array
- How to Fill the Entries in Parsing Table?
- How to Check if the Matrix is a Magic Square in Java?
- Check given matrix is magic square or not in C++
- How to fill a data.table row with missing values in R?
- Fill in the missing data in the following table
- In the following APs, find the missing terms in the boxes:$-4, \square, \square, \square, \square, 6$
- In the following APs, find the missing terms in the boxes:$\square, 38, \square, \square, \square, -22$
- Fill in the missing fractions.(a) \( \frac{7}{10}-\square=\frac{3}{10} \)(b) \( \square-\frac{3}{21}=\frac{5}{21} \)(c) \( \square-\frac{3}{6}=\frac{3}{6} \)(d) \( \square+\frac{5}{27}=\frac{12}{27} \)
- Write a Python code to fill all the missing values in a given dataframe
- In a magic square each row, column and diagonals have the same sum. Check which of the following is a magic square.$( i)$.$5$$-1$$-4$$-5$$-2$$7$$0$$3$$-3$$( ii)$.1−100−4−3−2−64−7
- In the following APs, find the missing terms in the boxes:$\square, 13, \square, 3$

Advertisements