?
Solved

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

Posted on 2003-03-31
3
Medium Priority
?
203 Views
Last Modified: 2010-04-15
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;
}
0
Comment
Question by:samkhool4u
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 8237984
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.
0
 

Author Comment

by:samkhool4u
ID: 8280077
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...
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 180 total points
ID: 8281582
>> for ( i = 0 ; i < rows ; i ++ )
>>           for ( j = 0 ; j < columns ; j ++ )
>>          matrix2[i][j] = matrix1[j][i] ; // end fors

for ( i = 0 ; i < transRows ; i ++ )
  for ( j = 0 ; j < transCols ; j ++ )
    matrix2[i][j] = matrix1[j][i] ; // end fors


That'll do it!

Mayank.


0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month11 days, 20 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question