multiplication of matrices using while loop in c - c

here below i have given multiplication of matrices in c language using for loop but can any help me make a more simplified version or can any help me make it using while loop
i want a simplified version
i want a code in while loop
:) just learning
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[10][10], b[10][10], mul[10][10], r, c, i, j, k;
system("cls");
printf("enter the number of row=");
scanf("%d", &r);
printf("enter the number of column=");
scanf("%d", &c);
printf("enter the first matrix element=\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("enter the second matrix element=\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d", &b[i][j]);
}
}
printf("multiply of the matrix=\n");
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
mul[i][j] = 0;
for (k = 0; k < c; k++)
{
mul[i][j] += a[i][k] * b[k][j];
}
}
}
//for printing result
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t", mul[i][j]);
}
printf("\n");
}
return 0;
}

It will not make code simpler only harder to read.
One of the loops example:
printf("multiply of the matrix=\n");
i = 0;
while (i < r)
{
j = 0;
while(j < c)
{
mul[i][j] = 0;
k = 0;
while(k < c)
{
mul[i][j] += a[i][k] * b[k][j];
k++;
}
j++;
}
i++;
}

Related

How to correctly print the result of matrices multiplication in different cases?

I'm making a program that multiplies 3 matrices and print the outcome. The program can input several cases of multiplication and each case can determined their own number of NxN matrix (n). However, if I input 2 cases, first with n=2 and second with n=3, the output of the first case will have a 3x3 matrix with row 3 and column 3 only have 0s. How do I fix this problem?
#include <stdio.h>
int main(){
int t, n, i, j, k, l, a[50][50][10], b[50][50][10], c[50][50][10], d[50][50][10], e[50][50][10];
scanf ("%d", &t);
for (l=1; l<=t; l++){
scanf("%d", &n);
// matrix a
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & a[i][j][l]);
}
}
// matrix b
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & b[i][j][l]);
}
}
// matrix c
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", & c[i][j][l]);
}
}
//Multiplication
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
d[i][j][l] = 0;
for (k = 0; k < n; k++) {
d[i][j][l] += a[i][k][l] * b[k][j][l];
}
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
e[i][j][l] = 0;
for (k = 0; k < n; k++) {
e[i][j][l] += d[i][k][l] * c[k][j][l];
}
}
}
}
//Printing the product
for (l=1; l<=t; l++){
printf ("Case #%d:\n", l);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d ", e[i][j][l]);
}
printf("\n");
}
}
return 0;
}
This is the expected output.

Expression must have arithmetic type issue

I need to multiply two square matrixes A and B 15x15.
Unfortunately, I'm getting this kind of error.
I know the problem is in pointers while calculating matrix C.
C[i][j] += *(A + k) * *(B + k)
I hope you can explain me what's wrong. I'm a beginner xD.
Thank you in advance.
#include <stdio.h>
#define N 15
#define _CRT_SECURE_NO_WARNINGS
int main() {
int A[N][N];
int B[N][N];
int C[N][N];
printf("Input matrix A.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("Enter your element:\n");
scanf_s("%d", &A[i][j]);
}
printf("\n");
}
printf("Input matrix B.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("Enter your element:\n");
scanf_s("%d", &B[i][j]);
}
printf("\n");
}
printf("Matrix A.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", A[i][j]);
}
printf("\n");
}
printf("Matrix B.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", B[i][j]);
}
printf("\n");
}
for (int i = 0; i < 15; i++) {
for (int j = 0; j < 15; j++) {
C[i][j] = 0;
for (int k = 0; k < 14; k++) {
C[i][j] += *(A + k) * *(B + k);
k++;
}
}
}
printf("Your result:\n");
printf("Matrix C.\n");
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", C[i][j]);
}
printf("\n");
}
return 0;
}
The problem in the multiplication is that A+k and B+k have type int (*)[15] which means dereferencing it once only makes a pointer out of them; furthermore, you need to take row and column items individually, which means A[i][k] and B[k][j], right? (also, there's no point on using confusing syntax, as the underlying operation is exactly the same).
Here's a fixed and improved version:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define N 15
/* Improvement 1 (type abstraction) */
typedef int NxN_int_matrix[N][N];
/* Improvement 2 (input function & wrapper) */
#define input_matrix(var) input_matrix_ex((var), #var)
static void input_matrix_ex(NxN_int_matrix dst, char *name)
{
printf("Input matrix %s.\n", name);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
/* Improvement 3 (nicer prompt) */
printf("%s[%2d][%2d]: ", name, i, j);
fflush(stdout);
scanf_s("%d", &dst[i][j]);
}
}
printf("\n");
}
/* Improvement 4 (print function) */
#define print_matrix(var) print_matrix_ex(#var, (var))
static void print_matrix_ex(char *name, NxN_int_matrix M)
{
printf("Matrix %s.\n", name);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d\t", M[i][j]);
}
printf("\n");
}
}
/* Improvement 5 (move multiplication to a function too, and fix it) */
static void mult_matrix(NxN_int_matrix dst, NxN_int_matrix a, NxN_int_matrix b)
{
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
/* Improvement 6 (don't write out intermediate values) */
int tmp = 0;
for (int k = 0; k < N; k++)
tmp += a[i][k] * b[k][j];
dst[i][j] = tmp;
}
}
}
int main()
{
NxN_int_matrix A, B, C;
input_matrix(A);
input_matrix(B);
print_matrix(A);
print_matrix(B);
mult_matrix(C, A, B);
printf("Your result:\n");
print_matrix(C);
return 0;
}
/* Possible further improvements:
* - using a transposed B might make multiplication faster
*/

How to transpose a matrix without using another matrix?

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.

What's wrong with this code for multiplying two matrices in C?

The aim of the matrixMultiplier function is to multiply A * B, and then show the resulting matrix C, for any two square matrices A and B. A and B sizes are limited by 10*10.
When the user is asked the size of the matrix they wish to introduce, although A and B are 10*10, the function only works with the submatrices in A and B up to the dimension which the user has specified, say 3*3.
I've tested this out multiplying the 3*3 identity matrix by itself and it's not giving me the correct answer.
#include <stdio.h>
void matrixMultiplier(int A[][10], int B[][10], int C[][10], int n) {
int i, j, k;
for (i = 0; i < n; i++) {
for (k = 0; k < n; k++) {
C[i][k] = 0; /* Initialize output matrix to zero */
}
}
for (i = 0; i < n; i++) {
for (k = 0; k < n; k++) {
for (j = 0; j < n; j++) {
C[i][k] += A[i][j] * B[j][k];
}
}
}
printf("\n");
for (i = 0; i < n; i++) {
for (k = 0; k < n; k++) {
printf("%4d", C[i][k]);
}
printf("\n");
}
}
int main(void) {
int A[][10] = {{0}};
int B[][10] = {{0}};
int C[][10] = {{0}}; /* Initialize output matrix to zero */
int i, j;
int n;
printf("Enter square matrix dimension: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("Assign a value: ");
scanf("%d", &A[i][j]);
}
printf("New row.\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%4d", A[i][j]);
}
printf("\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("Assign a value: ");
scanf("%d", &B[i][j]);
}
printf("New row.\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%4d", B[i][j]);
}
printf("\n");
}
matrixMultiplier(A, B, C, n);
return 0;
}
You need to use statically allocated arrays like this:
int A[10][10] = {{0}};
int B[10][10] = {{0}};
int C[10][10] = {{0}}; /* Initialize output matrix to zero */
As other commentators noticed, in C int A[][10] = {{0}}; is basically an equivalent of int A[1][10] = {{0}};, so only first rows are correctly set.
Take a look at this code:
#include <stdio.h>
#define MAX_SIZE 10
void matrixMultiplier(int A[][MAX_SIZE], int B[][MAX_SIZE], int C[][MAX_SIZE], int n)
{
int i, j, k;
for (i = 0; i < n; i++)
for (k = 0; k < n; k++)
for (j = 0; j < n; j++)
C[i][k] += A[i][j] * B[j][k];
printf("\nProduct Matrix C:\n\n");
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
printf("%4d", C[i][j]);
putchar('\n');
}
}
int main(void)
{
//int A[][10] = {{0}}; By initializing like this you are creating this int A[1][10] = {{0}};
int A[MAX_SIZE][MAX_SIZE] = {0};
int B[MAX_SIZE][MAX_SIZE] = {0};
int C[MAX_SIZE][MAX_SIZE] = {0};
int i, j, n;
printf("Enter square matrix dimension: ");
scanf("%d", &n);
printf("Input %d values row-wise, for matrix A: ", n*n);
for(i = 0; i < n; ++i)
for(j = 0; j < n; ++j)
scanf("%d", &A[i][j]);
printf("Input %d values row-wise, for matrix B: ", n*n);
for(i = 0; i < n; ++i)
for(j = 0; j < n; ++j)
scanf("%d", &B[i][j]);
printf("\nGiven Matrix A:\n\n");
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
printf("%4d", A[i][j]);
putchar('\n');
}
printf("\nGiven Matrix B:\n\n");
for(i = 0; i < n; ++i)
{
for(j = 0; j < n; ++j)
printf("%4d", A[i][j]);
putchar('\n');
}
matrixMultiplier(A, B, C, n);
return 0;
}
If you need more explanation about 2D arrays refer to this link

2D Array rotation in C by 90

I wrote this code to rotate a square matrix by 90 degrees. but it started to show me runtime error.
I have absolutely no clue why i get that notice.Can someone please fix the code for me.
It shows me segmentation fault.i have no clue what that means.
#include <stdio.h>
int main()
{
int N, i = 0, j = 0;
scanf("%d", &N);
int A[N][N], B[N][N], temp;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
scanf("%d", (A[i][j]));
}
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
B[i][j] = A[j][i];
}
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
temp = B[i][j];
B[i][j] = B[N - i - 1][j];
B[N - i - 1][j] = temp;
}
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
printf("%d", B[i][j]);
}
}
return 0;
}
replace this scanf("%d",(A[i][j])); with this scanf("%d",&A[i][j]);

Resources