Question

In: Computer Science

Write a function that will accept two integer matrices C and D by reference parameters. The...

Write a function that will accept two integer matrices C and D by reference parameters. The function will compute the transpose of C and store it in D. For your information, the transpose of matrix C is D, where D[j][i] = C[i][j]. [7 marks]

Explain the time complexity of this function inside of the function code as a comment. [3 marks]

in C++

Solutions

Expert Solution

#include <iostream>

using namespace std;
int n=0;
int m=0;

void transpose(int *A, int *B) 
{ 
    int i, j; 
    for (i = 0; i < n; i++) //It will run n times
        for (j = 0; j < n; j++) // it will also run n times
            *((B+i*n)+j)= *((A+j*n)+i); 
    //time complexity Explanation
    /*Since transpose() function has 2 for loops and each loop executes n time, thus it will n*n time in total
    Thus Time complexity of transpose is T(n) = O(n*n) */
} 

int main() {
  cout << "Enter size for Matrix: ";
  cin >> n;
  cout<<"Enter matrix in n×n form:"<<endl;
  int A[n][n];
  int B[n][n];
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cin>>A[i][j];
    }
  }
  transpose((int*)A, (int *)B);
  cout<<"Matrix after transpose is:"<<endl;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      cout<<B[i][j]<<" ";
    }
    cout<<endl;
  }
}

Sample output is as following:


Related Solutions

Write a function that will accept a list of numbers and an integer (n). The function...
Write a function that will accept a list of numbers and an integer (n). The function should return a list containing every nth item from the input list, always starting with the first item in the list. The original list should not be modified. For example, if the function is passed the list [8, 3, 19, 26, 32, 12, 3, 7, 21, 16] and the integer 3, it will return the list [8, 26, 3, 16] If the function is...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the matrices are same or 0 otherwise. Set appropriate parameters and return type if necessary.
C++: Write a reverse function that receives a reference to a integer linked list and reverses...
C++: Write a reverse function that receives a reference to a integer linked list and reverses the order of all the elements in it. For example, if the input linked list is 1 -> 4-> 2-> 3-> 6-> 5}, after processing by this function, the linked list should become 5-> 6-> 3-> 2-> 4-> 1. You need to write a main file to insert elements into the linked list and call the reverseLinkedList() function which takes the reference of first...
Q4. Please write a function Calculate_integer_division(). Your function should accept two parameters num1 and num2 and...
Q4. Please write a function Calculate_integer_division(). Your function should accept two parameters num1 and num2 and return the result of integer division of num1 by num2. So if num1 is 9 and num2 is 2, the function should return 4. Call your function and print out the result. Q5. Please print out the following with the help of a range() function: 200, 400, 600, 800 Q6. Please print 'Go.' if the traffic light is green, 'Wait.' if it's yellow and...
Write in C++ Print the following heading (should be in heading function) Welcome to Reference parameters...
Write in C++ Print the following heading (should be in heading function) Welcome to Reference parameters 2 ) ask the user for 2 numbers (should be in main) 3 ) Calculate the sum and product of the numbers using reference parameters (should be in getAnswer function) 4 ) Print the sum and product of the numbers in the following format: (should be in printmessage function) Here is the function you need        Function name | Parameters                       | Return Type    heading |...
write a C++ function called inc whose parameter is an integer reference type and that increases...
write a C++ function called inc whose parameter is an integer reference type and that increases the parameter by one when it is called.
Write a C++ program to determine the sum of two 1-D matrices: A = [a b...
Write a C++ program to determine the sum of two 1-D matrices: A = [a b c d] B = [e f g h] The user will provide the values of a to h.
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT