Question

Write you code in a way that you can compute any size matrix-vector product.

However, I specifically want you to confirm that the following product works correctly.e output of the matrix-vector program should look like 2.00 1.00 0.00 0.00 0.00 1.00 3.00 4.00 1.00 2.00 1.00 0.00 0.00 1.00

Required to implement these functions: assignMat, assignVec, printMatVec, allocVec, and allocMat.

As well as these function prototypes:

double* mvp1(double* mat, double* vec, int n);

double* mvp2(double** mat, double* vec, int n);

Cannot take user input. Only has to work for a square matrix, so no need for rows/column variables. Just need variable n

Note one takes the matrix as an int* and the other takes it as an int**

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

code

solution

#include <stdio.h> //standard library #include <stdlib.h> double* mvp-1 (double* matrix, double* vector, int n); double* mvp_double *vectoralloc vector (col value) assign_vector (vector, col _value) double *res1mvp 1 (matrix2, vector, col value) doubfor (int i-0; i< row_value; i++) for (int j-o; j<col _value; j++) scanf (Slf, &data) matrix[i]data; v İd free matrix (doublvectorl[ij data; void free_vector (double vectorl, int len1) free (vector1); void print matrix vector (double matrix, doubledouble* mvp_2 (double** matrix, double *vector, int n) double *res = (double*) malloc (n*sizeof (double) ) ; for (int 1-0 ; ¡

//output

2 1 0 0 o 1 2 1 0 o 0 1 2 1 o 0 0 1 2 1 0 0 0 1 2 2.00 1.00 0.00 0.00 0.001.00 3.00 1.00 2.00 1.00 0.00 0.001.00 4.00 0.00 1.

//copyable code

#include <stdio.h>

//standard library

#include <stdlib.h>

double* mvp_1(double* matrix, double* vector,int n);

double* mvp_2(double** matrix,double*vector,int n);

void free_matrix(double **matrix, int row_value, int col_value);

void assign_matrix(double **matrix, int row_value, int col_value);

void assign_vector(double *vector, int len);

void free_vector(double *vector, int len);

void print_matrix_vector(double **matrix, double *vector, double *res, int row_value, int col_value);

double *alloc_vector(int len);

double **alloc_matrix(int row_value, int col_value);

//Main

int main(int argc, char **argv){

    int row_value=5;

    int col_value=5;

    double **matrix1 = alloc_matrix(row_value, col_value);

    double *matrix2 = alloc_vector(row_value*col_value);

    assign_matrix(matrix1, row_value, col_value);

    for(int i1=0; i1<row_value; i1++)

    {

        for(int j1=0; j1<col_value; j1++)

        {

            *(matrix2 + i1*row_value + j1) = matrix1[i1][j1];

      }

    }

    double *vector = alloc_vector(col_value);

    assign_vector(vector, col_value);

    double *res1 = mvp_1(matrix2, vector, col_value);

    double *res2 = mvp_2(matrix1, vector, col_value);

    print_matrix_vector(matrix1, vector, res1, row_value, col_value);

    printf("\n\n");

    print_matrix_vector(matrix1, vector, res2, row_value, col_value);

    free_matrix(matrix1, row_value, col_value);

    free_vector(vector, col_value);

    free_vector(matrix2, row_value*col_value);

    free_vector(res1, col_value);

    free_vector(res2, col_value);

    return 0;

}

double **alloc_matrix(int row_value, int col_value)

{

    double **matrix = (double**)malloc(row_value*sizeof(double*));

    for(int i=0; i<row_value; i++) matrix[i] = (double*)malloc(col_value*sizeof(double));

    return matrix;

}

void assign_matrix(double **matrix, int row_value, int col_value)

{

    double data;

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

    {

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

        {

            scanf("%lf", &data);

            matrix[i][j] = data;

        }

    }

}

void free_matrix(double **matrix, int row_value, int col_value)

{

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

    {

        free(matrix[i]);

    }

    free(matrix);

}

double *alloc_vector(int len1)

{

    double *vector = (double*)malloc(len1*sizeof(double));

    return vector;

}

void assign_vector(double *vector1, int len1)

{

    double data;

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

    {

        scanf("%lf", &data);

        vector1[i] = data;

    }

}

void free_vector(double *vector1, int len1)

{

    free(vector1);

}

void print_matrix_vector(double **matrix, double *vector, double *res, int row_value, int col_value)

{

    int vi = 0;

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

    {

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

        {

            printf("%.2lf ", matrix[i][j]);

        }

        printf(" ");

        if(vi<col_value)

        {

            printf("%.2lf ", vector[vi]);

            vi++;

        }

        if(i==(int)row_value/2)

            printf(" = %.2lf\n", res[i]);

        else printf(" %.2lf\n", res[i]);

    }

}

double* mvp_2(double** matrix, double *vector,int n)

{

    double *res = (double*)malloc(n*sizeof(double));

    for(int i=0; i<n; i++) res[i] = 0;

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

    {

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

        {

            res[i] += matrix[i][j]*vector[j];

        }

    }

    return res;

}

double* mvp_1(double* matrix, double* vector,int n)

{

    double *res = (double*)malloc(n*sizeof(double));

    for(int i=0; i<n; i++) res[i] = 0;

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

    {

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

        {

            res[i] += *(matrix + i*n + j)*vector[j];

        }

    }

    return res;

}

Add a comment
Know the answer?
Add Answer to:
Write you code in a way that you can compute any size matrix-vector product. However, I specifically want you to confirm...
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
  • Instructions Write a C++ program that performs the following: Accept a list of floating point values...

    Instructions Write a C++ program that performs the following: Accept a list of floating point values on a single line (separated by spaces). This is vector A. The user may enter as many values as they desire. Accept a second list of floating point values on a single line (separated by spaces). This is vector B. The user may enter as many values as they desire. There is no requirement that the two vectors have the same number of elements....

  • In this exercise you will work with LU factorization of an matrix A. Theory: Any matrix A can be ...

    In this exercise you will work with LU factorization of an matrix A. Theory: Any matrix A can be reduced to an echelon form by using only row replacement and row interchanging operations. Row interchanging is almost always necessary for a computer realization because it reduces the round off errors in calculations - this strategy in computer calculation is called partial pivoting, which refers to selecting for a pivot the largest by absolute value entry in a column. The MATLAB...

  • I haven't code in awhile and would like some help getting back on my feet. Need to code this in C++. Thank you! In...

    I haven't code in awhile and would like some help getting back on my feet. Need to code this in C++. Thank you! In this homework, you will implement a simple version of the game Battleship, in which the player tries to destroy ships hidden on a map using a system of coordinates. In this program, the map will be represented by a 4x4 matrix of integers. A 1 in the matrix represents the presence of a ship. There are...

  • Write a C++ program that creates the following eight two dimensional random number vector: (a) the...

    Write a C++ program that creates the following eight two dimensional random number vector: (a) the voltage vector V[][] contains n X m voltages between 100 and 1000 Volts, (where n.= #rows, m=# columns) (b) the current vector I[][] contains nXm currents between 10 and 50 Amps, (c) the phase vector PH[][] contains nXm phase angles between 0 and 45 degrees (d) The power vector (units of Watts) is given by: P[][] = V[][]*I[]*cos(PH[][]*M_P1/180); (e) The reaction vector (units of...

  • Demo Your instructor will now debug the below faulty QuickSort example int size 8,W makes code...

    Demo Your instructor will now debug the below faulty QuickSort example int size 8,W makes code simpler void swapint "xp, int yp) yp-semp int partitionlint intArry int left, int right, int pivot) t int left Pointer left in righPointer right; printfi-Calling quick sort, index %d to %d with pivot %d、m"lea, righe, pivot while) N korp increasing left pointer until run into something greater thon pivet W keep decrcasing right pointer until nun into something smaller than piv 1 else t...

  • program in C. please paste code, thanks! CS 153 Program 4 - Stirling Approximation (separate compilation)...

    program in C. please paste code, thanks! CS 153 Program 4 - Stirling Approximation (separate compilation) 1. Write a pure function double factoriallint Ni that computes N! N 1023°4 ... N-1)'N Do this the obvious way by initializing a product to one and then multiplying the integers together using a loop. Zero factorial is defined to be one. Now write a pure function double atining in Ni that uses the Stirling approximation to compute: approx[e!) - V22) In this formula,...

  • Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which...

    Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which prints each character of the given string str on a new line, with each line numbered 1, 2, 3, …. For example calling the function with printNumberedChars("hello"); should output the following: 1. h 2. e 3. l 4. l 5. o Q2. Write a recursive function in C++ int sumArray(const int* arr, unsigned int size) { ... } which takes an array of integers,...

  • C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) L...

    C Programming write two functions, similar to what you see in the sample program. The first will ask the user to enter some information (I have included the type in parentheses) First Name (char[]) Last Name (char[]) Age (int) Height in Inches (double) Weight in Pounds (double) You will use pass-by-reference to modify the values of the arguments passed in from the main(). Remember that arrays require no special notation, as they are passed by reference automatically, but the other...

  • Write a program in C++ that simulates a soft drink machine. The program will need several...

    Write a program in C++ that simulates a soft drink machine. The program will need several classes: DrinkItem, DrinkMachine and Receipt. For each of the classes you must create the constructors and member functions required below. You can, optionally, add additional private member functions that can be used for doing implementation work within the class. DrinkItem class The DrinkItem class will contains the following private data members: name: Drink name (type of drink – read in from a file). Type...

  • COP 2220 Into programming in C(Answer in C, not C++). The question is: Please write the...

    COP 2220 Into programming in C(Answer in C, not C++). The question is: Please write the algorithm for a AppStore programming assignment. The details of the assignment, a sample algorithm, and its outline, are located below. Make the algorithm at least half a page, easy to read, and to understand, how it works. I just want the algorithm, not the programming code, I have that. I only need the ALGORITHM. Thanks for helping me out, I apprieciate it, have a...

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