Question

In C Language: Write a program to sum arrays row wise and column wise. Keep track...

In C Language: Write a program to sum arrays row wise and column wise. Keep track of time to do each matrix each way. Export you times and matrix sizes to a csv file. Use excel to open you csv. Make a graph of your times versus matrix size.

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

Copy the code and run. Look at the screenshot for indentation. Comment incase of bugs/doubts.

CODE:

matiTime 2 3 #include <stdio.h» #1nclueestdlib.h> include <time.h> 5 int nain) t /set initial rowsice and colsize int co!size

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

int main() {

//set initial rowsize and colsize

int rowsize = 10;

int colsize = 10;

//open file to write to

FILE* fp;

fp = fopen("times.csv", "w");

while(rowsize <= 1000) { //keep increasing rowsize till max limit is reached. Depends on available RAM

int arr[rowsize][colsize]; //create array

for(int j = 0; j < rowsize; j++) { //randomly assign values using rand function

for(int k = 0; k < colsize; k++) {

srand(time(0));

arr[j][j] = rand();

}

}

clock_t start, end; //start and end store clock times

double time_row;

start = clock(); //start clock

for(int j = 0; j < rowsize; j ++) { //iterate over all rows

long long int sum = 0; //stores sum

for(int k = 0; k < colsize; k++) { //iterate over all columns

sum += arr[j][k]; //keep adding

}

}

end = clock(); //stop clock

time_row = ((double)(end - start)) / CLOCKS_PER_SEC; //get time taken by adding row operation

double time_col; //repeat the same. This time for column wise sum

start = clock();

for(int j = 0; j < colsize; j ++) {

long long int sum = 0;

for(int k = 0; k < rowsize; k++) {

sum += arr[k][j];

}

}

end = clock();

time_col = ((double)(end - start)) / CLOCKS_PER_SEC;

fprintf(fp, "%lf,%lf,%d,%d\n", time_row, time_col, rowsize, colsize); //print to file

rowsize *= 2; //increase row and colsize

colsize *= 2;

}

fclose(fp);

return 0;

}

SAMPLE RUN:

sample output file produced by program. row_time, col_time, num_rows, num_cols.

.900884, θ·880083 , 16, 1e 3 4 5 000D86 , 0 . 000005, 20 , 2 9·000026, θ . 800018, 46, 46 9.000844 , θ·8日0038,80,BA θ· 600879

Add a comment
Know the answer?
Add Answer to:
In C Language: Write a program to sum arrays row wise and column wise. Keep track...
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
  • Write a C Program that finds the row totals and column totals of a Matrix. The...

    Write a C Program that finds the row totals and column totals of a Matrix. The user is prompted to enter the numbers to fill in a two-dimensional array of any size (say 3x3). The program then finds the sum of row elements and prints them along-side rows, and finds the sum of columns and prints them along-side corresponding columns. You will need nested for loops please help!

  • Language C Code Write a program that takes two integer arrays (A and B) and sums...

    Language C Code Write a program that takes two integer arrays (A and B) and sums them together (element wise). A third array to accept the result should be passed in as the output argument. Assume the arrays are all the same size. The argument N is the size of the arrays. Your code should provide a function with the following signature: void array Addition (int A[], int B[], int N, int output[]) { } Your code must also provide...

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

  • Write a Java program that calculates the sum of a variable sized matrix using a two...

    Write a Java program that calculates the sum of a variable sized matrix using a two dimensional array. (ArraySum.java) The user should provide the number of rows and columns Test multiple user inputs (3 times 2, 4 times 4, 6 times 2, etc) A sum should be created for each row, each column, and the total for the matrix Ex.: How big would you like your matrix? Rows - ? 3 Columns -? 2 Please enter your row 1? Column...

  • JAVA Write a method that returns the sum of all the elements in a specified column...

    JAVA Write a method that returns the sum of all the elements in a specified column in a matrix using the following header: public static double sumColumn(double [][] m, int columnIndex) Write another method that returns the sum of all the elements in a specified row in a matrix using the following header: public static double sumRow( double [][] m, int rowIndex) Write a test program that reads a 3-by-4 matrix and displays the sum of each column and sum...

  • Row and columns sum Create a java program that: Input: Receive as input in command line...

    Row and columns sum Create a java program that: Input: Receive as input in command line the sizes m and n of a two dimensional array Receive as input in command line the minValue and maxValue between which the random numbers will be generated for your two dimensional array Generate: Generate a two-dimensional with numbers between minValue and maxValue (inclusive) with the given size (m x n) Compute: Compute the sum for each row and store the results in a...

  • Write a program to find product of a row vector, U having dimension 1 x 2...

    Write a program to find product of a row vector, U having dimension 1 x 2 with a 2 x 2 matrix, A. The input to the program includes initialized row vector, U and matrix, A. The expected output is a transformed row vector. You can test your program using following example: A = 2 4 5 1 4 5 1 and U = [3 2] The result of product U.A is a transformed row vector: [16 14] Q2. Write...

  • 3. Write a complete assembly language program to read two matrices (2-dim arrays) A and B...

    3. Write a complete assembly language program to read two matrices (2-dim arrays) A and B and display the resulting matrix C, which is the sum of A and B. Procedures must be used to organize the code.

  • Language: c++ Write a program that opens a file to read 20 floating point numbers and...

    Language: c++ Write a program that opens a file to read 20 floating point numbers and store the information in a 4 x 5 array. The program should do the following: a. Compute the average of each set of 5 values b. Determine the largest value of the 20 values c. Report the results; print the original matrix and average of each row, and the largest of the values in this function. d. At the end of the program prompt...

  • Language is C++ Basic principles and theory of structured programming in C++ by implementing the class:...

    Language is C++ Basic principles and theory of structured programming in C++ by implementing the class: Matrix Use these principles and theory to help build and manipulate instances of the class (objects), call member functions Matrix class implementation from the main function file and, in addition, the Matrix class must have its interface(Matrix.h) in a separate file from its implementation (Matrix.cpp). The code in the following files: matrix.h matrix.cpp matrixDriver.h Please use these file names exactly. Summary Create three files...

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