User is asked to give a number of rows and columns to fill an array of 10x10 array with random numbers.(A part of the table will be filled e.g 2x2). Then a function called printArray will print its values.Then,function change_array will find in every row the biggest value and replace the elements left of it with the value.
For instance:
Initial Array
<p>41 67 34 0 </p>
<p>69 24 78 58</p>
<p>62 64 5 45</p>
changed Array
<p>67 67 34 0 </p>
<p>78 78 78 58 </p>
<p>64 64 5 45 </p>
However,the program gives this:
Initial Array
<p>41 67</p> <p>34 0</p>
Changed Array
<p>67 67</p>
<p>24576000 508</p>
Why is this happening?
simpio.h gets an input of integer value
This can't be done with pointers(We are not there yet in class)
The modified array must be printed in main()
#include <stdio.h>
#include <stdlib.h>
#include "simpio.h"
void populate_data(int R, int C, int A[R] [C]);
void printArray(int R, int C, int A[R] [C]);
void change_array(int R, int C, int A[R] [C]);
int main(void)
{
int A[10] [10],R,C,i,j;
while (TRUE)
{
printf("Give the number of rows: ");
R = GetInteger();
if (R>=0 && R<=10)
break;
else
{
printf("Wrong Answer\n");
}
}
while (TRUE)
{
printf("Enter the number of columns: ");
C = GetInteger();
if (C>=0 && C<=10)
break;
else
{
printf("Wrong Answer\n");
}
}
populate_data(R,C,A);
printf("Initial Array\n");
printArray(R,C,A);
change_array(R,C,A);
printf("Changed Array\n");
for (i=0; i<R; i++)
{
for (j=0; j<C; j++)
{
printf("%d ",A[i] [j]);
}
printf("\n");
}
return 0;
}
void populate_data(int R, int C, int A[R] [C])
{
int i,j;
for (i=0; i<R; i++)
{
for (j=0; j<C; j++)
{
A[i] [j] = rand() % 100;
}
}
}
void printArray(int R, int C, int A[R] [C])
{
int i,j;
for (i=0; i<R; i++)
{
for (j=0; j<C; j++)
{
printf("%-3d ",A[i] [j]);
}
printf("\n");
}
}
void change_array(int R, int C, int A[R] [C])
{
int i, j, max[R], m[R];
for (i=0; i<R; i++)
{
max[i] = A[i] [0];
for (j=0; j<C; j++)
{
if (max[i]< A[i] [j])
{
max[i] =A[i] [j];
m[i] = j;
}
}
}
for (i=0; i<R; i++)
{
for (j=0; j<C; j++)
{
if (j<m[i])
A[i] [j] = max [i];
}
}
}
This happens if the max value in a row is on first position, so you will never enter this block
if (max[i]< A[i] [j])
{
max[i] =A[i] [j];
m[i] = j;
}
and m[i] will stay uninitialized. Just add a
m[i] = 0;
before that loop.
Edit:
You must change your function definitions from A[R][C] to A[10][10], because R and C are unknown to the function at that point.
Also, there is no reason to declare the array with size 10x10. You can do that after you got the dimension from input with the correct number of rows and columns.
Related
I wrote this code that inputs 2 matrices and prints the product after multiplying the 2 matrices using pointers. But when I enter the first element I am getting segmentation fault. I have tried everything with pointers but I think this is some loop error as it says segmentation fault. Please help
#include<stdio.h>
#include<stdlib.h>
#define N 10
void readMatrix(float *arrp, int rows, int cols)
{
for (int i=0; i<rows; i++)
{
for (int j=0; j<cols; j++)
{
printf("Enter element [%d][%d] : ",i+1,j+1);
scanf("%f",&*(arrp + i) + j);
}
}
return;
}
void printMatrix(float *arrp, int rows, int cols)
{
for (int i=0; i<rows; i++)
{
for (int j=0; j<cols; j++) printf("%.2f\t",*((arrp + i) + j));
printf("\n");
}
return;
}
int main()
{
float *ap, *bp, *cp;
int arows, brows, acols, bcols;
printf("Give no. of rows of matrix A (<=%d) = ",N);
scanf("%d",&arows);
printf("Give no. of columns of matrix A (<=%d) = ",N);
scanf("%d",&acols);
if (arows>N || acols>N || arows<1 || acols<1)
{
printf("Illegal rows/cols = %d/%d. Should be <=%d.\n", arows, acols, N);
exit(0);
}
readMatrix(ap, arows, acols); printf("\n");
printf("Matrix A :\n"); printMatrix(ap, arows, acols); printf("\n");
printf("Give no. of rows of matrix B (<=%d) = ", N);
scanf("%d",&brows);
printf("Give no. of columns of matrix B (<=%d) = ", N);
scanf("%d",&bcols);
if (brows>N || bcols>N || brows<1 || bcols<1)
{
printf("Illegal rows/cols = %d/%d. Should be <=%d.\n", brows, bcols, N);
exit(0);
}
if (acols==brows)
{
readMatrix(bp, brows, bcols); printf("\n");
printf("Matrix B :\n"); printMatrix(bp, brows, bcols); printf("\n");
for (int i=0; i<arows; i++)
{
for (int j=0; j<bcols; j++)
{
float sum=0.0;
for (int k=0; k<acols; k++) sum += *((ap + i) + k) * *((bp + k) + j);
*((cp + i) + j) = sum;
}
}
printf("After Multiplication, Matrix C :\n"); printMatrix(cp, arows, bcols);
}
else printf("\nIncompatible matrices\nColumns of matrix A should be equal to rows of matrix B\n");
exit(0);
}
Your program never assigns any values to ap, bp, or cp. You need to reserve memory for the data in some way and set the pointers to point to it.
Additionally, *((ap + i) + k) is equivalent to *(ap + (i + k)). It merely adds i and k. The program needs to compute where the element in row i and column k is. If ap is a pointer to float, you need to multiply i by the number of columns. If ap is a pointer to an array, you need to insert another operator in there—go back to your source material for the class and see what is different about this expression than what the book or the instructor taught.
#include <stdio.h>
void sort(int *ptr, int n) {
int i,j,tmp;
for (i=0;i<n;i++)
for (j=0;j<n;j++)
if (ptr[i] < ptr[j])
{
tmp=ptr[i];
ptr[i]=ptr[j];
ptr[j]=tmp;
}
}
int main() {
int i,n;
int *ptr;
printf("Nr. of elements : 5 \n");
n=5;
ptr=(int*)malloc( n * sizeof(int));
for (i=0;i<n;i++) {
scanf("%d",&ptr[i]);
}
printf("Initial array is : ");
for (i=0;i<n;i++) {
printf("%d ",ptr[i]);
}
sort(ptr,n);
printf("Sorted array is : ");
for (i=0;i<n;i++) {
printf("%d ",ptr[i]);
}
return 0;
}
This is my code. I'm trying to sort a pointer array using a function.
Whatever the (int) input, it sorts out fine.
My confusion is that i'm using
ptr[i] < ptr[j]
instead of
ptr[i] > ptr[j]
as it should normally be to sort it ascending.
Why is that?
No, your confusion is misplaced. Look at the for loops, and the relation between i and j. There are times when i < j and times when i > j, so what constitutes being "out of order" and requiring a swap?
The inner loop should start at i+1 not at '0'; that will make the relation between i and j invariant.
Given your loops go from i = 0 .. n and j = 0 .. n, there is no guarantee in your code that i < j.
There's two ways to fix this:
void sort(int *ptr, int n) {
int i,j,tmp;
for (i=0; i<n; i++) {
for (j=0; j<n; j++) {
if (i < j && ptr[i] < ptr[j]) { // Note the changed conditional
tmp=ptr[i];
ptr[i]=ptr[j];
ptr[j]=tmp;
}
}
}
}
or
void sort(int *ptr, int n) {
int i,j,tmp;
for (i=0; i<n; i++) {
for (j=i+1; j<n; j++) { // Note the changed start value
if (ptr[i] < ptr[j]) {
tmp=ptr[i];
ptr[i]=ptr[j];
ptr[j]=tmp;
}
}
}
}
As we can see, you are using bubble sort.
In bubble sort, our main intention is either transfer the heavier element to the end or lighter element to the top.
(ptr[i] < ptr[j])
what you are doing is moving the heavy elements to the end of the array, this is why whenever you are finding ptr[j](j is an inner loop variable) which is bigger than the ptr[i] (outer loop variable), you are doing a swap.
The objective: Add only the pieces of the matrix that are part of a full X (upper and lower triangle).
1 1 1
0 1 0
1 1 1
Like this, middle one should add only once.
I can't add the lower triangle properly. Help much appreciated :)
void write(int niz[20][20], int n){
int i, j;
for(i=0; i<n; i++){
for(j=0; j<n; j++){
scanf("%d", &niz[i][j]);
}
}
}
void x(int niz[20][20], int n){
//Upper triangle
int i, j, pr=n, suma=0;
for(i=0; i<n/2 + n%2; i++,pr--){
for(j=i; j<pr; j++){
suma += niz[i][j];
}
}
printf("%d\n",suma);
//Lower triangle
pr = n;
for(i=n; i>n/2 + n%2; i--,pr--){
printf("%d",pr);
for(j=n-i; j<pr; j++){
printf("\n%d", niz[i][j]);
suma += niz[i][j];
}
}
printf("%d", suma);
}
int main()
{
int n;
printf("Matrix dimensions: ");
scanf("%d", &n);
printf("Numbers in the matrix: \n");
int niz[n][n];
write(niz, n);
x(niz, n);
}
Instead of writing separate functions for each lower, upper & diagonals you can do all together with little tricks, but it works only if row == column and thats's what you want I think.
int main() {
/* it can be anything like a[3][3] or a[7][7] and elements can
be all one or all 2 or any number */
int arr[5][5] = { {1,1,1,1,1},
{0,0,1,0,0},
{0,0,1,0,0},
{0,0,1,0,0},
{1,1,1,1,1} };
int row = sizeof(arr)/sizeof(arr[0]);
int col = sizeof(arr[0])/sizeof(arr[0][0]);
int sum = 0;
for(int index = 0; index < row; index++) {
for(int sub_index = 0; sub_index < col; sub_index++) {
if(index == 0 || (index == row-1) || sub_index == row/2)
sum = sum + arr[index][sub_index];
}
}
printf("sum = %d \n",sum);
return 0;
}
Its fine if it helps you otherwise write your own logic.
There are some mismatches between the declarations and types of the arguments passed to OP's function. While in main they declare a variable length array, named niz:
int n;
// ...
int niz[n][n];
The posted signature of both write and x requires an int niz(*)[20]. It should be changed to:
void write(int n, int niz[n][n]);
// this ^^^ may be a size_t, just remember to write it before the array
About the pattern you have to follow for the sum, I can't say to fully understand your requirement, but if I'm not completely wrong, it could be done this way:
#include <stdio.h>
#include <stdlib.h>
void read_matrix(int n, int niz[n][n])
{
for(int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
scanf("%d", &niz[i][j]);
}
}
}
// Separate the calculation from the printing
int hourglass_sum(int n, int niz[n][n])
{
int sum = 0;
int i = 0;
//Upper triangle
for(int k = n; i < k; ++i, --k) {
for(int j = i; j < k; ++j) {
sum += niz[i][j];
}
}
//Lower triangle
for(int k = i + 1; i < n; ++i, ++k) {
for(int j = n - i - 1; j < k; ++j) {
sum += niz[i][j];
}
}
return sum;
}
int main()
{
int n;
printf("Matrix dimensions: ");
scanf("%d", &n);
int niz[n][n];
read_matrix(n, niz);
printf("\nSum: %d", hourglass_sum(n, niz));
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I've tried to create two matrices and do the product in another matrix, but the compiler gives an error of core dump. The creation of the first two matrices is right; something is wrong with the third matrix.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m;
int n;
int t;
int i;
int **A;
int **B;
int k;
int j;
scanf("%d",&n);
scanf("%d",&m);
scanf("%d",&t);
A=malloc(n*sizeof(int*));
for(i=0;i<n;i++){
A[i]=malloc(m*sizeof(int));
}
for(i=0;i<n;i++){ // A[n][m]
for(j=0;j<m;j++)
{
scanf("%d",&(A[i][j]));
}
}
B=malloc(t*sizeof(int*));
for(i=0;i<t;i++) //B[m][t]
{
B[i]=malloc(n*sizeof(int));
}
for(i=0;i<t;i++){
for(j=0;j<n;j++)
{
scanf("%d",&(B[i][j]));
}
}
int **C;
C=malloc(t*sizeof(int*));
for(i=0;i<t;i++){{A[i]=malloc(m*sizeof(int));}
for(i=0;i<t;i++){
for(j=0;j<m;j++){
C[i][j]=0;
for(k=0;k<n;k++)
{
(C[i][j])=(C[i][j])+((A[k][j])*(B[i][k]));
}
}
}
}
return 0;
}
You must separate memory for C, not for A. That is why when you try to access C[i][j] it generates this error. Change:
for(i=0;i<t;i++){{A[i]=malloc(m*sizeof(int));}
to
for(i=0;i<t;i++){ C[i]=malloc(m*sizeof(int));}
Complete code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m;
int n;
int t;
int i;
int **A;
int **B;
int k;
int j;
scanf("%d",&n);
scanf("%d",&m);
scanf("%d",&t);
A=malloc(n*sizeof(int*));
for(i=0;i<n;i++){
A[i]=malloc(m*sizeof(int));
}
for(i=0;i<n;i++){ // A[n][m]
for(j=0;j<m;j++){
scanf("%d", &(A[i][j]));
}
}
B=malloc(t*sizeof(int*));
for(i=0;i<t;i++) //B[m][t]
{
B[i]=malloc(n*sizeof(int));
}
printf("B\n");
for(i=0;i<t;i++){
for(j=0;j<n;j++)
{
scanf("%d",&(B[i][j]));
}
}
int **C;
C=malloc(t*sizeof(int*));
for(i=0;i<t;i++){
C[i]=malloc(m*sizeof(int));
}
for(i=0;i<t;i++){
for(j=0;j<m;j++){
C[i][j]=0;
for(k=0;k<n;k++)
{
(C[i][j])=(C[i][j])+((A[k][j])*(B[i][k]));
}
}
}
return 0;
}
Previous answers may have fixed the problem that caused the crash, but the code is still broken. It would be nice if the variables had more descriptive names. Since each variable is declared on a separate line, you could make use of the space to provide descriptive comments about the variables.
It is conventional to reference the rows of a matrix first, and then the columns. So, I will diverge from your usage, and say that matrix A has m rows and n columns. B must then have n rows, and it looks like you intend for t to hold the number of columns in B. Then the product C will be a m X t matrix.
Your code started to go wrong when you allocated space for B. You allocated for t rows of n elements, when you should have allocated for m rows of t elements (by your own notation). In my code below, because I have changed the order of m and n, I allocate for n rows of t elements.
Then, for the product matrix, I have allocated for m rows of t elements, where you had allocated for t rows of m elements. The calculation of the elements of the product matrix was also wrong in your code. The [i][j] element of C is the vector dot-product of the ith row of A and the jth column of B. The way you have calculated this element, C[i][j] is the dot-product of the jth column of A and the ith row of B.
Here is the code with corrections. I included some input prompts, and some code to display the entered matrices and the resulting product.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int m; // rows in A
int n; // cols in A
int t; // cols in B
int i;
int **A; // points to first row of A
int **B; // points to first row of B
int **C; // points to first row of C
int k;
int j;
printf("Enter number of rows in A: ");
scanf("%d", &m);
printf("Enter number of columns in A: ");
scanf("%d", &n);
printf("Enter number columns in B: ");
scanf("%d", &t);
A = malloc(sizeof(int*) * m); // A[m][n]
for(i = 0;i < m; i++){
A[i] = malloc(sizeof(int) * n);
}
for(i = 0; i < m; i++){
for(j = 0; j < n; j++)
{
scanf("%d", &(A[i][j]));
}
}
B = malloc(sizeof(int*) * n); // B[n][t]
for(i = 0; i < n; i++)
{
B[i] = malloc(sizeof(int) * t);
}
for(i = 0; i < n; i++){
for(j = 0; j < t; j++)
{
scanf("%d", &(B[i][j]));
}
}
C = malloc(sizeof(int*) * m); // C[m][t]
for(i = 0; i < m; i++){
C[i] = malloc(sizeof(int) * t);
}
for(i = 0; i < m; i++){
for(j = 0; j < t; j++){
C[i][j] = 0;
for(k = 0; k < n; k++)
{
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}
printf("Matrix A:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%-5d", A[i][j]);
}
putchar('\n');
}
printf("Matrix B:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < t; j++) {
printf("%-5d", B[i][j]);
}
putchar('\n');
}
printf("Matrix product:\n");
for (i = 0; i < t; i++) {
for (j = 0; j < m; j++) {
printf("%-5d", C[i][j]);
}
putchar('\n');
}
return 0;
}
Here is the result of a test run:
λ> ./a.out
Enter number of rows in A: 2
Enter number of columns in A: 3
Enter number columns in B: 2
2 3 4
1 3 5
3 4
5 6
7 8
Matrix A:
2 3 4
1 3 5
Matrix B:
3 4
5 6
7 8
Matrix product:
49 58
53 62
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.