How to multiply matrix using pointers - C - c

I have to write a program which multiply matrix using pointers.
My program does it correctly, but there are two problems:
1) I can do only one multiplication - when I want next one there is error
2) when I input data of first and second matrix i have to put one more
char to make my program work.
What should I change in my code to solve those problems?
#include <stdio.h>
#include <stdlib.h>
while (z--) { // the number of sets
scanf ("%d %d", &n1, &m1); // first matrix n1 x m1
int **A = malloc(n1 * sizeof(int*));
for (i = 0; i < n1; i++) {
*(A+i) = malloc(m1 * sizeof(int));
}
for (i = 0; i < n1; i++) {
for (j = 0; j < m1; j++) {
scanf("%d ", (A+i*m1+j));
}
}
scanf("%d %d", &n2, &m2); // second matrix n2 x m2
int **B = malloc(n2 * sizeof(int*));
for (i = 0; i < n2; i++) {
*(B+i) = malloc(m2 * sizeof(int));
}
for (i = 0; i < n2; i++) {
for (j = 0; j < m2; j++) {
scanf("%d ", (B+i*m2+j));
}
}
if (m1 != n2) {
printf ("ERROR\n");
} else {
int **C = malloc(n1 * sizeof(int*)); //matrix with product
for (i = 0; i < n1; i++) {
*(C+i) = malloc(m2 * sizeof(int));
}
for (i=0; i<n1; i++) {
for (j=0; j<m2; j++) {
*(C+i*m2+j) = 0;
}
}
for (k = 0; k < n2; k++) {
for (i = 0; i < n1; i++) {
for (j = 0; j < m2; j++) {
C1 = 0;
A1 = *(A+i*m1+k);
B1 = *(B+k*m2+j);
C1 = (C1+A1*B1)%1000;
temp = *(C+i*m2+j);
*(C+i*m2+j) = temp+C1;
}
}
}
for (i = 0; i < n1; i++) {
for (j = 0; j < m2; j++) {
printf("%d ", *(C+i*m2 +j));
}
printf("\n");
}
for (i = 0; i < n1; i++) { // free third matrix
free(*(C+i));
}
free(C);
}
for (i = 0; i < n1; i++) { // free first matrix
free(*(A+i));
}
free(A);
for (i = 0; i < n2; i++) { // free second matrix
free(*(B+i));
}
free(B);
}
return 0;

You write to memory beyond the allocated size. This invokes undefined behavior, a good explanation for the erratic behavior you observe.
The offending code is this:
for (k=0; k<n2; k++){
for (i=0; i<n1; i++) {
for (j=0; j<m2; j++) {
C1=0;
A1=*(A+i*m1+k);
B1=*(B+k*m2+j);
C1=(C1+A1*B1)%1000;
temp=*(C+i*m2+j);
*(C+i*m2+j)=temp+C1;
}
}
}
You assume that the matrices are allocated as a single packed 2D array.
Since you allocate them as arrays of pointers to arrays of int, you cannot use this code. Use the much simpler version below:
for (k = 0; k < n2; k++){
for (i = 0; i < n1; i++) {
for (j = 0; j < m2; j++) {
C[i][j] += A[i][k] * B[k][j] % 1000;
}
}
}
It is not clear why you compute modulo 1000. If you really mean that, you should use this:
C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % 1000;
If you are not supposed to use the [] syntax, replace it with the equivalent for pointer dereferencing, not 2D array addressing. I understand what they are trying to teach you, but a diagram would be more effective than this.
*(*(C+i)+j) = (*(*(C+i)+j) + *(*(A+i)+k) * *(*(B+k)+j)) % 1000;
As you may have noticed, the [] syntax is the same for 2D arrays and arrays of pointers to arrays, but the code generated for pointer arithmetic and dereferencing is quite different.
There are other places where you need to change the indexing method, here is the corrected code:
while (z--) { // the number of sets
scanf("%d %d", &n1, &m1); // first matrix n1 x m1
int **A = malloc(n1 * sizeof(int*));
for (i = 0; i < n1; i++) {
*(A+i) = malloc(m1 * sizeof(int));
for (j = 0; j < m1; j++) {
scanf("%d ", *(A+i)+j);
}
}
scanf("%d %d", &n2, &m2); // second matrix n2 x m2
int **B = malloc(n2 * sizeof(int*));
for (i = 0; i < n2; i++) {
*(B+i) = malloc(m2 * sizeof(int));
for (j = 0; j < m2; j++) {
scanf("%d ", *(B+i)+j);
}
}
if (m1 != n2) {
printf ("ERROR\n");
} else {
int **C = malloc(n1 * sizeof(int*)); //matrix with product
for (i = 0; i < n1; i++) {
// using calloc so matrix is initialized to 0
*(C+i) = calloc(m2, sizeof(int));
}
for (k = 0; k < n2; k++) {
for (i = 0; i < n1; i++) {
for (j = 0; j < m2; j++) {
*(*(C+i)+j) = (*(*(C+i)+j) + *(*(A+i)+k) * *(*(B+k)+j)) % 1000;
}
}
}
for (i = 0; i < n1; i++) {
for (j = 0; j < m2; j++) {
printf("%d ", *(*(C+i)+j));
}
printf("\n");
}
for (i = 0; i < n1; i++) { // free third matrix
free(*(C+i));
}
free(C);
}
for (i = 0; i < n1; i++) { // free first matrix
free(*(A+i));
}
free(A);
for (i = 0; i < n2; i++) { // free second matrix
free(*(B+i));
}
free(B);
}
return 0;

Related

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.

Gauss-Jacobi iteration method

I'm trying to write a programm that solves system of equations Ax=B using Gauss-Jacobi iteration method.
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
int main(void) {
double **a, *b, *x, *f, eps = 1.e-2, c;
int n = 3, m = 3, i, j, bool = 1, d = 3;
/* printf("n=") ; scanf("%d", &n);
printf("m=") ; scanf("%d", &n) */
a =malloc(n * sizeof *a);
for (i = 0; i < n; i++)
a[i] = (double*)malloc(m * sizeof(double));
b = malloc(m * sizeof *b);
x = malloc(m * sizeof *x) ;
f = malloc(m * sizeof *f) ;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("a[%d][%d]=", i, j);
scanf("%le", &a[i][j]);
if(fabs(a[i][i])<1.e-10) return 0 ;
}
printf("\n") ;
}
printf("\n") ;
for (i = 0; i < n; i++) {
for (j = 0; j < m; j++) {
printf("a[%d][%d]=%le ", i, j, a[i][j]);
}
printf("\n") ;
}
for (j = 0; j < m; j++) {
printf("x[%d]=", j);
scanf("%le", &x[j]);
} //intial guess
printf("\n") ;
for (j = 0; j < m; j++) {
printf("b[%d]=", j);
scanf("%le", &b[j]);
}
printf("\n") ;
while (1) {
bool = 0;
for (i = 0; i < n; i++) {
c = 0.0;
for (j = 0; j < m; j++)
if (j != i)
c += a[i][j] * x[j];
f[i] = (b[i] - c) / a[i][i];
}
for (i = 0; i < m; i++)
if (fabs(f[i] - x[i]) > eps)
bool = 1;
if (bool == 1)
for (i = 0; i < m; i++)
x[i] = f[i];
else if (bool == 0)
break;
}
for (j = 0; j < m; j++)
printf("%le\n", f[j]);
return 0;
}
The condition of stoping the loop is that previous approximation minus current approximation for all x is less than epsilon.
It seems like i did everything according to algorithm,but the programm doesn't work.
Where did i make a mistake?
While not the most strict condition, the usual condition requiered to guarantee convergence in the Jacobi and Gauss-Seidel methods is diagonal dominance,
abs(a[i][i]) > sum( abs(a[i][j]), j=0...n-1, j!=i)
This test is also easy to implement as a check to run before the iteration.
The larger the relative gap in all these inequalities, the faster the convergence of the method.

Why does my C program that is supposed to output a matrix to the power of n output my matrix to the power of 2^n?

My code is supposed to take in a matrix M and raise it to the power of an integer A. However, somehow, my output is always M^(2^A). For example, if I want to find a matrix in its 3rd power, I will instead receive its 8th power.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void multiply(int ** p, int pwr, int dim, int ** prod) {
int m, i, j, k;
/*if (n<pwr){*/
int pos = 0;
for (m = 0; m < pwr; m++) {
for (i = 0; i < dim; i++) {
for (j = 0; j < dim; j++) {
for (k = 0; k < dim; k++) {
pos += p[i][k] * p[k][j];
}
prod[i][j] = pos;
pos = 0;
}
}
for (i = 0; i < dim; i++) {
for (j = 0; j < dim; j++) {
p[i][j] = prod[i][j];
prod[i][j] = 0;
}
}
}
/*n=n+1;
multiply(prod, q, pwr, dim, prod);
}*/
}
int main(int argc, char * argv[]) {
FILE * fp = fopen(argv[1], "r");
int dim, pwr, i, j;
fscanf(fp, "%d", & dim);
int ** matrix;
matrix = (int ** ) malloc(dim * sizeof(int * ));
for (i = 0; i < dim; i++) {
matrix[i] = (int * ) malloc(dim * sizeof(int));
}
int ** prod;
prod = (int ** ) malloc(dim * sizeof(int * ));
for (i = 0; i < dim; i++) {
prod[i] = (int * ) malloc(dim * sizeof(int));
}
for (i = 0; i < dim; i++) {
for (j = 0; j < dim; j++) {
fscanf(fp, "%d", & matrix[i][j]);
}
}
fscanf(fp, "%d", & pwr);
if (pwr == 1) {
for (i = 0; i < dim; i++) {
for (j = 0; j < dim; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
} else if (pwr >= 2) {
multiply(matrix, pwr, dim, prod);
for (i = 0; i < dim; i++) {
for (j = 0; j < dim; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}
return 0;
}
You are multiplying your matrix by itself and then store the result in the original one. Then you do it again.
So perfectly normal that it gets powered 8 times. What you need is another temporary matrix on which you store the result and keep the original matrix to multiply your result with.

Swapping rows in Gauss Jordan method

Suppose in Gauss Elimination Method the first value of matrix, A[0][0]=0
Then how can I swap row 1 of matrix "A" with row 2, so that i get the correct result ?
Assuming a simpel 2d-array build the way as shown below: just exchange the rows.
#include <stdio.h>
#include <stdlib.h>
#define S_MATRIX_DIM 5
int main()
{
int **A;
int *tmp;
int i, j, entry;
entry = 0;
A = malloc(S_MATRIX_DIM * sizeof(int *));
for (i = 0; i < S_MATRIX_DIM; i++) {
A[i] = malloc(S_MATRIX_DIM * sizeof(int));
for (j = 0; j < S_MATRIX_DIM; j++) {
A[i][j] = entry++;
}
}
puts("Matrix A =");
for (i = 0; i < S_MATRIX_DIM; i++) {
for (j = 0; j < S_MATRIX_DIM; j++) {
printf("%d,", A[i][j]);
}
putchar('\n');
}
tmp = A[0];
A[0] = A[1];
A[1] = tmp;
puts("Matrix A, row exchanged =");
for (i = 0; i < S_MATRIX_DIM; i++) {
for (j = 0; j < S_MATRIX_DIM; j++) {
printf("%d,", A[i][j]);
}
putchar('\n');
}
for (i = 0; i < S_MATRIX_DIM; i++) {
free(A[i]);
}
free(A);
exit(EXIT_SUCCESS);
}
keep a temp value from the same type;
swap value by value,
example:
temp=a[i][j];
a[i][j]=a[i+1][j];
a[i+1][j]=temp;

Resources