Sum and product of two matrices in C (with functions) - c

I have to write a program in C which finds the sum and the product of two matrices.
I wrote the functions but I get stuck at calling them in main. I don't know which variable is for rows and columns of result matrix.
#include <stdio.h>
void enterMatrix(int a[][10], int rows, int columns)
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
printf("a(%d,%d)=",i,j);
scanf("%d",&a[i][j]);
}
}
}
void displayMatrix(int a[][10], int rows, int columns)
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
printf("%d", a[i][j]);
printf(" ");
}
printf("\n");
}
}
void matrixSum(int a[][10], int b[][10], int c[][10], int rows, int columns)
{
int i,j;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
}
void matrixProduct(int a[][10], int b[][10], int c[][10], int rows, int columns)
{
int i,j,k;
for(i=0;i<rows;i++)
{
for(j=0;j<columns;j++)
{
c[i][j]=0;
for(k=0;k<columns;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
}
int main(void)
{
int a[10][10], b[10][10], sum[10][10], product[10][10];
int rowsA,columnsA,rowsB,columnsB;
printf("Number of rows for matrix A: \n");
scanf("%d",&rowsA);
printf("Number of columns for matrix A: \n");
scanf("%d",&columnsA);
printf("Number of rows for matrix B: \n");
scanf("%d",&rowsB);
printf("Number of columns for matrix B: \n");
scanf("%d",&columnsB);
printf("Enter first matrix: \n");
enterMatrix(a,rowsA,columnsA);
printf("Show first matrix: \n");
displayMatrix(a,rowsA,columnsA);
printf("Enter second matrix: \n");
enterMatrix(b,rowsB,columnsB);
printf("Show second matrix: \n");
displayMatrix(b,rowsB,columnsB);
if((rowsA==rowsB) && (columnsA==columnsB))
{
matrixSum(a,b,sum, ???, ???);
printf("The sum matrix is: \n");
displayMatrix(sum, ???, ???);
}
else
{
printf("Wrong information.");
}
if((rowsA==columnsB) && (rowsB==columnsA))
{
matrixProduct(a,b,product,???,???);
printf("The product matrix is \n");
displayMatrix(product,???,???);
}
return 0;
}

For matrixSum you just give rowsA and columnsA, as they are equal to rowsB and columnsB.
For matrixProduct you need three numbers: rowsA, columnsA and columnsB. rowsB is not needed, as it is equal to columnsA.
You need to change your matrixProduct function to use these three numbers at the correct places. Your current code doesn't work except when all numbers are equal.
Also your test if((rowsA==columnsB) && (rowsB==columnsA)) before calling matrixProduct only needs if(rowsB==columnsA).
if((rowsA==rowsB) && (columnsA==columnsB))
{
matrixSum(a,b,sum, rowsA, columnsA);
printf("The sum matrix is: \n");
displayMatrix(sum, rowsA, columnsA); // the sum has the same dimensions as A and B
}
else
{
printf("Both matrices don't have equal dimension.\n");
}
if(rowsB==columnsA)
{
matrixProduct(a,b,product,rowsA,columnsA,columnsB);
printf("The product matrix is \n");
displayMatrix(product,rowsA,columnsB); // the product matrix has the
// number of rows of A and the number of columns of B
}
else
{
printf("The number of columns of A needs to be equal to the number or rows of B.\n");
}
Your matrixProduct function could be adapted as follows:
void matrixProduct(int a[][10], int b[][10], int c[][10], int rowsA, int columnsA, int columnsB)
{
int i,j,k;
for(i=0;i<rowsA;i++)
{
for(j=0;j<columnsB;j++)
{
c[i][j]=0;
for(k=0;k<columnsA;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
}
}
}

Related

My C Program is not executing fully, it's stopping midway. Question is taking elements of array from user and calculating sum of each column in array

The Question is to take dimension of array and elements of it from the user. Then calculate the sum of each column in array, it stops midway for some reason. I am new to C, is there a problem in usig malloc not in right way?
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m,n;
int arr[m][n];
int **arr;
int i,j;
printf("Enter Number of Rows and Columns:\t");
scanf("%d %d", &m, &n);
arr=(int**)malloc(sizeof(int*)*m);
for(i=0;i<10;i++)
{
arr[i]=(int*)malloc(sizeof(int)*n);
}
printf("Type Array Elements:\t");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d", &arr[i][j]);
}
}
printf("This is Where It Stops");
//ADDITION OF COLUMNS
int Sum;
int *sum=(int*)malloc(j*sizeof(int));
for(j=0;j<n;j++)
{
for(i=0;i<m;i++)
{
Sum=0;
Sum=Sum+arr[i][j];
}
sum[j]=Sum;
}
for(j<0;j<0;j++)
{
printf("Sum of Column %d is %d", j, sum[j]);
}
return 0;
}

sorting a matrix using column sort

according to the book "introduction to algorithms I tried sorting a matrix using column sort.here is my approach
1) sort each row of matrix -
#include<stdio.h>
#include<stdlib.h>
void sort(int arr[3][3], int k)// here k defines the column number ,k remains constant through out the function
//because we have to sort the matrix column wise respectively
{
int i;
int c[10]={0};
int b[3][3];
for(i=0;i<3;i++)
{ c[arr[i][k]]++;
}
for(i=1;i<3;i++)
{
c[i]+=c[i-1];
}
for(i=2;i>=0;i--)
{
b[c[arr[i][k]]-1][k]=arr[i][k];
c[arr[i][k]]--;
}
for(i=0;i<3;i++)
{
arr[i][k]=b[i][k];
}
}
I have passed k as an argument,which is the column number (as it remains constant throughout the sorting of each column, and only the row number changes,so i have iterated only over the row number )
the function to pass the desired column number which then calls the count function
void column(int arr[3][3]) // to call the function column wise by passing k as a parameter to count function
{
int k;
for(k=0;k<3;k++)
sort(arr,k);
}
2) transpose a matrix
{
int i,j,temp;
for(i=0;i<3;i++)
{ for(j=0;j<3;j++)
temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
the print function
void print(int arr[3][3]) // to print the output
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
the main function
{
int arr[3][3];
int i,j;
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d ",&arr[i][j]);
}
// prints the array just inputed
print(arr);
// column calls the function sort according to the column
column(arr);
transpose(arr); // matrix is transposed
printf("\n");
print(arr); // matrix is printed
column(arr); // matrix is again passed with respect to the columns and sorted
transpose(arr); // matrix is transposed to get the original matrix
printf("\n");
print(arr); //final result is printed
return 0;
}
the output is very unlikely how can it be sorted correctly
Here is a working version of your code. I fixed the dropped bracket and the transpose function:
#include <stdio.h>
#include<stdlib.h>
void sort(int arr[3][3], int k)// here k defines the column number ,k remains constant through out the function
//because we have to sort the matrix column wise respectively
{
int i,j;
int tmp = 0;
for(i=0;i<2;i++)
{
for (j = 0; j < 3-i-1; j++)
{
if (arr[j][k] > arr[j+1][k])
{
tmp = arr[j][k];
arr[j][k] = arr[j+1][k];
arr[j+1][k] = tmp;
}
}
}
printf("\n");
}
void column(int arr[3][3]) // to call the function column wise by passing k as a parameter to count function
{
int k;
for(k=0;k<3;k++)
{
sort(arr,k);
}
}
void transpose(int arr[3][3])
{
int i,j,temp;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i != j && i<j)
{
temp=arr[i][j];
arr[i][j]=arr[j][i];
arr[j][i]=temp;
}
}
}
}
void print(int arr[3][3]) // to print the output
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",arr[i][j]);
}
printf("\n");
}
}
int main()
{
int arr[3][3] = {0};
int i,j;
for (i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d ",&arr[i][j]);
}
printf("\n");
// prints the array just inputed
print(arr);
printf("\n");
// column calls the function sort according to the column
column(arr);
print(arr);
transpose(arr); // matrix is transposed
printf("\n");
print(arr); // matrix is printed
column(arr); // matrix is again passed with respect to the columns and sorted
printf("\n");
print(arr); // matrix is printed
transpose(arr); // matrix is transposed to get the original matrix
printf("\n");
print(arr); //final result is printed
return 0;
}
Here is the output of: Sort, Transpose, Sort, then transpose:

Junk values when using fixed size array whereas if I use dynamically sized array the results are correct

#include<stdio.h>
void readMatrix(int m,int n,int a[][n]);
void displayMatrix(int m,int n,int a[][n]);
void addMatrix(int m,int n,int a[][n],int b[][n]);
int main()
{
int a[5][5],b[5][5],m,n,p,q;
printf("Enter the no. of rows and columns of matrix A\n");
scanf("%d%d",&m,&n);
printf("Enter the no. of rows and columns of matrix B\n");
scanf("%d%d",&p,&q);
if(m!=p||n!=q)
{
printf("Matrix addition not possible\n");
return 0;
}
printf("Enter %d elements to matrix A\n",m*n);
readMatrix(m,n,a);
printf("Enter %d elements to matrix B\n",m*n);
readMatrix(p,q,b);
printf("Matrix A\n");
displayMatrix(m,n,a);
printf("Matrix B\n");
displayMatrix(p,q,b);
addMatrix(m,n,a,b);
}
void readMatrix(int m,int n,int a[m][n])
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
}
void displayMatrix(int m,int n,int a[m][n])
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
}
void addMatrix(int m,int n,int a[m][n],int b[m][n])
{
int c[5][5]; //I guess this is causing problem
//int c[m][n]; //gives correct answer
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("Sum Matrix\n");
/*Gives correct result
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
*/
displayMatrix(m,n,c);
}
displayMatrix(m,n,c) is correct If i define array as c[m][n], code for it is given above.
It is giving junk value if I use c[5][5].
I'm using gcc compiler. I displayed the sum in addMatrix function itself, which gives correct result. Not able to figure out the mistake. Kindly help. Thank You.

Chef and Dolls MISSP

Here is the link to the codechef problem - Chef and Dolls
Why does my code always print the wrong answer? The output should be the number which doesn't have a match but my code always print the first element. Coudn't get the conditions right, what should be the condition?
Problem Statement
Chef is fan of pairs and he likes all things that come in pairs. He even has a doll collection in which all dolls have paired.One day while going through his collection he found that there are odd number of dolls. Someone had stolen a doll!!!
Help chef find which type of doll is missing..
Input
The first line contains the number of test cases.
Second line of the input contains the number of elements in the array.
The next n lines are the types of each doll that is left.
Output
Find the type of doll that doesn't have a pair
Example
Input:
1
3
1
2
1
Output:
2
#include <stdio.h>
int main(){
int t, N, i, m, k, z, flag=0;
scanf("%d", &t);
while(t--)
{
scanf("%d",&N);
int arr[N];
for(i=0;i<N;i++){
scanf("%d", &arr[i]);
}
int j;
for(j=0;j<N;j++){
m=arr[j];
for(k=0;k<N;k++){
if(m==arr[k] && k!=j)
{
flag=0;
}
else
{
flag=1;
break;
}
printf("%d", m);
}
}
}
return 0;
}
You break out of your loop everytime k==j. You have to check for every array member if it is present somewhere else in the array.
#include <stdio.h>
int main()
{
int t, N, i, j, k, m, z, flag;
scanf("%d", &t);
while(t--)
{
scanf("%d",&N);
int arr[N];
for(i=0;i<N;i++)
{
scanf("%d", &arr[i]);
}
for(j=0;j<N;j++)
{
m=arr[j];
flag=1;
for(k=0;k<N;k++)
{
if(m==arr[k] && k!=j)
{
flag=0;
}
}
if(flag)
{
printf("%d\n", m);
}
}
}
return 0;
}
But be careful as this only looks if an array member has a duplicate in it. You probably want to check if there is an even number of elements in the array.
If you want to check which array members occur an odd number of times
#include <stdio.h>
int main()
{
int t, N, i, j, k, m, z, num, flag;
scanf("%d", &t);
while(t--)
{
scanf("%d",&N);
int arr[N], odds[N], n_odds;
for(i=0;i<N;i++)
{
scanf("%d", &arr[i]);
}
n_odds=0;
for(j=0;j<N;j++)
{
m=arr[j];
num=0;
for(k=0;k<N;k++)
{
if(m==arr[k])
{
num++;
}
}
if(num%2) //occurs an odd number of times
{
flag=1;
for(i=0;i<n_odds;i++)
{
if(m==odds[i]) //has already been checked
{
flag=0;
}
}
if(flag)
{
printf("%d\n", m);
odds[n_odds++]=m;
}
}
}
}
return 0;
}

Passing an 2d array into a function using a pointer to pointer variable in c

the method which takes in pointer to pointer as argument
int findMax(int **a, int m, int n)
{
int max=**a,i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(max<=a[i][j]){
max=a[i][j];
}
}
}
return max;
}
This is the main function from where the findMax method is called.
int main()
{
// Variable Declaration
int m,n,i,j,a[50][50],*arr[50],**arrd;
// User Input
printf("Enter the number of rows in the matrix\n");
scanf("%d",&m);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&n);
printf("Enter the elements in the matrix\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
// Single Pointer Allocation
for(i=0;i<m;i++){
arr[i]=&a[i][0];
}
arrd=&arr[0];
// Output
printf("The matrix is\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("The maximum element in the matrix is %d\n",findMax(arrd,m,n));
return 0;
}
I just want to find out max element in a 2d array using a function which takes in pointer to pointer of the array.
this code works fine but i am looking for a better approach...
#include <stdio.h>
#define NUMCOLUMNS 50
#define NUMROWS 50
int findMax(int (*a)[NUMCOLUMNS], int m, int n)
{
int max=**a,i,j;
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(max<=a[i][j]){
max=a[i][j];
}
}
}
return max;
}
int main()
{
// Variable Declaration
int m,n,i,j,a[NUMROWS][NUMCOLUMNS];
// User Input
printf("Enter the number of rows in the matrix\n");
scanf("%d",&m);
printf("Enter the number of columns in the matrix\n");
scanf("%d",&n);
printf("Enter the elements in the matrix\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
scanf("%d",&a[i][j]);
}
}
// Output
printf("The matrix is\n");
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("The maximum element in the matrix is %d\n",findMax(a,m,n));
return 0;
}

Resources