I'm having trouble sorting this square matrix (a 2d array that has number of rows and same number of column n x n) using row-wise approach in C programming. Please help me program this in C to sort it using row-wise approach, here is the code:
#include <stdio.h>
#define MAX 100
int main() {
int mat[MAX][MAX];
int i, j, m, n;
int rowsum, columnsum, diagonalsum;
int k;
int magic = 0;
int transpose[MAX][MAX];
printf("Enter the # of rows and columns of the square matrix (must
be n x n):\n");
scanf("%d %d", &m, &n);
if(m==n) {
printf("Enter the elements of the matrix: \n");
for(i=0; i<m; i++) {
for(j=0; j<n; j++) {
scanf("%d", &mat[i][j]);
}
}
printf("The matrix is:\n");
for(i=0; i<m; i++) {
for(j=0; j<n; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
for (i = 0; i < m; i++)
for( j = 0 ;
j < n ; j++ )
transpose[j][i] = mat[i][j];
printf("Transpose of the matrix:\n");
for (i = 0; i < n; i++)
{
for (j = 0;
j < m; j++)
printf("%d\t", transpose[i][j]);
printf("\n");
}
// calculate diagonal sum
diagonalsum = 0;
for(i=0; i<m; i++) {
for(j=0; j<n; j++) {
if(i==j) {
diagonalsum = diagonalsum + mat[i][j];
}
}
}
// calculate row sum
for(i=0; i<m; i++) {
rowsum = 0;
for(j=0; j<n; j++) {
rowsum = rowsum + mat[i][j];
}
if(rowsum != diagonalsum) {
printf("The matrix is not magic square\n");
return;
}
}
// calculate column sum
for(i=0; i<m; i++) {
columnsum = 0;
for(j=0; j<n; j++) {
columnsum = columnsum + mat[j][i];
}
if(columnsum != diagonalsum) {
printf("The matrix is not magic square\n");
return;
}
}
printf("The matrix is a magic square\n");
} else {
printf("Please enter a square matrix, where m = n\n");
}
int all_distinct = 1;
int count, l;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
count = 0;
for (k = 0; k < m; k++) {
for (l = 0; l < n; l++) {
if (mat[k][l] == mat[i][j]) {
count++;
}
}
}
if (count != 1) {
all_distinct = 0;
}
}
}
if (all_distinct) {
printf("All elements in the matrix are distinct\n");
} else {
printf("All elements in the matrix are not distinct\n");
}
return 0;
}
#The code is slightly modified for error correction. Use of exit() function is done instead of using return;
#include <stdio.h>
#include<stdlib.h>
#define MAX 100
int main()
{
int mat[MAX][MAX];
int i, j, m, n;
int rowsum, columnsum, diagonalsum;
int k;
int magic = 0;
int transpose[MAX][MAX];
printf("Enter the # of rows and columns of the square
matrix (must be n x n):\n");
scanf("%d %d", &m, &n);
if(m==n)
{
printf("Enter the elements of the
matrix: \n");
for(i=0; i<m; i++)
{
for(j=0; j<n;
j++)
{
scanf("%d", &mat[i][j]);
}
}
printf("The matrix is:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n;
j++)
{
printf("%d\t", mat[i][j]);
}
printf("\n");
}
for (i = 0; i < m; i++)
for( j = 0 ; j
< n ; j++ )
transpose[j][i]
= mat[i][j];
printf("Transpose of the
matrix:\n");
for (i = 0; i < n;
i++)
{
for (j =
0; j < m; j++)
printf("%d\t", transpose[i][j]);
printf("\n");
}
//calculate diagonal sum
diagonalsum = 0;
for(i=0; i<m; i++)
{
for(j=0; j<n;
j++)
{
if(i==j)
{
diagonalsum = diagonalsum +
mat[i][j];
}
}
}
// calculate row sum
for(i=0; i<m; i++)
{
rowsum =
0;
for(j=0; j<n;
j++)
{
rowsum = rowsum + mat[i][j];
}
if(rowsum !=
diagonalsum)
{
printf("The matrix is not magic
square\n");
exit(1);
}
}
// calculate column sum
for(i=0; i<m; i++)
{
columnsum =
0;
for(j=0; j<n;
j++)
{
columnsum = columnsum + mat[j][i];
}
if(columnsum !=
diagonalsum)
{
printf("The matrix is not magic
square\n");
exit(1);
}
}
printf("The matrix is a magic
square\n");
}//End of IF
else
{
printf("Please enter a square
matrix, where m = n\n");
}
int all_distinct = 1;
int count, l;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
count = 0;
for (k = 0; k
< m; k++)
{
for (l = 0; l < n; l++)
{
if (mat[k][l] ==
mat[i][j])
{
count++;
}
}
}
if (count !=
1)
{
all_distinct = 0;
}
}
}
if (all_distinct)
{
printf("All elements in the matrix
are distinct\n");
}
else
{
printf("All elements in the matrix
are not distinct\n");
}
return 0;
}//End of main()
#include <stdio.h>
#include<stdlib.h>
#define MAX 100
int main()
{
int mat[MAX][MAX];
int i, j, m, n;
int rowsum, columnsum, diagonalsum;
int k;
int magic = 0;
int transpose[MAX][MAX];
printf("Enter the # of rows and columns of the square
matrix (must be n x n):\n");
scanf("%d %d", &m, &n);
if(m==n)
{
printf("Enter the elements of the
matrix: \n");
for(i=0; i<m; i++)
{
for(j=0; j<n;
j++)
{
scanf("%d", &mat[i][j]);
}
}
printf("The matrix is:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n;
j++)
{
printf("%d\t", mat[i][j]);
}
printf("\n");
}
for (i = 0; i < m; i++)
for( j = 0 ; j
< n ; j++ )
transpose[j][i]
= mat[i][j];
printf("Transpose of the
matrix:\n");
for (i = 0; i < n;
i++)
{
for (j =
0; j < m; j++)
printf("%d\t", transpose[i][j]);
printf("\n");
}
//calculate diagonal sum
diagonalsum = 0;
for(i=0; i<m; i++)
{
for(j=0; j<n;
j++)
{
if(i==j)
{
diagonalsum = diagonalsum +
mat[i][j];
}
}
}
// calculate row sum
for(i=0; i<m; i++)
{
rowsum =
0;
for(j=0; j<n;
j++)
{
rowsum = rowsum + mat[i][j];
}
if(rowsum !=
diagonalsum)
{
printf("The matrix is not magic
square\n");
exit(1);
}
}
// calculate column sum
for(i=0; i<m; i++)
{
columnsum =
0;
for(j=0; j<n;
j++)
{
columnsum = columnsum + mat[j][i];
}
if(columnsum !=
diagonalsum)
{
printf("The matrix is not magic
square\n");
exit(1);
}
}
printf("The matrix is a magic
square\n");
}//End of IF
else
{
printf("Please enter a square
matrix, where m = n\n");
}
int all_distinct = 1;
int count, l;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
count = 0;
for (k = 0; k
< m; k++)
{
for (l = 0; l < n; l++)
{
if (mat[k][l] ==
mat[i][j])
{
count++;
}
}
}
if (count !=
1)
{
all_distinct = 0;
}
}
}
if (all_distinct)
{
printf("All elements in the matrix
are distinct\n");
}
else
{
printf("All elements in the matrix
are not distinct\n");
}
return 0;
}//End of main()
#include <stdio.h>
#include<stdlib.h>
#define MAX 100
int main()
{
int mat[MAX][MAX];
int i, j, m, n;
int rowsum, columnsum, diagonalsum;
int k;
int magic = 0;
int transpose[MAX][MAX];
printf("Enter the # of rows and columns of the square
matrix (must be n x n):\n");
scanf("%d %d", &m, &n);
if(m==n)
{
printf("Enter the elements of the
matrix: \n");
for(i=0; i<m; i++)
{
for(j=0; j<n;
j++)
{
scanf("%d", &mat[i][j]);
}
}
printf("The matrix is:\n");
for(i=0; i<m; i++)
{
for(j=0; j<n;
j++)
{
printf("%d\t", mat[i][j]);
}
printf("\n");
}
for (i = 0; i < m; i++)
for( j = 0 ; j
< n ; j++ )
transpose[j][i]
= mat[i][j];
printf("Transpose of the
matrix:\n");
for (i = 0; i < n;
i++)
{
for (j =
0; j < m; j++)
printf("%d\t", transpose[i][j]);
printf("\n");
}
//calculate diagonal sum
diagonalsum = 0;
for(i=0; i<m; i++)
{
for(j=0; j<n;
j++)
{
if(i==j)
{
diagonalsum = diagonalsum +
mat[i][j];
}
}
}
// calculate row sum
for(i=0; i<m; i++)
{
rowsum =
0;
for(j=0; j<n;
j++)
{
rowsum = rowsum + mat[i][j];
}
if(rowsum !=
diagonalsum)
{
printf("The matrix is not magic
square\n");
exit(1);
}
}
// calculate column sum
for(i=0; i<m; i++)
{
columnsum =
0;
for(j=0; j<n;
j++)
{
columnsum = columnsum + mat[j][i];
}
if(columnsum !=
diagonalsum)
{
printf("The matrix is not magic
square\n");
exit(1);
}
}
printf("The matrix is a magic
square\n");
}//End of IF
else
{
printf("Please enter a square
matrix, where m = n\n");
}
int all_distinct = 1;
int count, l;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
count = 0;
for (k = 0; k
< m; k++)
{
for (l = 0; l < n; l++)
{
if (mat[k][l] ==
mat[i][j])
{
count++;
}
}
}
if (count !=
1)
{
all_distinct = 0;
}
}
}
if (all_distinct)
{
printf("All elements in the matrix
are distinct\n");
}
else
{
printf("All elements in the matrix
are not distinct\n");
}
return 0;
}//End of main()
I'm having trouble sorting this square matrix (a 2d array that has number of rows and...
#include "stdio.h" int main() { float array[5][3],rowsum=0.0,colsum=0.0; int i,j; for(i=0;i<5;i++){ printf("Enter the %d elements of array:\n",i); for(j=0;j<3;j++){ scanf("%f",&array[i][j]); } } printf("Given table data:\n"); for(i=0;i<5;i++){ for(j=0;j<3;j++){ printf(" %0.1f",array[i][j]); } printf("\n"); } for(i=0;i<5;i++){ for(j=0;j<3;j++){ rowsum=rowsum+array[i][j]; } printf("sum of the %d row is %0.1f\n",i,rowsum); rowsum=0; } for(j=0;j<3;j++){ colsum+=array[j][i]; printf("Sum of %d coloumn is %0.1f\n",j,colsum); colsum=0; } return(0); } can someone help me to get the correct row sum and column sum in c program
Explain what the problem is within the program. Fix the problem when you create a child process per column. The code is given below. So basically, after the child processes have successfully excuted their code, the final print statement does not give the correct values. It prints the original matrix rather than the multiplied matrix.#include #include #include #include int main(int argc, char *argv[]){ int row = 0; int column = 0; row = atoi(argv[1]); column = atoi(argv[2]); int *A = ...
Write a function Transpose that transposes a matrix T with M rows and N colums. The transposed matrix, TT, should have N rows and M columns. Example. Given the matrix T. (C++14) Example: 1 2 3 0 -6 7 The transposed matrix TT is 1 0 2 -6 3 7 DEFAULT CODE: Add to code as needed: #include <iostream> #include <string> #include <cmath> using namespace std; void Transpose(int T[100][100], int TT[100][100], int M, int N) { int i; int j;...
Java language only **Mandatory Rules** Put Descriptive comments on the code displayed on top of previous comments alreadys displayed On Top of Code write summary of what code will do in comments At the end of the code have the output in comment form I Will thumbs up good Work thanks! public class MagicSquare { static int[][] createMagicSquare(int square[][]) { // Initialize position for 1 int i = 3/2; int j = 3-1; // One by one put all values...
I am trying to add a string command to my code. what am i doing wrong? #define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <stdio.h> #include <string.h> #include <math.h> int main(void) { int i, j; int rowA, colA, rowB, colB; int A[10][10], B[10][10]; int sum[10][10]; char str1[10]; printf("This is a matrix calculator\n"); //read in size from user MATRIX A printf("Enter in matrix A....\n"); printf("\t#row = "); scanf("%d", &rowA); printf("\t#col = "); ...
Debug the following matrix program in C: // Program to read integers into a 3X3 matrix and display them #include <stdio.h> void display(int Matrix[3][3],int size); int main(void) { char size; double Matrix[size][size+1]; printf("Enter 9 elements of the matrix:\n"); int i; for (i = 0: i <= size: i++) { int j = 0; for (; j <= size++; j++){ scanf("%d", matrix[i--][4]) } } Display(Matrix,9); return 0; void...
ASSEMBLY LANGUAGE The matrix (two-dimensional array) with ROWS and COLS dimensions of integer values is given. Perform various matrix processing activities according to the algorithms below. Store the results into the output vector (one-dimensional array) with appropriate size. For Grade 7) Count the number of odd values (n mod 2 <> 0) for each row. For Grade 9) Calculate the sum of positive values for each column. To obtain inputs and return the results, define appropriate type C/C++ functions. Please...
Use basic java for this after importing PrintWriter object You will make a simple Magic Square program for this Java Programming Assignment. Carefully read all the instructions before beginning to code. It is also required to turn in your pseudocode or a flowchart along with this program. Here are the instructions: At the beginning of the program, briefly describe to the user what a Magic Square Matrix is (described further on), and then allow them to enter “start” to begin...
Consider the following matrix transpose routines: typedef int array[4][4]; void transpose (array dst, array src) { int i, j; for (i=0; i<4; i++) { for (j=0; j<4; j++) { dst[i][j] = src[i][j]; } } } void transpose2 (array dst, array src) { int i, j; for (i=0; i<4; i++) { for (j=0; j<4; j++) { dst[j][i] = src[j][i]; } } } Assume this code runs on...
Define a two-dimensional int array which has 5 rows and 3 columns. The elements in array is randomly initialized (using Math.random()). Write a method to find the maximum value in this two dimensional array; Write a method to find the average value in the array; Write a method to count how many elements are smaller than average; Write a method to copy contents in a two-dimensional array to a one-dimensional array. The method will return this one-dimensional array. Method is...