Question

What to do Write a function with the following signature: float* matrix multiplication(float* left, float* right, int rows, int shared, int columns); The first two arguments are two pointers to the beginning of two matrices with the dimensions (rows,shared) and (shared, columns) correspondingly. The remaining arguments specify these dimensions. The return value will return a pointer to the very beginning of the result matrix. That said, you need to provide the space for the result matrix by dynamically allocating memory within your function. What to submit I ask you to submit the following: Submit your well-structured, well-documented code for the above function. Submit a driver that tests your code as follows: oHard-code the following two matrices into the driver code and then run your function: Left matrix: float left 2)13 (1,2,3),14,5,6) Right matrix: float right[3114 87,8,9,1011,12,13,14),115,16,17,18) Finally, print out the result matrix on the standard output two-dimentionally, i.e. in its natural appearance. Submit a screen shot that shows the printout of your result matrix. Hint Use malloc0 as discussed in class in order to allocate the piece of memory that is going to accommodate your result matrixWrite in C code

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

# include <stdio.h>
# include <stdlib.h>

float* matrix_multiplication (float*left, float*right,int rows,int shared,int columns) // function to multiply
{
   int i,j,c,d,k;
   float val1,val2,sum=0.00;
   float* res=(float *)malloc(rows*columns*sizeof(float)); // alocatting for the result
   for (i=0;i<rows;i++) // initializing the values with 0 : optional step
   {
       for (j=0;j<columns;j++)
       {
           *(res+i*columns+j)=0;
       }
   }

   for (c = 0; c < rows; c++) { // main process of multiplication begins
      for (d = 0; d < columns; d++) {
        for (k = 0; k < shared; k++) {
          val1=*(left+c*shared+k); //computing the elements
          val2=*(right+k*shared+d);
          sum = sum + val1*val2;
        }
        //printf("%f\n",sum);
        *(res+c*columns+d)= sum; // computing the products
        sum = 0.00; // reinitializing sum
      }
    }

    return res;
}

void main ()
{
   int m,n,p,q,i,j;
   printf("enter the first matrrix dimension\n"); // entering the first mat dimension
   scanf("%d",&m);
   scanf("%d",&n);
   printf("enter the second matrrix dimension\n"); // entering 2nd mat dimension
   scanf("%d",&p);
   scanf("%d",&q);
   float* l=(float *)malloc(m*n*sizeof(float)); // alocating for 1st mat
   float* r=(float *)malloc(p*q*sizeof(float)); // allocating for 2nd mat
   float* result=(float *)malloc(m*q*sizeof(float)); // alolocating for the result

   printf("enter the first matrix\n"); // entering the first matrix
   for (i=0;i<m;i++)
   {
       for (j=0;j<n;j++)
       {
           scanf("%f",(l+i*n+j));
       }
   }
   printf("enter the 2nd matrix\n"); // entering the second matrix
   for (i=0;i<p;i++)
   {
       for (j=0;j<q;j++)
       {
           scanf("%f",(r+i*q+j));
       }
   }
   printf(" the first matrix\n"); // display the first matrix
   for (i=0;i<m;i++)
   {
       for (j=0;j<n;j++)
       {
           printf("%f\t",*(l+i*n+j));
       }
       printf("\n");
   }

   printf(" the second matrix\n"); // display the second matrix
   for (i=0;i<p;i++)
   {
       for (j=0;j<q;j++)
       {
           printf("%f\t",*(r+i*q+j));
       }
       printf("\n");
   }

   result=matrix_multiplication(l,r,m,n,q); // calling the function

   printf(" the result matrix\n"); // display the resultant
   for (i=0;i<m;i++)
   {
       for (j=0;j<q;j++)
       {
           printf("%f\t",*(result+i*q+j));
       }
       printf("\n");
   }
}

output:

enter the first matrrix dimension
2
3
enter the second matrrix dimension
3
4
enter the first matrix
1
2
3
4
5
6
enter the 2nd matrix
7
8
9
10
11
12
13
14
15
16
17
18
the first matrix
1.000000   2.000000   3.000000  
4.000000   5.000000   6.000000  
the second matrix
7.000000   8.000000   9.000000   10.000000  
11.000000   12.000000   13.000000   14.000000  
15.000000   16.000000   17.000000   18.000000  
the result matrix
66.000000   72.000000   78.000000   84.000000  
156.000000   171.000000   186.000000   201.000000  

Add a comment
Know the answer?
Add Answer to:
Write in C code What to do Write a function with the following signature: float* matrix...
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
  • In this assignment, you are asked to: 1. create a Matrix class that stores and operate...

    In this assignment, you are asked to: 1. create a Matrix class that stores and operate on a dynamic two-dimensional array. The class has the following structure: Private member variables: - int ** mat; - int rows; - int cols; Public member functions: +Default constructor: sets both rows and cols to 3 and creates 3x3 matrix dynamically +Parameterized constructor: sets the rows and cols to the values passed to the constructor and create a matrix with the given dimensions. +Destructor:...

  • Here's the main function of a C program that Reads in two matrices by prompting the...

    Here's the main function of a C program that Reads in two matrices by prompting the user for their dimensions, dynamically allocating memory from the heap, and reading the values into the memory using row major ordering for the matrix values. Multiplies the two matrices together and puts the result into another dynamically allocated piece of memory (after checking that the dimensions are appropriate for matric multiplication). Outputs the two input and the result matrices. Write the implementations of the...

  • 1. Write a MATLAB function that takes a matrix, a row number and a scalar as...

    1. Write a MATLAB function that takes a matrix, a row number and a scalar as arguments and multiplies each element of the row of the matrix by the scalar returning the updated matrix. 2. Write a MATLAB function that takes a matrix, two row numbers and a scalar as arguments and returns a matrix with a linear combination of the rows. For example, if the rows passed to the function were i and j and the scalar was m,...

  • Write your own matlab code to perform matrix multiplication. Recall that to multiply two matrices, the...

    Write your own matlab code to perform matrix multiplication. Recall that to multiply two matrices, the inner dimensions must be the same. Write this as a function with input arguments the matrices to be multiplied and output argument the resultant matrix, use "assert" statement(s) to check that the input arguments are valid and print an error message if they are not. Test with A=[1,2,3;4,5,6] and B=[7,8;9,10;11,12] showing results for: (a) A*B, (b) B*A, and (c) A*A.

  • For the program described below, document your code well. Use descriptive identifier names. Use s...

    For the program described below, document your code well. Use descriptive identifier names. Use spaces and indentation to improve readability. Include a beginning comment block as well as explanatory comments throughout. In the beginning comment block, add your name, program name, date written, program description. The description briefly describes what the program does. Include a comment at the beginning of each method to describe what the method does. Write a program to perform operations on square matrices (i.e. equal number...

  • For the program described below, document your code well. Use descriptive identifier names. Use spaces and...

    For the program described below, document your code well. Use descriptive identifier names. Use spaces and indentation to improve readability. Include a beginning comment block as well as explanatory comments throughout. In the beginning comment block, add your name, program name, date written, program description. The description briefly describes what the program does. Include a comment at the beginning of each method to describe what the method does. Write a program to perform operations on square matrices (i.e. equal number...

  • Please the write code using Matlab 8. Write your own code to perform matrix multiplication. Recall...

    Please the write code using Matlab 8. Write your own code to perform matrix multiplication. Recall that to multiply two matrices, the inner dimensions must be Every element in the resulting C matrix is obtained by: Your code must be a function file and it must check to see if matrix multiplication can be performed on the provided matrices. Test your code with the following matrices: 4 4 2 9 -3 A2 17

  • 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....

  • Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix...

    Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix algebra: A vector is a one-dimensional set of numbers, such as [42 9 20]. The dot product of two equal-length vectors A and B is computed by multiplying the first entry of A by the first entry of B, the second entry of A by the second entry of B, etc., and then summing these products. For example, the dot product of [42 9...

  • Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_RO...

    Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_ROWS = 10; const int MAX_COLS = 10; class MatrixType { public: MatrixType(); void MakeEmpty(); void SetSize(int rowsSize, int colSize); void StoreItem(int item, int row, int col); void Add(MatrixType otherOperand, MatrixType& result); void Sub(MatrixType otherOperand, MatrixType& result); void Mult(MatrixType otherOperand, MatrixType& result); void Print(ofstream& outfile); bool AddSubCompatible(MatrixType otherOperand); bool MultCompatible(MatrixType otherOperand);...

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