hey mayank thanks for ur reply....
i tired ur suggestion but iam facing a minor difficulty
my program now looks like this
--------------------------
/*exam_prac.c*/
/*Transposition of matrices*/
/*date:3-march-2003*/
#include<stdio.h>
int matrix1[10][10],i,j;
int rows,columns;
int matrix2[10][10], transRows, transCols ;
int main()
{
int row,column;
printf("\n PLEASE ENTER THE NUMBER OF ROWS");
scanf("%d",&rows);
printf("PLEASE ENTER THE NUMBER OF COLUMNS");
scanf("%d",&columns);
// for inputting the values,
for ( i = 0 ; i < rows ; i ++ )
{
for ( j = 0 ; j < columns ; j ++ )
{
printf ( "\n Enter value at row %d, column %d: ", i+1, j+1) ;
scanf ( "%d", &matrix1[i][j] ) ;
} // end fors
}
for ( i = 0 ; i < rows ; i ++ )
{
for ( j = 0 ; j < columns ; j ++ )
printf ( "%d\t", matrix1[i][j] ) ; // end nested for
printf ( "\n" ) ; // display new row in the next line
} // end outer for
printf("\n");
//And lastly, to find the transpose of a matrix, I'd better keep another matrix as:
transRows = columns ;
transCols = rows ; // interchange the number of rows and columns
for ( i = 0 ; i < rows ; i ++ )
for ( j = 0 ; j < columns ; j ++ )
matrix2[i][j] = matrix1[j][i] ; // end fors
// the element at [i][j] becomes the element at [j][i] in the transpose
//And displays it, as:
for ( i = 0 ; i < transRows ; i ++ )
{
for ( j = 0 ; j < transCols ; j ++ )
printf ( "%d\t", matrix2[i][j] ) ; // end nested for
printf ( "\n" ) ; // display new row in the next line
} // end outer for
return 0;
}
--------------------------
i tested it ..
2x2 works fine 3x3 fine 4x4 fine 5x5 fine and so on.. but
if i try.. to do 2x3 and 3x2.. 5x2 4x2 and so on ... u get the idea..
the results were
--------------------------
PLEASE ENTER THE NUMBER OF ROWS4
PLEASE ENTER THE NUMBER OF COLUMNS2
Enter value at row 1, column 1: 1
Enter value at row 1, column 2: 2
Enter value at row 2, column 1: 3
Enter value at row 2, column 2: 4
Enter value at row 3, column 1: 5
Enter value at row 3, column 2: 6
Enter value at row 4, column 1: 7
Enter value at row 4, column 2: 8
1 2
3 4
5 6
7 8
1 3 0 0
2 4 0 0
--------------------------
thanx...
Main Topics
Browse All Topics





by: mayankeaglePosted on 2003-03-31 at 04:29:49ID: 8237984
Sam,
[i],matrix 1[1][i]);
First of all, when you're reading the number of rows and columns, then please use the range: [0 to rows -1, 0 to columns - 1], unlike: [0 to rows, 0 to columns] as you've done, because you're taking in an extra row and an extra column this way.
Next, a matrix is not displayed as:
>> for(i=0;i<=column;i++)
>> {
>> printf("\n%d%d",matrix1[0]
>> }
You need a nested loop, the outer one will run through rows and the inner one htrough columns (of course, it can also be done the other way round):
for ( i = 0 ; i < rows ; i ++ )
{
for ( j = 0 ; j < columns ; j ++ )
printf ( "%d\t", matrix1[i][j] ) ; // end nested for
printf ( "\n" ) ; // display new row in the next line
} // end outer for
Also, for inputting the values, use:
for ( i = 0 ; i < rows ; i ++ )
for ( j = 0 ; j < columns ; j ++ )
{
printf ( "\n Enter value at row %d, column %d: ", row + 1, column + 1 ) ;
scanf ( "%d", &matrix1[i][j] ) ;
} // end fors
And lastly, to find the transpose of a matrix, I'd better keep another matrix as:
int matrix2[10][10], transRows, transCols ;
transRows = columns ;
transCols = rows ; // interchange the number of rows and columns
for ( i = 0 ; i < rows ; i ++ )
for ( j = 0 ; j < columns ; j ++ )
matrix2[i][j] = matrix1[j][i] ; // end fors
// the element at [i][j] becomes the element at [j][i] in the transpose
And display it, as:
for ( i = 0 ; i < transRows ; i ++ )
{
for ( j = 0 ; j < transCols ; j ++ )
printf ( "%d\t", matrix2[i][j] ) ; // end nested for
printf ( "\n" ) ; // display new row in the next line
} // end outer for
Hope that helps!
Mayank.