Question
Create a program in C that prompts the user to enter two file names, opens the files, and reads the data into dynamically allocated arrays
Create a program in C that prompts the user to enter two file names, opens the files, and reads the data into dynamically allocated arrays based on the size of the data.
The first file will contain a matrix of arbitrary size. The format of the file will consist of two lines with a single integer on each line. These two values will represent the number of rows and columns respectively. The remainder of the file will consist of a comma separated array of floating point values. This will store the matrix.
The second file will be similar to the first, but will contain a single integer on the first line representing the number of rows in the file. Each subsequent line will contain a single floating point number. This file will store the vector.
The files can be generated in Excel and saved as CSV (comma separated value) files. If using a non-Windows based computer, save in "Windows CSV" format.
Once the data has been successfully read into the dynamic arrays, perform the matrix-vector multiply using equations and a description provided. Write the result out to a file called "output.csv."
Use MATLAB to verify that the matrix vector product is correct.
If completed correctly, the program will successfully read and parse the input as well as produce the matrix-vector product.
//Multiiplying a Matrix by a Vector
#include
int main() { char filenameMatrix[30]; // name of file1 char filenameVector[30]; // name of file2
// represents rows & columns of matrix and size of vector int r, c, n;
// represents 2d array of matrix int **mat; // represents 1d array of vector int *vect; // represents 1d array of output int *result; // temporary variables int i,j;
//prompt the user to enter filenames printf("Enter the file name containing matrix: "); scanf("%s",filenameMatrix); printf("Enter the file name containing vector: "); scanf("%s",filenameVector);
//reading from matrix file FILE* file = fopen (filenameMatrix, "r"); if(file==NULL) printf("Error in opening Matrix file."); fscanf (file, "%d ", &r); // get number of rows fscanf (file, "%d ", &c); // get number of columns
// dynamically allocating memory to matrix mat = (int **)malloc(sizeof(int *) * r); mat[0] = (int *)malloc(sizeof(int) * c * r); for(i = 0; i < r; i++) mat[i] = (*mat + c * i);
//reading from matrix file while (!feof (file)) { for(i=0;i //reading from vector file file = fopen (filenameVector, "r"); if(file==NULL) printf("Error in opening Vector file."); fscanf (file, "%d ", &n); // get the size of vector // dynamically allocating memory to vector vect = (int*) malloc(sizeof(int) * n); //reading from vector file while (!feof (file)) { for(i=0;i //perform multiplication now if(c!=n) { printf("Matrix Multiplication is not Possible!"); exit(1); } // dynamically allocating memory to our result i.e. product of matrix and vector result = (int*) malloc(sizeof(int) * r); for(i=0;i // writing the result array to output.csv file FILE *f = fopen("output.csv", "wb"); fwrite(result, sizeof(int), sizeof(result), f); fclose(f); printf("Output Multiplied Vector written successfully to output.csv file. "); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started