Question

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 functions input_matrix, matrix_multiply, and output_matrix (6.0%).

/*---------------------------------------------------------------------------*/
int main(void) {

    double *m1,*m2,*m3;
    int m1_rows,m1_columns,m2_rows,m2_columns;

    if (((m1 = input_matrix(&m1_rows,&m1_columns,"Matrix 1")) != NULL) &&
((m2 = input_matrix(&m2_rows,&m2_columns,"Matrix 2")) != NULL) &&
((m3 = malloc(m1_rows*m2_columns*sizeof(double))) != NULL)) {
        printf("Matrix 1\n");
        output_matrix(m1,m1_rows,m1_columns);
        printf("Matrix 2\n");
        output_matrix(m2,m2_rows,m2_columns);
        if (matrix_multiply(m1,m1_rows,m1_columns,m2,m2_rows,m2_columns,m3)) {
            printf("Product\n");
            output_matrix(m3,m1_rows,m2_columns);
            free(m1);
            free(m2);
            free(m3);
            return(0);
        } else {
            printf("Error in dimensions\n");
            free(m1);
            free(m2);
            free(m3);
            return(-1);
        }
    } else {
        free(m1);
        free(m2);
        free(m3);
        printf("Error allocating memory\n");
        return(-2);
    }
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <stdio.h>

#include <stdlib.h>

double *input_matrix(int *r, int *c, char *name);

int matrix_multiply(double *m1, int m1_rows, int m1_columns, double *m2, int m2_rows, int m2_columns, double *m3);

void output_matrix(double *m, int rows, int columns);

int main(void)

{

double *m1, *m2, *m3;

int m1_rows, m1_columns, m2_rows, m2_columns;

if (((m1 = input_matrix(&m1_rows, &m1_columns, "Matrix 1")) != NULL) &&

((m2 = input_matrix(&m2_rows, &m2_columns, "Matrix 2")) != NULL) &&

((m3 = malloc(m1_rows * m2_columns * sizeof(double))) != NULL))

{

printf("Matrix 1\n");

output_matrix(m1, m1_rows, m1_columns);

printf("Matrix 2\n");

output_matrix(m2, m2_rows, m2_columns);

if (matrix_multiply(m1, m1_rows, m1_columns, m2, m2_rows, m2_columns, m3))

{

printf("Product\n");

output_matrix(m3, m1_rows, m2_columns);

free(m1);

free(m2);

free(m3);

return (0);

}

else

{

printf("Error in dimensions\n");

free(m1);

free(m2);

free(m3);

return (-1);

}

}

else

{

free(m1);

free(m2);

free(m3);

printf("Error allocating memory\n");

return (-2);

}

}

double *input_matrix(int *r, int *c, char *name)

{

printf("Enter %s rows and columns: ", name);

scanf("%d %d", r, c);

double *m = (double *)malloc((*r) * (*c) * sizeof(double));

int i, j;

printf("Enter %s elements: ", name);

for (i = 0; i < *r; i++)

{

for (j = 0; j < *c; j++)

{

scanf("%lf", &m[i * (*c) + j]);

}

}

return m;

}

int matrix_multiply(double *m1, int m1_rows, int m1_columns, double *m2, int m2_rows, int m2_columns, double *m3)

{

if(m1_columns!=m2_rows)

return 0;

int i, j, k;

for (i = 0; i < m1_rows; i++)

{

for (j = 0; j < m2_columns; j++)

{

double sum = 0;

for (k = 0; k < m2_rows; k++)

{

sum = sum + m1[i * m1_columns + k] * m2[k * m2_columns + j];

}

m3[i * m2_columns + j] = sum;

}

}

}

void output_matrix(double *m, int rows, int columns)

{

int i, j;

for (i = 0; i < rows; i++)

{

for (j = 0; j < columns; j++)

{

printf("%.2lf ", m[i * columns + j]);

}

printf("\n");

}

}

Add a comment
Know the answer?
Add Answer to:
Here's the main function of a C program that Reads in two matrices by prompting the...
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
  • Hi, I need to make a program in C that reads the type of currency and...

    Hi, I need to make a program in C that reads the type of currency and organizes them into stacks based on currency which can be read back. This is what I have so far, can I get help finishing it? #include <stdio.h> #include <stdlib.h> const float POUND = 1.31; const float YEN = 0.0091; const float RUPEE = 0.014; const float EURO = 1.11; char c; int currValue; float exchangeValue; float finValue; int printValue; struct node {    int...

  • The C++ program below contains a function that adds a specific row of two matrices and...

    The C++ program below contains a function that adds a specific row of two matrices and store the result in the corresponding row of a third matrix. This is done by a loop to invokes a function that adds a single row of the two matrices. By the end of the loop all rows should be added and stored in the result matrix. The rows are added sequentially one after the other. You are required to modify the program below...

  • Write in C code What to do Write a function with the following signature: float* matrix...

    Write in C code 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...

  • Need help with shuffle function and give 8 cards to user and computer from shuffle deck?...

    Need help with shuffle function and give 8 cards to user and computer from shuffle deck? #include <stdio.h> #include <stdbool.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <time.h> typedef struct card_s{     char suit;     int face;     struct card_s *listp; } card; void card_create(card* thisNode, char cardSuit, int cardFace, card* nextLoc) {     thisNode->suit = cardSuit;     thisNode->face = cardFace;     thisNode->listp = nextLoc;     return; } void card_insertAfter(card* thisNode, card* newNode) {     card* tmpNext = NULL;    ...

  • Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin,...

    Convert the C program into a C++ program.Replace all C input/output statements with C++ statements (cin, cout, cin.getline) . Re-make the func function by the following prototype: void func( double, double &); #include int user_interface(); double func(double); void print_table(); int main (int argc, char *argv[]) {    print_table(user_interface());    return 0 ; } int user_interface(int val){    int input = 0;    printf("This function takes in x and returns an output\n");    printf("Enter Maximum Number of X:");    scanf("%d", &input);...

  • ****Using C and only C**** I have some C code that has the function addRecord, to...

    ****Using C and only C**** I have some C code that has the function addRecord, to add a record to a linked list of records. However, when I run it, the program exits after asking the user to input the address. See picture below: Here is my code: #include<stdio.h> #include<stdlib.h> struct record { int accountno; char name[25]; char address[80]; struct record* next; }; void addRecord(struct record* newRecord) //Function For Adding Record at last in a SinglyLinkedList { struct record *current,*start,*temp;...

  • 1) Write a script(in Bash shell) that characterizes the application performance. Specifically, your script should automatically...

    1) Write a script(in Bash shell) that characterizes the application performance. Specifically, your script should automatically test both algorithms of the matrix math program for each of the array sizes shown in Table. Also, extend your script to automatically parse the output of the program. Table (Array Sizes in Test Suite) Algorithm 1 Algorithm 2 256 256 512 512 768 768 1024 1024 1280 1280 1536 1536 1792 1792 2048 2048 C source file // Adapted from https://gustavus.edu/+max/courses/F2011/MCS-284/labs/lab3/ // Max...

  • 1) Implement a C program that multiplies two matrices with dimension n x m and m...

    1) Implement a C program that multiplies two matrices with dimension n x m and m x r (n, m, r are provided by the user as argument to the main function. The elements of the matrix are generated randomly. Test the program. example: matmult 2 3 4 will multiply two matrices of dimensions 2 x 3 and 3 x 4, the elements are generated randomly. 2) Determine the highest dimension(s) for which the program will crash 3) Please Explain...

  • Q6 Common Mistakes in C 5 Points Each of the following code samples below is making...

    Q6 Common Mistakes in C 5 Points Each of the following code samples below is making a common mistake in C. From the options: probable segfault, double free, incorrect use of free, logic error, memory leak, and no error select the error type that best categorizes the mistake. You can assume all the code has the necessary #include macros. Also you should assume that the system is 32-bit. 1) #DEFINE PASSWORD "correct horse battery staple" char *check_permissions (char *user_guess) {...

  • C LANGUAGE I just need the void push() function, which inserts new node to the front...

    C LANGUAGE I just need the void push() function, which inserts new node to the front of the list #include<stdio.h> #include<stdlib.h> typedef struct node { int data; struct node *next; } Node; //Creating head a as a global Node* Node *head; /* Given a node prev_node, insert a new node after the given prev_node */ void insertAfter (Node * prev_node, int new_data) { /*1. check if the given prev_node is NULL */ if (prev_node == NULL) { printf ("the given...

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