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.
Related
The program is to accept a matrix and an integer and replace all the elements with asterisk('*') other than the elements present in upper left and right triangle and lower right and left triangle of size k.
code:
#include <stdio.h>
int main()
{
int n, x, i, j;
// Inputs
printf("n: ");
scanf("%d\n", &n);
char a[n][n], b[n][n];
printf("Enter the matrix elements\n");
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("[%d][%d]: ", i, j);
scanf("%c\n", &a[i][j]);
b[i][j] = '*';
}
}
printf("x: ");
scanf("%d\n", &x);
printf("x: %d\n",x);
// Implementation
// Top right and left triangles
for (i = 0; i < n; i++)
{
for (j = 0; j < x - i; j++)
{
b[i][j] = a[i][j];
}
for (j = n - 1; j >= (n - x) + i; j--)
{
b[i][j] = a[i][j];
}
}
// Bottom right and left triangles
int c = 0;
for (i = n - 1; i >= (n - x); i--)
{
for (j = 0; j < x - c; j++)
{
b[i][j] = a[i][j];
}
for (j = n - 1; j >= (n-x) + c; j--)
{
b[i][j] = a[i][j];
}
c++;
}
// Printing the mat
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%c ", b[i][j]);
}
printf("\n");
}
return 0;
}
The program works for inputs 1 through 8... I didn't try for inputs past 8 because its was tedious enough to type 64 elements, but when I submitted the program it showed that for test case of matrix size 50 the result was an error, specifically 'utf-8' codec can't decode byte 0x87 in position 377: invalid start byte
The program works fine for inputs 1 to 8. Any idea why this is happening?
The code is supposed to take inputs to form a 3x3 Matrix and then multiply each term by the diagonal element of that line, but, for some reason that i don't know, it multiplies two times by the diagonal when the column index is bigger than the row index.
#include <stdio.h>
#define R 3
int a[R][R], i, j;
int main(void) {
for (i = 0; i < R; i++) {
for (j = 0; j < R; j++) {
printf("\nInsira o n%i%i ", i, j);
scanf("%i", &a[i][j]);
}
}
for (i = 0; i < R; i++) {
for (j = 0; j < R; j++) {
a[i][j] = a[i][j] * a[i][i];
}
}
for (i = 0; i < R; i++) {
printf("\n");
for (j = 0; j < R; j++) {
printf("%i ", a[i][j]);
}
}
}
input:
9 8 7
6 5 4
3 2 1
output:
81 648 567
30 25 100
3 2 1
The diagonal value for a given row is being changed before that row has been fully multiplied, so once the column goes past the diagonal, the multiplies are using the new value of that diagonal rather than the old value.
You can fix it (and improve the speed) as follows:
for (i = 0; i < R; i++) {
int tmp = a[i][i];
for (j = 0; j < R; j++) {
a[i][j] *= tmp;
}
}
Also, as mentioned, both i and j should be local variables.
Write a program which will accept 2-dimensional square matrix and find out the transpose of it.
Program should not make use of another matrix
Hi I am trying to transpose a 2*2 matrix without using another matrix.
Is there anything wrong with my transpose logic?
I am a newbie
#include <stdio.h>
int main()
{
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1); //i+1 so that it can print 1 row, 2 row, 3 row etc
for (j = 0; j < 2; j++) { //loop inside to loop to get value for a[0][0],a[0][1],a[0][2]
scanf("%d", &mat[i][j]);
}
}
printf("The matrix\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
//transpose logic using same matrix
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
printf("The transpose of the matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
}
EDIT: I found an easier way to do it however I still don't understand why my transpose logic by using this
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
cannot get it to transpose.
Below is my corrected answer
#include <stdio.h>
int main()
{
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1);//i+1 so that it can print 1 row, 2 row, 3 row etc
for (j = 0; j < 2; j++) {//loop inside to loop to get value for a[0][0],a[0][1],a[0][2]
scanf("%d", &mat[i][j]);
}
}
printf("The matrix\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
printf("The transpose of the matrix is\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[j][i]);
}
printf("\n");
}
}
Your program is a good attempt, but transposing the matrix is like reversing an array: you must stop half way to avoid swapping the transposed values twice, leading to the original matrix as you observe.
You should stop the inner loop when j == i, hence change the inner loop to:
for (j = 0; j < i; j++) { // j < i instead of j < 2
Here is a modified version:
#include <stdio.h>
int main() {
int mat[2][2];
int i, j, temp;
for (i = 0; i < 2; i++) {
printf("\nEnter elements of %d row of first matrix: ", i + 1);
for (j = 0; j < 2; j++) {
if (scanf("%d", &mat[i][j]) != 1)
return 1;
}
}
printf("The matrix:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
//transpose logic using same matrix
for (i = 0; i < 2; i++) {
for (j = 0; j < i; j++) {
temp = mat[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = temp;
}
}
printf("The transpose of the matrix is:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
return 0;
}
Your corrected answer does not transpose the matrix at all, it merely outputs the transposed matrix. The matrix mat in memory is unchanged.
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.
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");
}
}