Multiplication of 2D arrays, using pointers to pointers - c

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

Related

Finding the highest frequency of an array and all elements which have that frequency

I'm asked to find the highest frequency from an array of elements and all elements with said frequency. My code seem to work just fine but it seems to have a mistake somewhere when i submit it. Can anyone help me find the error?
Format Input:
The first line contains an integer T stating the number of test cases. For each test case, the first line contains a single integer N which indicate the number of element in the array. The next line contains N integers Xi (1≤i≤N) which indicate ith element in the array.
Format Output:
Consists of T lines where each line has the format “Case #X: Y ”, where X is the test case number starting at 1 and Y is the highest frequency. Next line contains all elements which have that frequency sorted in ascending order.
Constraints:
1 ≤ T ≤ 20 | 2 ≤ N ≤ 20.000 | 1 ≤ Xi ≤ 2 × 10^5
Sample Input:
3
8
1 1 2 2 3 4 5 5
8
5 5 4 3 2 2 1 1
4
1 1 1 3
Sample Output:
Case #1: 2
1 2 5
Case #2: 2
1 2 5
Case #3: 3
1
Here is my code:
#include <stdio.h>
int main() {
int T, N[20];
scanf("%d", &T); getchar();
int A[T][20000];
for (int i = 0; i<T; i++) {
scanf("%d", &N[i]); getchar();
for (int j = 0; j<N[i]; j++) {
scanf("%d", &A[i][j]); getchar();
}
int X = 0;
for (int j = 0; j<N[i]; j++) {
for (int k = j + 1; k<N[i]; k++) {
if (A[i][k]<A[i][j]) {
X = A[i][j];
A[i][j] = A[i][k];
A[i][k] = X;
}
}
}
}
int f[20000];
for (int i = 0; i<T; i++) {
int c = 0, mc = 0;
for (int j = 0; j<N[i]; j++) {
c = 1;
if(A[i][j] != -1) {
for (int k = j+1; k<N[i]; k++) {
if (A[i][j] == A[i][k]) {
c++;
A[i][k] = -1;
}
}
f[j]=c;
}
if (c>mc) {
mc = c;
}
}
printf("Case #%d: %d\n", i+1, mc);
for (int j = 0; j<N[i]; j++) {
if (A[i][j] != -1) {
if (f[j] == mc) {
printf ("%d", A[i][j]);
if (j<N[i]-1) {
printf(" ");
}
}
}
}
printf("\n");
}
return 0;
}
EDIT
So I made another code where instead of inputting all arrays at once and outputting everything at once, this code outputs the frequency and elements after i input the first arrays of numbers. But it seems like the code still have problems and i can't find where... P.s I'm pretty new to this, so i apologise for the lack of efficiency of my codes.
NEW CODE
#include <stdio.h>
int main() {
int T, N;
scanf("%d", &T); getchar();
int A[20000];
for (int i = 0; i<T; i++) {
scanf("%d", &N); getchar();
for (int j = 0; j<N; j++) {
scanf("%d", &A[j]); getchar();
}
int X;
for (int j = 0; j<N; j++) {
for (int k = j + 1; k<N; k++) {
if (A[k]<A[j]) {
X = A[j];
A[j] = A[k];
A[k] = X;
}
}
}
int f[N], c = 0, mc = 0;
for (int j = 0; j<N; j++) {
c = 1;
if(A[j] != -1) {
for (int k = j+1; k<N; k++) {
if (A[j] == A[k]) {
c++;
A[k] = -1;
}
}
f[j]=c;
if (c>mc) {
mc = c;
}
}
}
printf("Case #%d: %d\n", i+1, mc);
for (int j = 0; j<N; j++) {
if (A[j] != -1) {
if (f[j] == mc) {
printf ("%d", A[j]);
if (j<N-1) {
printf(" ");
}
}
}
}
printf("\n");
}
return 0;
}
It took me a couple of days but i finally got how to do this. Apparently, it was not as complicated as i thought... here is the working code. Thanks to everyone who helped :)
#include <stdio.h>
int main() {
int T, N;
scanf("%d", &T);
for (int i = 0; i<T; i++) {
scanf("%d", &N); getchar();
//INPUT elements and counting frequncy for each element
int f[200001] = {0}, E = 0;
for (int j = 0; j<N; j++) {
scanf("%d", &E); getchar();
f[E]++;
}
//find max frequency and how many elements with max frequency
int max = 0, c = 0;
for (int j = 1; j<200001; j++) {
if (f[j] == max) {
c ++;
}
if (f[j]>max) {
max = f[j];
c = 1;
}
}
//OUTPUT result
printf("Case #%d: %d\n", i+1, max);
int counter = 0;
for (int j = 1; j<200001; j++) {
if (f[j] == max) {
counter ++;
if (counter<c){
printf("%d ", j);
} else {
printf("%d\n", j);
}
}
}
}
return 0;
}

Can't change dynamically allocated array values inside a function call

I'm having some issues with C.
I was tasked with this assignment:
Create a dynamically allocated matrix (max 5x5), let the user fill in the values, then send it to a function along with an unallocated array (1D) which will be used to store all values of the matrix that are lower than the average of each row.
The 1D array should be defined in the main function and allocated inside the function that finds the desired elements.
This was going fine, I made my matrix, I sent it to the aforementioned function, but I could not change the values of the newly allocated 1D array.
#include<stdio.h>
#include<stdlib.h>
int lower_than_average(int **matrix, int **p, int m, int n) {
int i, j, sum;
float average;
int count = 0, counter = 0;
for (i = 0; i < m; i++) {
sum = 0;
for (j = 0; j < n; j++) sum += matrix[i][j];
average = (float)sum / n;
for (j = 0; j < n; j++){
if (matrix[i][j] < average) count++;
}
}
*p = (int*)malloc(count * sizeof(int));
for (i = 0; i < m; i++) {
sum = 0;
for (j = 0; j < n; j++) sum += matrix[i][j];
average = (float)sum / n;
for (j = 0; j < n; j++) {
if (matrix[i][j] < average) *p[counter++] = matrix[i][j];
}
}
printf("\n\n");
return(count);
}
void print_matrix(int **matrix, int m, int n) {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n\n");
}
}
int main(void) {
int m, n, **matrix, *elements_a = NULL;
int i, j, elements_a_size;
do {
printf("Enter the number of rows and columns [max 5x5]: ");
scanf("%d %d", &m, &n);
} while (n > 5 || n < 0 || m > 5 || m < 0);
matrix = (int**)malloc(m * sizeof(int*)); // Alokacija redaka
/*
X | <empty>
X | <empty>
X | <empty>
X | <empty>
*/
for (i = 0; i < m; i++) matrix[i] = (int*)calloc(n, sizeof(int)); // Alokacija stupaca
/*
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
*/
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("Enter [%d][%d] element: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
printf("\n\n");
print_matrix(matrix, m, n);
elements_a_size = lower_than_average(matrix, &elements_a, m, n);
printf("\nElements of rows smaller than the row average value: ");
for (i = 0; i < elements_a_size; i++) printf("%d ", elements_a[i]);
for (i = 0; i < m; i++) free(matrix[i]);
free(matrix);
return 0;
}
For the input: 3 3 then 1 2 3 4 5 6 7 8 9
It outputs literally nothing.
**p and *p[0]
instead of
*p[counter++]
it outputs this: 7 -2467754 -2467754
What am I doing wrong? I thought the allocation was correct and that I could access *(p + n) but it doesn't do anything...
You have to change the line
*p[counter++] = matrix[i][j];
by
(*p)[counter++] = matrix[i][j];
because [] take priority over *
See : Operator priorities in c/c++
The easiest way may be to create a one dimensional array that is big enough to store all the elements in an array, and then populate the 1D array as you go through each row of the 2D array. Once you have gone through the 2D array you copy the values that you need to an array that is just long enough and return.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int lower_than_average(int **matrix, int **p, int m, int n) {
int i, j, sum;
float average;
int count = 0, counter = 0;
int *buff;
buff = (int *)malloc(m * n * sizeof(int));
if (NULL == buff)
{
return (-1); //return negative value to show an error occurred
}
for (i = 0; i < m; i++) {
sum = 0;
for (j = 0; j < n; j++) sum += matrix[i][j];
average = (float)sum / n;
for (j = 0; j < n; j++){
if (matrix[i][j] < average)
{
buff[count] = matrix[i][j];
count++;
}
}
}
*p = (int*)malloc(count * sizeof(int));
if(NULL == *p)
{
free(buff);
return (-1); //return negative value to show an error occurred
}
memcpy(*p, buff, count*sizeof(int));
free(buff);
printf("\n\n");
return(count);
}
void print_matrix(int **matrix, int m, int n) {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n\n");
}
}
int main(void) {
int m, n, **matrix, *elements_a = NULL;
int i, j, elements_a_size;
do {
printf("Enter the number of rows and columns [max 5x5]: ");
scanf("%d %d", &m, &n);
} while (n > 5 || n < 0 || m > 5 || m < 0);
matrix = (int**)malloc(m * sizeof(int*)); // Alokacija redaka
/*
X | <empty>
X | <empty>
X | <empty>
X | <empty>
*/
for (i = 0; i < m; i++) matrix[i] = (int*)calloc(n, sizeof(int)); // Alokacija stupaca
/*
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
X | Y Y Y Y Y Y
*/
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("Enter [%d][%d] element: ", i, j);
scanf("%d", &matrix[i][j]);
}
}
printf("\n\n");
print_matrix(matrix, m, n);
elements_a_size = lower_than_average(matrix, &elements_a, m, n);
printf("\nElements of rows smaller than the row average value: ");
for (i = 0; i < elements_a_size; i++) printf("%d ", elements_a[i]);
printf("\n"); //a new line so the prompt isn't on the same line as the output
for (i = 0; i < m; i++) free(matrix[i]);
free(matrix);
free(elements_a); //also free the memory dynamically allocated in the function!
return 0;
}
And the results:
Enter the number of rows and columns [max 5x5]: 3
3
Enter [0][0] element: 1
Enter [0][1] element: 2
Enter [0][2] element: 3
Enter [1][0] element: 4
Enter [1][1] element: 5
Enter [1][2] element: 80
Enter [2][0] element: 12
Enter [2][1] element: 2
Enter [2][2] element: 1
1 2 3
4 5 80
12 2 1
Elements of rows smaller than the row average value: 1 4 5 2 1

How to perform different operations on matrix using dynamic memory allocation?

There is segmentation fault in case 5 of this program(transpose) this segmentation fault occurs only when the input row is greater than that of the input column. Hopefully this is due to the reason I have not allocated the memory accordingly.
b= (int**)malloc(r*sizeof(int*));
for(i=0; i<c; i++)
{
b[i] = (int*)malloc(sizeof(int));
}
And if I am doing it accordingly like this:
b= (int**)malloc(c*sizeof(int*));
for(i=0; i<r; i++)
{
b[i] = (int*)malloc(sizeof(int));
}
there still is seg. fault and this time it is not producing correct output for matrix of any order.
Here is my full code :
#include<stdio.h>
#include<stdlib.h>
int** inputmatrix(int **a,int r, int c)
{
int i, j;
a = (int**)malloc(r*sizeof(int));
for(i=0; i<c; i++)
{
a[i] = (int*)malloc(sizeof(int));
}
printf("\n Input the Elements of the Matrix :");
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
scanf("%d",&a[i][j]);
}
}
return a;
}
void showmatrix(int** a, int r, int c)
{
int i, j;
for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; j++)
{
printf(" %d",a[i][j]);
}
}
}
int** add(int **a, int **b, int r, int c)
{
int i,j;
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
a[i][j] = a[i][j]+b[i][j];
}
}
return a;
}
int** multiplication(int** a, int **b, int r1, int c1, int c2)
{
int **c,i,j,k;
c = (int**)malloc(r1*sizeof(int*));
for(i=0; i<c2; i++)
{
c[i] = (int*)malloc(sizeof(int));
}
for(i=0; i<r1; i++)
{
for(j=0; j<c2; j++)
{
c[i][j] = 0;
for(k=0; k<c1; k++)
{
c[i][j] = c[i][j] + a[i][k]*b[k][j];
}
}
}
return c;
}
int minval(int **a, int r, int c)
{
int i, min;
min = a[r][0];
for(i=0; i<c; ++i)
{
if(a[r][i]<min)
{
min = a[r][i];
}
}
return min;
}
int maxval(int **a, int r, int c)
{
int i, max;
max = a[0][c];
for(i=0; i<r; ++i)
{
if(a[i][c] > max )
{
max = a[i][c];
}
}
return max;
}
void saddlepoint(int **a, int r, int c)
{
int i, j, rpos, cpos, flag = 0,sp;
for(i=0; i<r; ++i)
{
for(j=0; j<c; ++j)
{
if(a[i][j] == minval(a, i, c) && a[i][j] == maxval(a, r, j))
{
sp = a[i][j];
flag = 1;
rpos = i;
cpos = j;
}
}
}
if(flag == 1)
{
printf("\n The Saddle point of the Matrix is found at position (%d,%d) value is %d ", rpos, cpos,sp);
}
else
{
printf("\n There is no saddle point in the Matrix ");
}
}
int magicsquare(int **a, int r, int c)
{
int i, j, row_sum, col_sum, d1, d2, flag = 0;
if(r==c)
{
for(i =0 ;i<r; i++) // for digonals
{
d1 = d1 + a[i][i];
d2 = d2 + a[i][r-i-1];
}
for(i=0; i<r; i++)
{
row_sum = 0;
for(j=0; j<c; j++)
{
row_sum = row_sum + a[i][j];
}
if(row_sum == d1)
flag = 1;
else
break;
}
for(i=0; i<r; i++)
{
col_sum = 0;
for(j=0; j<c; j++)
{
col_sum = col_sum + a[j][i];
}
if(col_sum == d1)
flag =1;
else
break;
}
}
else
{
printf("\n This Matrix is not a Magic Square ");
}
return flag;
}
int** transpose(int **a, int r, int c)
{
int i, j, **b;
b= (int**)malloc(c*sizeof(int*));
for(i=0; i<r; i++)
{
b[i] = (int*)malloc(sizeof(int));
}
for(i =0; i<r; i++)
{
for(j=0; j<c; j++)
{
b[j][i] = a[i][j];
}
}
return b;
}
int main()
{
int **a, **b,r1,c1,r2,c2, i,j,ch,f;
int **c;
printf("\n enter your choice : \n1.Addition \n2.Multiplication \n3.Saddle Point \n4. Magic Square \n5.Transpose\n");
scanf("%d",&ch);
printf("\n enter the oder of matrix A :");
scanf("%d%d",&r1,&c1);
a = inputmatrix(a,r1,c1);
switch(ch)
{
case 1:
printf("\n enter the oder of matrix B :");
scanf("%d%d",&r2,&c2);
if(r1==r2 && c1==c2)
{
b = inputmatrix(b,r2,c2);
a = add(a,b,r1,c1);
printf("\n the result of the addition of matrices is :");
for(i=0; i<r1; i++)
{
printf("\n");
for(j=0;j<c1; j++)
{
printf("%d\t",a[i][j]);
}
}
}
else
{
printf("\n these matrices can't be added ");
}
break;
case 2 :
printf("\n Enter the Order of Matrix B :");
scanf("%d%d",&r2,&c2);
b = inputmatrix(b,r2,c2);
if(c1 == r2)
{
c = multiplication(a, b, r1, c1, r2);
for(i=0; i<r1; i++)
{
printf("\n");
for(j=0; j<c2; j++)
{
printf("%d\t",c[i][j]);
}
}
}
else
{
printf("\n Sorry, These Matrices Can't be Multiplied ");
}
break;
case 3 :
printf("\n The Matrix you Entered is :");
for(i=0; i<r1; i++)
{
printf("\n");
for(j=0; j<c1; j++)
{
printf(" %d",a[i][j]);
}
}
saddlepoint(a,r1,c1);
break;
case 4 :
printf("\n The Matrix you Entered is :");
for(i=0; i<r1; i++)
{
printf("\n");
for(j=0; j<c1; j++)
{
printf(" %d",a[i][j]);
}
}
int f = magicsquare(a, r1, c1);
if(f==1)
printf("\n This Matrix is a Magic Square ");
else
printf("\n This Matrix is not a Magic Square ");
break;
case 5 :
printf("\n The Matrix you enter is :");
showmatrix(a,r1,c1);
b = transpose(a,r1,c1);
printf("\n the transpose of the entered matrix is :");
for(i=0; i<c1; i++)
{
printf("\n");
for(j=0; j<r1; j++)
{
printf(" %d",b[i][j]);
}
}
break;
default : printf("\n Sorry, This is a Wrong Choice ");
}
return 0;
}
A few Output cases are also below:
case 1:
enter your choice :
1.Addition
2.Multiplication
3.Saddle Point
4. Magic Square
5.Transpose
5
enter the oder of matrix A :3
2
Input the Elements of the Matrix :1
2
3
4
5
Segmentation fault (core dumped)
case 2:
enter your choice:
1.Addition
2.Multiplication
3.Saddle Point
4. Magic Square
5.Transpose
5
enter the oder of matrix A :2
3
Input the Elements of the Matrix :1
2
3
4
5
6
The Matrix you enter is :
1 2 3
4 5 6
the transpose of the entered matrix is :
1 4
2 5
3 6
And there is some problem in the multiplication function also, there also right matrix is not being displayed.
b= (int**)malloc(r*sizeof(int*));
This allocates an array of r int* elements.
for(i=0; i<c; i++)
This goes over the first c elements of this array. If c>r, then
b[i] = (int*)malloc(sizeof(int));
the behaviour of the line above is undefined. If c<=r, it is well-defined but not very useful, as it allocates one element for each matrix row. It probably becomes broken later, when you try to access elements past the first column.
To allocate a matrix with r rows and c columns you probably want to do this:
b = malloc(r * sizeof(int*)); /* allocate `r` rows */
for(i = 0; i < r; i++) /* for each of the `r` rows */
b[i] = malloc (c * sizeof(int)); /* allocate `c` columns */
There is mistake in the declaration of the arrays using malloc.
When you wish to declare a 2-D array of 'r' rows and 'c' columns, the statements should be something like
int **arr=malloc(r*sizeof(int *));
for(i=0;i<r;i++)
arr[i]=malloc(c*sizeof(int));
This will eliminate the segmentation fault.
The first statement declares an array of 'r' integer pointers.
The for loop executes 'r' times since you have to initialize each of the 'r' pointers declared by the previous statement.
The statement inside the for loop declares an integer array of length 'c' to store 'c' elements of each row.
Moreover, it initializes each of the pointers of the array 'arr' with the addresses of these rows(in correct order of-course).
Thus we get 'r' rows of 'c' elements each and hence an (r x c) matrix.

How to delete duplicated values in array in C?

I want to delete duplicates values in array. For example: array[1,5,6,1,3,5,9] I want to have array[6,3,9].
I have written this, but I am having troubles:
#include<stdio.h>
main() {
int array[50], i, j, k=0, c=0, array2[50], n;
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("array[%d]= ", i); scanf("%d", &array[i]);
}
for (i = 0; i < n; ) {
for (j = i + 1; j < n; j++) {
if (array[i] == array[j])
i++;
else {
array2[k++] = array[i];
c++;
}
}
}
for (k = 0; k < c; k++) {
printf("%d ", array2[k]);
}
system("pause");
}
You should start by describing your problem in pseudo code, and breaking it into smaller pieces.
Start from scratch by deleting all these redundant variables, and try to implement the following algorithm:
for each element in inputArray
if not elementIsDuplicate(element)
add to outputArray
That's a single for loop. The elementIsDuplicate is separate function.
Now try to implement the function elementIsDuplicate. This function also contains a single loop, takes input parameters (int* array, int n, int currentIdx) and returns 1 or 0 indicating whether the element at currentIdx occurs anywhere else in the array.
#include<stdio.h>
int main(){
int array[50], i, j, k=0, c, n, array2[50] = {0};
printf("Enter array dimension: "); scanf("%d", &n);
for (i = 0; i < n; ++i){
int num, dup = 0;
printf("array[%d]= ", i); scanf("%d", &num);
for(j = 0; j < k; ++j){
if(array[j] == num){
array2[j] = dup = 1;
break;
}
}
if(!dup){
array[k++] = num;
}
}
for (c=i=0; i < k; ++i){
if(!array2[i])
printf("%d ", array[c++] = array[i]);
}
printf("\n");
/*
for(i=0;i<c;++i)
printf("%d ", array[i]);
printf("\n");
*/
system("pause");
return 0;
}
#include<stdio.h>
main()
{
int n, a[50], b[50], count = 0, c, d;
printf("Enter number of elements in array\n");
scanf("%d",&n);
printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
scanf("%d",&a[c]); //enter array elements
for(c=0;c<n;c++)
{
for(d=0;d<count;d++)
{
if(a[c]==b[d])
break;
}
if(d==count)
{
b[count] = a[c];
count++;
}
}
printf("count is: %d\n",count);
printf("Array obtained after removing duplicate elements\n");
for(c=0;c<count;c++)
printf("%d\n",b[c]);
return 0;
}
This will remove multiple duplicates from the desired array..
Example: if the input array is: 3 6 5 6 2 8 6 5 9 8 6 ,,then the output array will be: 3 6 5 2 8 9

getting address as output when multiplying two matrices

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.

Resources