Link to home
Start Free TrialLog in
Avatar of samkhool4u
samkhool4u

asked on

HELP A NEWBIE... TRANSPOSE OF A MATRIX..

hello everyone!!
 first let me tell you that iam newbie.. ive just started this a few months ago.. ( so if u could please put appropraite detailed comments.. u will be helping a newbie understand it better... thankyou.
 iam trying to produce a transpose of a matrix.

(the transpose of a matrix is obtained by exchanging the elements of each row witht the elements of the corresponding cloumn).

The program should ask the user to enter the number of rows and columns of a matrix. it then asks to enter matrix row by row. Once all values are read from the keyboard, it prints the orignal matrix entered by the user and its transpose in an appropriate format. finally it asks the user if the transpose of another matrix is required . the program keeps repeating this process until the user enters n or N ans the terminates.

p.s .. ihave to make use of functions and validation..e.g it  should not accept the matrix 1x1.. cause it is invalid.


so far ive  done. this.. but i got lots of buggs in it.. if any one could please help me here ... i would be greatful...  thank you.....

 /*exam_prac.c*/
/*Multiplication of matrices*/
    date:3-march-2003*/
#include<stdio.h>
int matrix1[10][10],i;
int rows,columns;



int transpose()
{
     /*-------------------  MATRIX  -------------------------------------*/
               rows=rows-1;
     for(i=0;i<=columns;i++)
     {
               printf("\nEnter valus for row %d\n",i);
               columns=columns-1;
            for(i=0;i<=columns;i++)
               {
               scanf("%d%d",& matrix1[i][columns]);
               }
     }
     printf("\nColumns and rows");

     for(i=0;i<=columns;i++)
      {
                     for(i=0;i<=columns;i++)
                           {
                              printf("\n%d", matrix1[i][columns]);
                           }
       }
/*---------------------------------------------------------------------*/

}




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);

      transpose();

          printf("\nThe transpose of the matrix is\n ");

     for(i=0;i<=column;i++)
       {
          printf("\n%d%d",matrix1[0][i],matrix1[1][i]);

       }

 return 0;
}
Avatar of Mayank S
Mayank S
Flag of India image

Sam,

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][i],matrix1[1][i]);

>>      }

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.
Avatar of samkhool4u
samkhool4u

ASKER

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
-----------------------------------------------------------  please help me i will grateful...
                  thanx...
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial