getting address as output when multiplying two matrices - c

hi I have a question about multiplying two matrices
This is the code that I wrote
#include <stdio.h>
void getData(int matrix1[20][20], int matrix2[20][20], int row1, int row2, int col1, int col2);
void matrixMult(int matrix1[20][20], int matrix2[20][20], int matrix3[20][20], int row1, int row2, int col1, int col2);
int main(void)
{
// global declarations
int matrix1[20][20];
int matrix2[20][20];
int matrix3[20][20];
int row1;
int row2;
int col1;
int col2;
printf("Enter first matrix dimension: ");
scanf("%d %d", &row1, &col1);
printf("%d x %d\n", row1, col1);
printf("Enter first matrix dimension: ");
scanf("%d %d", &row2, &col2);
printf("%d x %d\n", row2, col2);
printf("\n");
if(col1 == row2)
{
getData(matrix1, matrix2, row1, row2, col1, col2);
matrixMult(matrix1, matrix2, matrix3, row1, row2, col1, col2);
}
return 0;
}
void getData(int matrix1[20][20], int matrix2[20][20], int row1, int row2, int col1, int col2)
{
// local declarations
int i = 0;
int j = 0;
printf("Enter number by row and column: ");
printf("\n");
// input first matrix
for(i = 0; i < row1; i++)
for(j = 0; j < col1; j++)
{
scanf("%d", &matrix1[i][j]);
}
// reset i and j
i = 0;
j = 0;
printf("Enter number by row and column: ");
printf("\n");
// intput second matrix
for(i = 0; i < row2; i++)
for(j = 0; j < col2; j++)
{
scanf("%d", &matrix1[i][j]);
}
return;
}
void matrixMult(int matrix1[20][20], int matrix2[20][20], int matrix3[20][20], int row1, int row2, int col1, int col2)
{
int i = 0;
int j = 0;
int k = 0;
int sum = 0;
for(i = 0; i < row1; i++)
{
for(j = 0; j < col2; j++)
{
for (k= 0; k < row2; k++)
{
sum += matrix1[i][k] * matrix2[k][j];
}
matrix3[i][j] = sum;
sum = 0; // set sum to 0
}
}
//reset i and j
i = 0;
j = 0;
printf("product of matrix 1 and 2: ");
printf("\n");
for(i = 0; i < row1; i++)
{
for(j = 0; j < col2; j++)
{
printf("%d\t", matrix3[i][j]);
}
printf("\n");
}
return;
}
the output does not print the value after multiplication instead its print out the address of those values
Enter first matrix dimension: 2 2
2 x 2
Enter first matrix dimension: 2 3
2 x 3
Enter number by row and column:
1 2
2 3
Enter number by row and column:
1 2 3
1 2 4
product of matrix 1 and 2:
40829548 41222770 41615992
40829548 41222770 41615992
what wrong with the code, did i do something wrong when passing matrix1, matrix2 and matrix3 to the matrixMult function ?
thank in advance.
I re-wrote the code in main only, but this time the last column of the product matrix still print out garbage
#include <stdio.h>
int main(void)
{
// global declarations
int matrix1[20][20];
int matrix2[20][20];
int matrix3[20][20];
int row1;
int row2;
int col1;
int col2;
int i = 0;
int j = 0;
int k = 0;
int sum = 0;
printf("Enter first matrix dimension: ");
scanf("%d %d", &row1, &col1);
printf("%d x %d\n", row1, col1);
printf("Enter first matrix dimension: ");
scanf("%d %d", &row2, &col2);
printf("%d x %d\n", row2, col2);
printf("\n");
if(col1 != row2)
{
printf("Matrices cannot be multiply!");
}
else
{
printf("Enter first matrix: ");
printf("\n");
for(i = 0; i < row1; i++)
for(j = 0; j < col1; j++)
scanf("%d", &matrix1[i][j]);
printf("Enter second matrix: ");
printf("\n");
for(i = 0; i < row1; i++)
for(j = 0; j < col1; j++)
scanf("%d", &matrix2[i][j]);
for(i = 0; i < row1; i++)
{
for(j = 0; j < col2; j++)
{
for (k= 0; k < row2; k++)
{
sum += matrix1[i][k] * matrix2[k][j];
}
matrix3[i][j] = sum;
sum = 0; // set sum to 0
}
}
printf("product of matrix 1 and 2: ");
printf("\n");
for(i = 0; i < row1; i++)
{
for(j = 0; j < col2; j++)
{
printf("%d\t", matrix3[i][j]);
}
printf("\n");
}
}
return 0;
}
this is my output
Enter first matrix dimension: 2 2
2 x 2
Enter first matrix dimension: 2 3
2 x 3
Enter first matrix:
1 2
3 4
Enter second matrix:
2 1 4
2 5 6
product of matrix 1 and 2:
10 5 1717986916
22 11 -1717986924
why does the last column print out garbage?

The problem in your code lies here :
printf("Enter number by row and column: ");
printf("\n");
// intput second matrix
for(i = 0; i < row2; i++)
for(j = 0; j < col2; j++)
{
scanf("%d", &matrix1[i][j]);// you were entering the values in matrix1 and leaving the matri2 blank so make it matrix2 instead.
}
Change this :
scanf("%d", &matrix1[i][j]);
to this :
scanf("%d", &matrix2[i][j]);

I do not think what you see printed are addresses: your printf statement seems correct. You have to check the function in which you are filling the matrix, the error is probably there. It can help if you reset the destination matrix at the beginning of your program.
I also suggest that you print your input matrix after reading them.

Related

For C: Why does my Matrix output have the incorrect formatting? (Beginner)

My inputs repeat themselves rather then starting from blank and letting me input the next integer. An additional issue is that I am unable to print the matrix like "100 200" and then "300 400" on the next line. I have tried messing with the newlines and taking out the scan function but nothing seems to work.
#include<stdio.h>
int rows;
int colums;
int i;
int j;
int main() {
int mat[100][100];
printf("Please enter the number of rows: ");
scanf("%d",&rows);
printf("Please enter the number of columns: ");
scanf("%d",&colums);
printf("Enter Matrix A\n");
for(int i = 0; i < rows; ++i){
for(int j = 0; j < colums; ++j) {
scanf("%d", &mat[i][j]);
printf("%d",mat[i][j]);
}
}
printf("Enter Matrix B\n");
int matb[100][100];
for(int i = 0; i < rows; ++i){
for(int j = 0; j < colums; ++j) {
scanf("%d", &matb[i][j]);
printf("%d",matb[i][j]);
}
}
printf("A+B =\n");
for(int i = 0; i < rows; ++i){
for(int j = 0; j < colums; ++j){
int new;
new = mat[i][j]+matb[i][j];
printf("%d",new);
}
}
}
When you're reprinting the numbers after the user enters them, end with a newline so they don't enter the next number on the same line.
When you're printing the sums, put a space after each number, and a newline after each row.
I've annotated the changes below with comments.
#include<stdio.h>
int rows;
int colums;
int i;
int j;
int main() {
int mat[100][100];
printf("Please enter the number of rows: ");
scanf("%d",&rows);
printf("Please enter the number of columns: ");
scanf("%d",&colums);
printf("Enter Matrix A\n");
for(int i = 0; i < rows; ++i){
for(int j = 0; j < colums; ++j) {
scanf("%d", &mat[i][j]);
printf("%d\n",mat[i][j]); // added newline
}
}
printf("Enter Matrix B\n");
int matb[100][100];
for(int i = 0; i < rows; ++i){
for(int j = 0; j < colums; ++j) {
scanf("%d", &matb[i][j]);
printf("%d\n",matb[i][j]); // added newline
}
}
printf("A+B =\n");
for(int i = 0; i < rows; ++i){
for(int j = 0; j < colums; ++j){
int new;
new = mat[i][j]+matb[i][j];
printf("%d ",new); // added space
}
printf("\n"); // added newline
}
}
I was looking through your code and saw the following conceptual errors:
You do declare i,j at the main beginning but also declared locals for each for loop, that’s not necessarily you can choose one way or another,
You cant obtain a printed results by rows because you are printing each value since you have the print statement nested inside both loops, for row printing you have to take out the print of the second loop. Also you have to print newline like was pointed in the other answer.
#include<stdio.h>
int rows;
int colums;
int i;
int j;
int main() {
int mat[100][100];
printf("Please enter the number of rows: ");
scanf("%d",&rows);
printf("Please enter the number of columns: ");
scanf("%d",&colums);
printf("Enter Matrix A\n");
for(i = 0; i < rows; ++i){
for(j = 0; j < colums; ++j) {
scanf("%d", &mat[i][j]);
}
printf("\n");
}
printf("Enter Matrix B\n");
int matb[100][100];
for(i = 0; i < rows; ++i){
for(j = 0; j < colums; ++j) {
scanf("%d", &matb[i][j]);
}
printf("\n");
}
printf("A+B =\n");
int new;
for(i = 0; i < rows; ++i){
for(j = 0; j < colums; ++j){
new = mat[i][j]+matb[i][j];
printf("%d ", new);
}
printf("\n");
}
}
That's it
Output:
> cc test.c -o test.exe && test
Please enter the number of rows: 2
Please enter the number of columns: 2
Enter Matrix A
10 20
30 40
Enter Matrix B
1 2
3 4
A+B =
11 22
33 44

Creating a matrix that has its elements as the sum of the indices of that element

int main() {
int n = 0;
int matrix[n][n];
printf("Insert the order of the matrix:");
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = i + j;
printf("The matrix is:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
}
for a 2x2 matrix the output should be 0 1 1 2, but it is 1 2 1 2, and for a 3x3 matrix it should be 0 1 2 1 2 3 2 3 4 but it shows 2 3 4 2 3 4 2 3 4
The issue is that my output is always just my first row of the matrix, repeated n times. Any help?
#include <stdio.h>
int main() {
int n = 0;
printf("Insert the order of the matrix:");
scanf("%d", &n);
int matrix[n][n];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
matrix[i][j] = i + j;
printf("The matrix is:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
}
}
You declare the array with size n equal zero. Then the length of a row is zero, hence the offset for each consecutive row is zero, too. That means all rows are stored at the same place. As a final result, the last row's values overwrite all the previous rows.
Do input n before declaring matrix[n][n].

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);
}

Multiplication of 2D arrays, using pointers to pointers

I've been trying to implement a program in C, where i have two 2D arrays (created with pointers) and i have to multiply them (like multiplying matrices) and store the results to a third array. The dimensions of the arrays are given by the user. I've done the code but i'm getting wrong results. Is there something wrong with my formula? I wanted to do the reading and printing with functions.
For example, when i input n = 2, m = 2 and k =2. For each element of the matrix i input
A(0)(0) = 1, A(0)(1) = 2, A(1)(0) = 3, A(1)(1) = 4
and
B(0)(0) = 1,> B(0)(1) = 2, B(1)(0) = 3, B(1)(1) = 4.
The output should've been
C(0)(0) = 7, C(0)(1) = 10, C(1)(0) = 15 and C(1)(1) = 22.
Instead the output is
C(0)(0) = 7, C(0)(1) = 10, C(1)(0) = 33, C(1)(1) = 46.
I hope it's not hard to understand, i'm not allowed to post an image yet :(
#include <stdio.h>
#include <stdlib.h>
int **read(int **x, int i, int j);
int **prod(int **x, int **y, int n, int m, int k);
void print(int **x, int r, int c);
int main()
{
int n, m, k, i, j;
printf("Give n, m and k: ");
scanf("%d%d%d", &n, &m, &k);
int **A, **B, **C;
A = (int **)malloc(n*sizeof(int *));
for (i = 0; i < n; i++)
*(A+i) = (int *)malloc(k*sizeof(int));
B = (int **)malloc(k*sizeof(int *));
for (i = 0; i < k; i++)
*(B+i) = (int *)malloc(m*sizeof(int));
C = (int **)malloc(n*sizeof(int *));
for (i = 0; i < n; i++)
*(C+i) = (int *)malloc(m*sizeof(int)
for (i = 0; i < n; i++)
for (j = 0; j < k; j++)
A = read(A, i, j);
for (i = 0; i < k; i++)
for (j = 0; j < m; j++)
B = read(B, i, j);
C = prod(A, B, n, m, k);
print(C, n, m);
}
int **read(int **x, int i, int j)
{
printf("Give value to store in cell [%d][%d]: ", i, j);
scanf("%d", &x[i][j]);
return x;
}
int **prod(int **x, int **y, int n, int m, int k)
{
int i, j, l, sum;
int **res;
for (i = 0; i < n; i++)
for (j = 0; j < k; j++)
{
sum = 0;
for (l = 0; l < m; l++)
sum = sum + (x[i][l]*y[l][j]);
res[i][j] = sum;
}
return res;
}
void print(int **x, int r, int c)
{
int i, j;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j ++)
printf("C[%d][%d]: %d\t", i, j, x[i][j]);
printf("\n");
}
}
first understand the matrix without pointers
Matrix multiplication without pointers
int r1,r2,c1,c2;
int i,j,sum,k;
void matrixmul()
{
printf("Getting values for matrices");
printf("\n---------------------------");
printf("\n Enter the row for Matrix A: ");
scanf("%d",&r1);
printf("\n Enter the column for Matrix A: ");
scanf("%d",&c1);
printf("\n Enter the row for Matrix B: ");
scanf("%d",&r2);
printf("\n Enter the column for Matrix A: ");
scanf("%d",&c2);
int a[r1][c1];
int b[r2][c2];
int c[r1][c2];
if (!((c1==r2)&&(r1==c2)))
{
printf("Row column miss matching so cannot do matrix multiplication");
}
else
{
printf("\n A Matrix");
printf("\n ---------");
for (i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("\n Enter %d and %d value: ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n B Matrix");
printf("\n ---------");
for (i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("\n Enter %d and %d value: ",i,j);
scanf("%d",&b[i][j]);
}
}
}
for(i=0;i<c1;i++)
{
for(j=0;j<r2;j++)
{
for (k=0;k<r2;k++)
{
sum = sum + a[i][k]*b[k][j];
}
c[i][j] = sum;
sum = 0;
}
}
printf("\nResultant Matrix");
printf("\n----------------\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
void main()
{
matrixmul();
}
Output
Enter the row for Matrix A: 2
Enter the column for Matrix A: 3
Enter the row for Matrix B: 3
Enter the column for Matrix B: 2
A Matrix
Enter the 0 and 0 value:1
Enter the 0 and 1 value:2
Enter the 0 and 2 value:3
Enter the 1 and 0 value:3
Enter the 1 and 1 value:2
Enter the 1 and 2 value:11
B Matrix
Enter the 0 and 0 value:4
Enter the 0 and 1 value:5
Enter the 1 and 0 value:7
Enter the 1 and 1 value:8
Enter the 1 and 0 value:9
Enter the 1 and 1 value:10
Resultant Matrix
45 51
125 141
Matrix with pointers-in this program 2D is declared using pointer variable and get the values for the 2D array and display the values
void main()
{
int r = 2, c = 3, i, j, count;
int *a[r];//while declaring the array as pointer its number of rows are specified
for (i=0; i<r; i++)
{
a[i] = (int *)malloc(c * sizeof(int));//Allocate memory space for each row for 'c' columns
}
count = 0;
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
scanf("%d",&a[i][j] );
}
}
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
printf("%d\t ", a[i][j]);
}
printf("\n");
}
}
Output
3
2
1
1
2
3
3 2 1
1 2 3
The following is the final complete matrix multiplication using pointers
void input()
{
int r1, c1,r2,c2, i, j,k,sum;
printf("\n Enter the row for Matrix A: ");
scanf("%d",&r1);
printf("\n Enter the column for Matrix A: ");
scanf("%d",&c1);
printf("\n Enter the row for Matrix B: ");
scanf("%d",&r2);
printf("\n Enter the column for Matrix A: ");
scanf("%d",&c2);
if (!((c1==r2)&&(r1==c2)))
{
printf("Row column miss matching so cannot do matrix multiplication");
}
else
{
int *a[r1];
int *b[r2];
int *c[r1];
for (i=0; i<r1; i++)
{
a[i] = (int *)malloc(c1 * sizeof(int));
}
for (i=0; i<r2; i++)
{
b[i] = (int *)malloc(c2 * sizeof(int));
}
for (i=0; i<r1; i++)
{
c[i] = (int *)malloc(c2 * sizeof(int));
}
printf("\n A Matrix");
printf("\n ----------");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c1; j++)
{
printf("\n Enter the %d and %d value:",i,j);
scanf("%d",&a[i][j] );
}
}
printf("\n B Matrix");
printf("\n ----------");
for (i = 0; i < r2; i++)
{
for (j = 0; j < c2; j++)
{
printf("\n Enter the %d and %d value:",i,j);
scanf("%d",&b[i][j]);
}
}
for(i=0;i<c1;i++)
{
for(j=0;j<r2;j++)
{
for (k=0;k<r2;k++)
{
sum = sum + a[i][k]*b[k][j];
}
c[i][j] = sum;
sum = 0;
}
}
printf("\n Resultant Matrix");
printf("\n------------------");
for (i = 0; i < r1; i++)
{
for (j = 0; j < c2; j++)
{
printf("%d\t ", c[i][j]);
}
printf("\n");
}
}
}
void main()
{
input();
}
Output
Enter the row for Matrix A: 2
Enter the column for Matrix A: 3
Enter the row for Matrix B: 3
Enter the column for Matrix B: 2
A Matrix
Enter the 0 and 0 value:1
Enter the 0 and 1 value:2
Enter the 0 and 2 value:3
Enter the 1 and 0 value:3
Enter the 1 and 1 value:2
Enter the 1 and 2 value:11
B Matrix
Enter the 0 and 0 value:4
Enter the 0 and 1 value:5
Enter the 1 and 0 value:7
Enter the 1 and 1 value:8
Enter the 2 and 0 value:9
Enter the 2 and 1 value:10
Resultant Matrix
45 51
125 141
run the above program one by one understand it then convert the final program into function as per your required,Thank you

Add or substract negative numbers in C

So i've been trying to do a code that adds the integers of a row in an array (this integers can be negative for example)
So you will have something like this:
input:
3 2 (Number of rows and columns)
12 7
-3 6
-2 -5
output:
19
3
-7
What I've done is this:
#include <stdio.h>
int 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);
printf("Enter the co-efficients 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("%d\n",sum);
sum = 0;
}
}
I can't find a way to "add" negative numbers, what should I change/add to this code? Thanks again :)
#include <stdio.h>
int main (){
int array[10][10];
int i, j, m, n, sum = 0;
printf("Enter the order of the matrix\n");
scanf("%d %d", &m, &n);
printf("Enter the co-efficients 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("%d\n",sum);
sum = 0;
}
return 0;
}

Resources