I am a beginner in programming. Learning 2D arrays in C now. I encountered a question asking to transpose a 2D matrix (i.e a 2D array). The book's suggested answer suggested using another array.
I was trying to find a way to do the same without the other array.
Here's my code:
#include <stdio.h>
#include <stdlib.h>
void printAr(int[3][3]);
int main()
{
int A[3][3];
printf("Enter the numbers: \n");
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
scanf("%d",&A[i][j]);
}
}
printf("\n-------------------------------\n");
printAr(A);
printf("Now transposing------------------\n");
for(int f=0;f<3;f++)
{
for(int h=0;h<3;h++)
{
int t=A[f][h];
A[f][h]=A[h][f];
A[h][f]=t;
}
}
printAr(A);
return 0;
}
void printAr(int B[3][3])
{
for(int k=0;k<3;k++)
{
for(int l=0;l<3;l++)
{
printf("%d ",B[k][l]);
}
printf("\n");
}
}
Here is the output:
Enter the numbers: 1 2 3 4 5 6 7 8 9 ------------------------------- 1 2 3 4 5 6 7 8 9 Now transposing------------------ 1 2 3 4 5 6 7 8 9
What am i doing wrong? And why can't it transpose it?