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

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

  • Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program...

    Use c++ as programming language. The file needs to be created ourselves (ARRAYS) Write a program that contains the following functions: 1. A function to read integer values into a one-dimensional array of size N. 2. A function to sort a one-dimensional array of size N of integers in descending order. 3. A function to find and output the average of the values in a one dimensional array of size N of integers. 4. A function to output a one-dimensional...

  • It is required to write a Matrix Calculator Program using C Programming Language that could only...

    It is required to write a Matrix Calculator Program using C Programming Language that could only perform addition, subtraction, and multiplication of two matrices of appropriate dimensions. Your program should prompt the user to select a matrix operation from a list of options that contains the aforementioned operations. Then, the user should be prompted to enter the dimensions of the two operand matrices. Next, your program should prompt the user to enter the elements of each matrix row-wise. Your program...

  • Write a C++ program that lets a maker of chips and salsa keep track of their...

    Write a C++ program that lets a maker of chips and salsa keep track of their sales for five different types of salsa they produce: mild, medium, sweet, hot, and zesty. It should use two parallel five-element arrays: an array of strings that holds the five salsa names and an array of integers that holds the number of jars sold during the past month for each salsa type. The salsa names should be stored using an initialization list at the...

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