Calculating sum of rows in matrix - c

The sum of rows part just doesn't work properly . Any suggestions?
Also if the main diagonal is i==j, what will be the opposite diagonal ? How do i define it?
int main (void) {
int A[5][5];
int B[5];
int x=0,sum=0;
int n,m,i=0,j;
printf("Enter rows and columns : \n");
scanf("%d %d",&n,&m);
printf("Enter matrix : \n");
for (i = 0 ; i < n ; i++) {
for (j = 0 ; j < m ; j++) {
scanf("%d",&A[i][j]);
}
}
/* Sum of rows Problem */
for(i = 0 ; i < n ; i++) {
B[i] = 0;
for(j = 0 ; j < m ; j++) {
B[i] = B[i] + A[i][j];
++i;
}
}
for(i = 0 ; i < n ; i++) {
for(j = 0 ; j < m ; j++) {
printf("The sum of rows %d \n", B[j]);
}
}
return 0;
}

Actually, you just have to remove ++i inside the inner loop, and the program runs fine.
Code:
int main (void) {
int A[5][5];
int B[5];
int x=0,sum=0;
int n,m,i=0,j;
printf("Enter rows and columns : \n");
scanf("%d %d",&n,&m);
printf("Enter matrix : \n");
for (i = 0 ; i < n ; i++) {
for (j = 0 ; j < m ; j++) {
scanf("%d",&A[i][j]);
}
}
/* Sum of rows Problem */
for(i = 0 ; i < n ; i++) {
B[i] = 0;
for(j = 0 ; j < m ; j++) {
B[i] = B[i] + A[i][j]; //Removed the stray ++i from here.
}
}
for(i = 0 ; i < n ; i++)
{
printf("The sum of row %d is %d \n",i+1,B[i]);
}
return 0;
}
And answering your second question, the opposite diagonal is i == size - j- 1 if size is the size of the array.

Related

I want to use scanf to store values in a 2D array in C language. But it doesn't store it like how i want it to

#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=0;
int y=0;
int matrix[x][y];
printf("no. of rows \n");
scanf("%d",&x);
printf("no. of columns \n");
scanf("%d",&y);
printf("co-efficient of matrix \n");
for(int i = 0 ; i < x ; i++)
{
for(int j = 0 ; j < y ; j++)
{
scanf("%d",&matrix[i][j]);
};
};
for(int i = 0 ; i < x ; i++)
{
for(int j = 0 ; j < y ; j++)
{
printf("%d",matrix[i][j]);
};
printf("\n");
};
printf("%d",matrix[0][0]);
}
Output looks like this.
I input the values:
1 2 3 4 5 6
but then the in the output is:
4 5 6 4 5 6
moving
int matrix[x][y];
below
scanf("%d",&y);
have fixed the issue. My array had [0][0] dimensions as I was declaring the array right after declaring x and y which were initialized with 0 value. Hence declaring the array after getting the values of x and y fixed the problem.
I prefer malloc to allocate memory.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x,y;
printf("no. of rows \n");
scanf("%d",&x);
printf("no. of columns \n");
scanf("%d",&y);
int **matrix= (int **)malloc(sizeof(int)*x*y);
printf("co-efficient of matrix \n");
for(int i = 0 ; i < x ; i++)
{
for(int j = 0 ; j < y ; j++)
{
scanf("%d",&matrix[i][j]);
};
};
for(int i = 0 ; i < x ; i++)
{
for(int j = 0 ; j < y ; j++)
{
printf("%d",matrix[i][j]);
};
printf("\n");
};
printf("%d",matrix[0][0]);
free(matrix);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x = 0;
int y = 0;
printf("no. of columns \n"); scanf("%d", &x);
printf("no. of rows \n"); scanf("%d", &y);
printf("co-efficient of matrix \n");
int* p = (int*)malloc(x * y * sizeof(int));
//memset(p,0 , x*y*sizeof(int));
int **matrix=(int**)malloc(y*sizeof(int*));
for (int a = 0; a < y; a++)
{
matrix[a] = p + a * x; //matrix[a]=&p[a*x]
}
for (int j = 0; j < y; j++)
for (int i = 0; i < x; i++)
scanf("%d", &matrix[j][i]);
for (int j = 0; j < y; j++)
{
for (int i = 0; i < x; i++)
printf("%d", matrix[j][i]);
printf("\n");
}
free(matrix);
free(p);
}

2x2 Matrix Multiplication

I'm trying to write a program to calculate the sum and product of two matrices, but I can't get it the product to work. The sum is fine. I am using Visual Studio.
Here's my program:
#include <stdio.h>
#include <stdlib.h>
int main()
{
float a[2][2], b[2][2], c[2][2], d[2][2], sum;
int i,j,k;
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
d[i][j] = 0;
}
}
printf("Enter the elements of 1st matrix\n");
/*
* Reading two dimensional Array with the help of two for loop. If there
* was an array of 'n' dimension, 'n' numbers of loops are needed for
* inserting data to array.
*/
for (i = 0; i < 2; ++i)
for (j = 0; j < 2; ++j) {
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%f", &a[i][j]);
}
printf("\nEnter the elements of 2nd matrix\n");
for (i = 0; i < 2; ++i)
for (j = 0; j < 2; ++j) {
printf("Enter b%d%d: ", i + 1, j + 1);
scanf("%f", &b[i][j]);
}
printf("\nMatrix 1:\n");
for (i = 0; i < 2; ++i)
for(j = 0; j < 2; ++j) {
printf("%.1f\t", a[i][j]);
if (j == 1) /* To display matrix sum in order. */
printf("\n");
}
printf("\nMatrix 2:\n");
for(i = 0; i < 2; ++i)
for(j = 0; j < 2; ++j) {
printf("%.1f\t", b[i][j]);
if (j == 1) /* To display matrix sum in order. */
printf("\n");
}
for (i = 0; i < 2; ++i)
for(j = 0; j < 2; ++j) {
/* Writing the elements of multidimensional array using loop. */
c[i][j]=a[i][j]+b[i][j]; /* Sum of corresponding elements of two arrays. */
}
printf("\nSum Of Matrix:\n");
for (i = 0; i < 2; ++i)
for(j = 0; j < 2; ++j) {
printf("%.1f\t",c[i][j]);
if (j == 1) /* To display matrix sum in order. */
printf("\n");
}
for(i = 0 ; i < 2 ; i++) {
for(j = 0 ; j < 2 ; j++) {
sum = 0 ;
for(k = 0 ; k < 2 ; k++) {
sum = sum + a[i][k] * b[k][j];
printf("a: %d; b: %d\n", a[i][k], b[k][j]);
printf("%d", sum);
}
d[i][j]=sum;
}
}
printf("\nThe multiplication matrix is : \n\n") ;
for(i = 0; i < 2; i++) {
for(j = 0; j < 2; j++) {
printf("%d \t", d[i][j]) ;
}
printf("\n") ;
}
system("PAUSE");
return 0;
}
Here is the output:
Matrix 1:
1.0 2.0
3.0 4.0
Matrix 2:
5.0 6.0
7.0 8.0
Sum Of Matrix:
6.0 8.0
10.0 12.0
a: 0; b: 1072693248
0a: 0; b: 1073741824
0a: 0; b: 1072693248
0a: 0; b: 1073741824
0a: 0; b: 1074266112
0a: 0; b: 1074790400
0a: 0; b: 1074266112
0a: 0; b: 1074790400
0
The multiplication matrix is :
0 0
0 0
I can't see where the problem is with both matrices A and B.
First off, I would not hardcode the termination of the for loops to a constant, but to sizeof(a)/sizeof(a[0]), for instance. Secondly, the problem is you are trying to print floats as ints - line 68 reads:
printf("a: %d; b: %d\n",a[i][k],b[k][j]);
but it should be
printf("a: %.1f; b: %.1f\n",a[i][k],b[k][j]);
These problems exist on lines 68, 69, and 79. If you change the %d to %f you should be fine.

How to move largest number of matrix

I need to move the largest number of randomly generated matrix to the right lower corner of matrix, but I don't know how. Can someone help.
This is my code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, rows, columns;
int **matrix;
printf("write the number of rows\n");
scanf("%d", &rows);
printf("write the number of columns\n");
scanf("%d", &columns);
matrix = malloc(rows * sizeof *matrix);
for (i = 0 ; i < rows ; i++)
{
matrix[i] = malloc(columns * sizeof(int));
for (j = 0 ; j < columns ; j++)
matrix[i][j] = rand() % 100;
}
for (i = 0 ; i < rows ; i++)
{
for (j = 0 ; j < columns ; j++)
printf("%5d", matrix[i][j]);
printf("\n");
}
for (i = 0 ; i < rows ; i++)
free(matrix[i]);
free(matrix);
return 0;
}
Try the following
int max = matrix[0][0];
for (i = 0 ; i < rows ; i++)
{
for (j = 0 ; j < columns ; j++)
{
if ( max < matrix[i][j] ) max = matrix[i][j];
}
}
matrix[rows-1][columns-1] = max;
If you need to exchange the right lower element of the matrix with the maximum element then the code can look like
int max_i = 0;
int max_j = 0;
for (i = 0 ; i < rows ; i++)
{
for (j = 0 ; j < columns ; j++)
{
if ( matrix[max_i][max_j] < matrix[i][j] ) max_i = i, max_j = j;
}
}
int tmp = matrix[rows-1][columns-1];
matrix[rows-1][columns-1] = matrix[max_i][max_j];
matrix[max_i][max_j] = tmp;
You can generate the matrix with the requested feature this way
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, j, rows, columns, largest, largestRow, largestColumn, swapValue;
int **matrix;
printf("write the number of rows\n");
scanf("%d", &rows);
printf("write the number of columns\n");
scanf("%d", &columns);
largest = 0;
largestRow = 0;
largestColumn = 0;
matrix = malloc(rows * sizeof *matrix);
if (matrix == NULL)
return -1;
for (i = 0 ; i < rows ; i++)
{
matrix[i] = malloc(columns * sizeof(int));
if (matrix[i] == NULL)
{
int k;
for (k = i - 1 ; k >= 0 ; k--)
free(matrix[k]);
free(matrix);
return -1;
}
for (j = 0 ; j < columns ; j++)
{
matrix[i][j] = rand() % 100;
if (matrix[i][j] > largest)
{
largestRow = i;
largestColumn = j;
largest = matrix[i][j];
}
}
}
swapValue = matrix[rows - 1][columns - 1];
matrix[rows - 1][columns - 1] = largest;
matrix[largestRow][largestColumn] = swapValue;
for (i = 0 ; i < rows ; i++)
{
for (j = 0 ; j < columns ; j++)
printf("%5d", matrix[i][j]);
printf("\n");
}
for (i = 0 ; i < rows ; i++)
free(matrix[i]);
free(matrix);
return 0;
}

A program to find if the sum of the elements in the middle three rows equals to sum of elements in the middle three columns of matrix print

I am trying to write a C program to find if the sum of the elements in the middle three rows equals to sum of elements in the middle three columns of matrix print. So far am able to find the sum of all the columns and rows in the matrix.
#include<stdio.h>
void main()
{
static int array[10][10];
int i, j, m, n,sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
if (m>=5&& n>=5)
{
printf("Enter the elements of the matrix\n");
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
scanf("%d", &array[i][j]);
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
sum = sum + array[i][j] ;
}
printf("Sum of the %d row is = %d\n", i, sum);
sum = 0;
}
sum = 0;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
sum = sum + array[i][j];
}
printf("Sum of the %d column is = %d\n", j, sum);
sum = 0;
}
}
else
{
printf("The matrix should be a 5 by 5 or bigger");
}
}
Try this
int mid1 = (m-3)/2;
int mid2 = (n-3)/2;
int sum1=0,sum2=0;
//suppose m is 9 index(0-8), so this for loop will add the index 3,4,5
for(i=mid1; i<mid1+3; i++)
{
for(j=0; j<n; j++)
{
sum1+=array[i][j];
}
}
for(i=0; i<m; i++)
{
for(j=mid2; j<mid2+3; j++)
{
sum2+=array[i][j];
}
}
if(sum1==sum2)
//equal
else
//not equal
Let's assume that the array has n rows and m columns. In this case you can calculate the sums of middle three rows and columns the following way
if ( n >= 3 && m >= 3 )
{
int k = ( n - 3 ) / 2;
int l = ( m - 3 ) / 2;
int cols_sum = 0;
int rows_sum = 0;
for ( i = k; i < k + 3; i++ )
{
for ( j = 0; j < m; j++ ) rows_sum += array[i][j];
}
for ( i = 0; i < n; i++ )
{
for ( j = l; j < l + 3; j++ ) cols_sum += array[i][j];
}
if ( rows_sum == cols_sum ) /* print appropriate message */;
}
Here is a demonstrative program
#include <stdio.h>
#define N 3
#define M 3
#define RANGE 3
int main( void )
{
int array[N][M] =
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
size_t n = N;
size_t m = N;
if ( n >= RANGE && m >= RANGE )
{
size_t k = ( n - RANGE ) / 2;
size_t l = ( m - RANGE ) / 2;
size_t i, j;
int cols_sum = 0;
int rows_sum = 0;
for ( i = k; i < k + RANGE; i++ )
{
for ( j = 0; j < m; j++ ) rows_sum += array[i][j];
}
for ( i = 0; i < n; i++ )
{
for ( j = l; j < l + RANGE; j++ ) cols_sum += array[i][j];
}
if ( rows_sum == cols_sum )
{
printf( "The sums of three middle rows and columns are equal "
"each other and have value %d\n", rows_sum );
}
else
{
printf( "The sums of three middle rows and columns are not equal "
"each other.\n"
"The sum of the rows has value %d "
"and the sum of the columns has value %d\n",
rows_sum, cols_sum );
}
}
return 0;
}
The output is
The sums of three middle rows and columns are equal each other and have value 45
Now all what you need is to provide the appropriate dimensions of the array and the user input of the data.
In you code you are asking user for row and column count, but you have already decided on that, moreover you cannot create a array dynamically with user input. However, there are ways you can work with to create arrays based on users request, we have Dynamic Memory Allocation concept to the rescue. For for time being, work with predefined size.
How to get row Data
In c, you have to fix on a row as i, in array[i][j] i is row and j is column reference, and loop through j to get all the row element values
How to get Column Data
same as above, loop through i and fix on j, exactly opposite.
#include<stdio.h>
int main()
{
/*This code works for only odd numbers and you have to make necessary changes to accommodate for even number of rows and columns*/
const int row = 5, colmn = 5;
static int array[row][colmn];
int i, j, m, n,sumRow = 0, sumColmn = 0;
int middleRow = row / 2, middleColmn = colmn / 2;
int howManyRows = 3;
printf("Enter the elements of the matrix\n");
/*
* first get elements from user
*/
for (i = 0; i < row; ++i)
{
for (j = 0; j < colmn; ++j)
{
printf("[%d][%d]",i,j);
scanf("%d", &array[i][j]);
}
}
/*
* printing elements to cross check the output
*/
for (i = 0; i < row; ++i)
{
for (j = 0; j < colmn; ++j)
{
printf("%d\t",array[i][j]);
}
printf("\n");
}
/*
* logic : get rows to adds (outer loop will loop through the rows to consider)
* inner loop is to get all the elements in each row
*/
for(int threeRows = 0 - howManyRows/2 ; threeRows <= howManyRows/2 ;threeRows++)
{
for (j = 0; j < row; ++j)
{
sumRow += array[j][middleColmn + threeRows];
}
}
/*
* its the same as above just the opposite
*/
for(int threeColmn = 0 - howManyRows/2; threeColmn <= howManyRows/2 ;threeColmn++)
{
for (j = 0; j < row ; ++j)
{
sumColmn += array[middleColmn + threeColmn][j];
}
}
printf("middleRow = %d middleColmn = %d sumRow = %d sumColmn = %d",middleRow, middleColmn,sumRow,sumColmn);
if(sumRow == sumColmn) printf("\nThey are equal");
else printf("\nnot equal");
return 0;
}

Can't get matrix printed in to a file

I'm having a problem with matrix.
When i'm trying to print the last matrix ( Invertible matrix ) to file ( matrixrez.txt) program prints only first column.
Why?
Matrix.txt:
1 0 1 1 2 0
1 0 2 0 3 0
2 1 1 1 2 3
Here's my code:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file, *outf;
int matrixA[3][3], matrixB[3][3], a[3][3];
int trash[3];
int i, j, k, sum;
float determinant = 0;
i = j = k = sum = 0;
file = fopen("matrix.txt", "rt");
outf = fopen("matrixrez.txt", "w+");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
fscanf(file, "%d", &matrixA[i][j]);
}
for (k = 0; k < 3; k++) fscanf(file, "%d", &trash[k]);
}
fseek(file, 0, SEEK_SET);
for (i = 0; i < 3; i++) {
for (k = 0; k < 3; k++) fscanf(file, "%d", &trash[k]);
for (j = 0; j < 3; j++) {
fscanf(file, "%d", &matrixB[i][j]);
}
}
/* Matrix multiplication */
for ( i = 0 ; i < 3 ; i++ )
{
for ( j = 0 ; j < 3 ; j++ )
{
for ( k = 0 ; k < 3 ; k++ )
{
sum = sum + matrixA[i][k]*matrixB[k][j];
}
a[i][j] = sum;
sum = 0;
}
}
for(i=0;i<3;i++)
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf("%d ", matrixA[i][j]);
}
}
printf("\n");
for (i = 0; i < 3; i++) {
printf("\n");
for (j = 0; j < 3; j++) {
printf("%d ", matrixB[i][j]);
}
}
printf("\n");
printf("The resultant matrix is:: \n");
fprintf(outf,"The resultant matrix is:: \n");
for (i = 0; i < 3; i++) {
printf("\n");
fprintf(outf,"\n");
for (j = 0; j < 3; j++) {
printf("%d ", a[i][j]);
fprintf(outf,"%d ", a[i][j]);
}
}
printf("\n\n Inversion of Matrix: \n");
fprintf(outf,"\n\n Inversion of Matrix: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3] [(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"\n");
printf("\n");
}
printf("\n");
system("pause");
return 0;
}
I get matrixrez.txt:
The resultant matrix is::
2 4 3
3 6 6
3 9 3
Inversion of Matrix:
4.00
-1.67
-0.67
I think something wrong is with line at the end of a file (if I'm not mistaken) :
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
You need to put braces around what you meant to have as your for loop block at the end:
printf("\n\n Inversion of Matrix: \n");
fprintf(outf,"\n\n Inversion of Matrix: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
{ /* YOU NEED THIS BRACE */
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3] [(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
} /* AND YOU NEED THIS BRACE */
fprintf(outf,"\n");
printf("\n");
}
You do not have brackets around the inner for loop. So, it loops through i just fine but each time it cycles through j without calling the fprintf just the printf
The code should be
printf("\n\n Inversion of Matrix: \n");
fprintf(outf,"\n\n Inversion of Matrix: \n");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3] [(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"%.2f\t",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
fprintf(outf,"\n");
printf("\n");
}
}

Resources