Question

Given a 2d matrix of size N x M (which must be read from a file...

Given a 2d matrix of size N x M (which must be read from a file provided to you), transpose it into a M x N matrix where N is the number of rows and M is the number of columns.

void transpose(int n, int m, const int A[n][m], int AT[m][n]);

Write a main() program to test and demonstrate your function. Show your function works for, at minimum, 3x6 and 10x6 matrices.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C program to transpose a matrix

#include <stdio.h>

#include <stdlib.h>

// function to transpose a matrix A and store the result in AT

void transpose(int n, int m, const int A[n][m], int AT[m][n])

{

               for(int i=0;i<n;i++)

               {

                              for(int j=0;j<m;j++)

                                             AT[j][i] = A[i][j];

               }

}

// test the above function

int main() {

               int n = 3, m= 6;

               int A1[3][6], AT1[6][3];

               int num =1;

               for(int i=0;i<n;i++)

               {

                              for(int j=0;j<m;j++)

                              {

                                             A1[i][j] = num;

                                             num++;

                              }

               }

               printf("\nOriginal Matrix: \n");

               for(int i=0;i<n;i++)

               {

                              for(int j=0;j<m;j++)

                                             printf("%d ",A1[i][j]);

                              printf("\n");

               }

               transpose(n,m,A1,AT1);

               printf("\nTranspose Matrix: \n");

               for(int i=0;i<m;i++)

               {

                              for(int j=0;j<n;j++)

                                             printf("%d ",AT1[i][j]);

                              printf("\n");

               }

               n = 10, m= 6;

               int A2[n][m], AT2[m][n];

               num =1;

               for(int i=0;i<n;i++)

               {

                              for(int j=0;j<m;j++)

                              {

                                             A2[i][j] = num;

                                             num++;

                              }

               }

               printf("\n\nOriginal Matrix: \n");

               for(int i=0;i<n;i++)

               {

                              for(int j=0;j<m;j++)

                                             printf("%d ",A2[i][j]);

                              printf("\n");

               }

               transpose(n,m,A2,AT2);

               printf("\nTranspose Matrix: \n");

               for(int i=0;i<m;i++)

               {

                              for(int j=0;j<n;j++)

                                             printf("%d ",AT2[i][j]);

                              printf("\n");

               }

               return 0;

}

//end of program

Output:

Add a comment
Answer #2

#include<stdio.h>
void transpose(int n, int m, const int A[n][m], int AT[m][n])
{
   int i,j;
   for(i=0;i<m;i++)
   {
       for(j=0;j<n;j++)
       {
           AT[i][j]=A[j][i];
       }
   }
   for(i=0;i<m;i++)
   {
       for(j=0;j<n;j++)
       {
           printf("%d ",AT[i][j]);
       }
       printf(" \n");
   }
}
int main()
{
   FILE *fp;
    int i,j,n=10,m=6;
   int A[n][m],AT[m][n];
    fp = fopen("transpose.txt", "r");

    if (fp == NULL)
    {
        printf("Can't open file for reading.\n");
    }
    else
    {
       for(i=0;i<n;i++)
       {
           for(j=0;j<m;j++)
           {
               fscanf(fp, "%d", &A[i][j]);
           }
       }
        fclose(fp);
    }
    for(i=0;i<n;i++)
       {
           for(j=0;j<m;j++)
           {
               printf("%d ",A[i][j]);
           }
           printf(" \n");
       }
       printf("\n\n\nTranspose=\n\n");
   transpose(n, m, A, AT);
   printf("\n\nsecond testcase\n\n");
   n=3;
    for(i=0;i<n;i++)
    {
           for(j=0;j<m;j++)
           {
               fscanf(fp, "%d", &A[i][j]);
           }
   }
   for(i=0;i<n;i++)
       {
           for(j=0;j<m;j++)
           {
               printf("%d ",A[i][j]);
           }
           printf(" \n");
       }
       printf("\n\n\nTranspose=\n\n");
   transpose(n, m, A, AT);

}
  

SCREENSHOTS


}

Input File 2D array

Add a comment
Know the answer?
Add Answer to:
Given a 2d matrix of size N x M (which must be read from a file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ must use header files and implementation files as separate files. I’ll need a header file,...

    C++ must use header files and implementation files as separate files. I’ll need a header file, implementation file and the main program file at a minimum. Compress these files into one compressed file. Make sure you have adequate documentation. We like to manipulate some matrix operations. Design and implement a class named matrixMagic that can store a matrix of any size. 1. Overload the addition, subtraction, and multiplication operations. 2. Overload the extraction (>>) and insertion (<<) operators to read...

  • Write a function Transpose that transposes a matrix T with M rows and N colums. The...

    Write a function Transpose that transposes a matrix T with M rows and N colums. The transposed matrix, TT, should have N rows and M columns. Example. Given the matrix T. (C++14) Example: 1 2 3 0 -6 7 The transposed matrix TT is 1 0 2 -6 3 7 DEFAULT CODE: Add to code as needed: #include <iostream> #include <string> #include <cmath> using namespace std; void Transpose(int T[100][100], int TT[100][100], int M, int N) { int i; int j;...

  • I only need the "functions" NOT the header file nor the main implementation file JUST the impleme...

    I only need the "functions" NOT the header file nor the main implementation file JUST the implementations for the functions Please help, if its difficult to do the complete program I would appreciate if you could do as much functions as you can especially for the derived class. I am a beginer so I am only using classes and pointers while implementing everything using simple c++ commands thank you in advanced Design and implement two C++ classes to provide matrix...

  • C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional...

    C++ Write a program to calculate the sum of two matrices that are stored inside two-dimensional arrays. Your program should include three functions: one for getting input data, one for calculating the sum of matrices, and one for printing the result. a) Function inputMatrix: This Function prompts the user to enter the data and stores data inside two-dimensional array. b) Function addMatrices: This function calculatesthe sum result of two matrices. c) Function printMatrix: This function prints the matrix to screen....

  • I'm having trouble sorting this square matrix (a 2d array that has number of rows and...

    I'm having trouble sorting this square matrix (a 2d array that has number of rows and same number of column n x n) using row-wise approach in C programming. Please help me program this in C to sort it using row-wise approach, here is the code: #include <stdio.h> #define MAX 100 int main() { int mat[MAX][MAX]; int i, j, m, n; int rowsum, columnsum, diagonalsum; int k; int magic = 0; int transpose[MAX][MAX]; printf("Enter the # of rows and columns...

  • Using C programming

    Write another function called checkdiag2 (int checkdiag2 (int matrix[][100], int size)) that will return 1 if all the numbers on the antidiagonal are the same and 0 otherwise.Rewrite your main program to use 2D dynamic allocation instead for your array. Your array will have rows and columns depending of the first integer read from the file.Call your function from the main program. A typical report would look like The matrix is 8x8 and all the numbers on the antidiagonal are the same.Check your...

  • Debug the following matrix program in C: // Program to read integers into a 3X3 matrix...

    Debug the following matrix program in C: // Program to read integers into a 3X3 matrix and display them #include <stdio.h> void display(int Matrix[3][3],int size); int main(void) {         char size;         double Matrix[size][size+1];         printf("Enter 9 elements of the matrix:\n");         int i;         for (i = 0: i <= size: i++)     {       int j = 0;       for (; j <= size++; j++){         scanf("%d", matrix[i--][4])       }     }         Display(Matrix,9);         return 0; void...

  • Language is C++ Basic principles and theory of structured programming in C++ by implementing the class:...

    Language is C++ Basic principles and theory of structured programming in C++ by implementing the class: Matrix Use these principles and theory to help build and manipulate instances of the class (objects), call member functions Matrix class implementation from the main function file and, in addition, the Matrix class must have its interface(Matrix.h) in a separate file from its implementation (Matrix.cpp). The code in the following files: matrix.h matrix.cpp matrixDriver.h Please use these file names exactly. Summary Create three files...

  • Using C programming...

    Write a text file contains a square matrix of integer numbers. The file contains an integer number indicating the identical number of rows and columns followed by the data itself (that number cannot be larger than 100). For example, a file containing a 3x3 matrix would contain 3 followed by 9 other integer numbers. The program will call one function to determine if all the numbers on the main diagonal are the same.The function is called checkdiag (int checkdiag (int matrix[][100], int...

  • Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT