Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

USE C PROGRAMING LANGUAGE. At the end of the page I have posted the codes to work with (mmultSeqOptions.c and writeRandom2DArray.c) mmultSeqOptions.c /* Program to

USE C PROGRAMING LANGUAGE.

At the end of the page I have posted the codes to work with (mmultSeqOptions.c and writeRandom2DArray.c)

image text in transcribed

image text in transcribed

image text in transcribed

mmultSeqOptions.c

/* Program to generate two square 2D arrays of random doubles and

time their multiplication.

Compile by: gcc -O5 -o mmult mmultSeqOptions.c -lm

Run by: ./mmult 1000

*/

#include

#include

#include // use the time to seed the random # generator

#include // needed for fabs function

#define TRUE 1

#define FALSE 0

#define BOOL int

// function prototypes

double ** allocate2DArray(int rows, int columns);

void print2DArray(int rows, int columns, double ** array2D);

void generateRandom2DArray(int rows, int columns,

double min, double max, double ** random2DArray);

BOOL equal2DArrays(int rows, int columns, double ** array1, double ** array2,

double tolerance);

void matrixMultiplication(int rows1, int columns1, double ** array1,

int rows2, int columns2, double ** array2,

double ** product);

void matrixMultiplicationAlt(int rows1, int columns1, double ** array1,

int rows2, int columns2, double ** array2,

double ** product);

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

double ** A;

double ** B;

double ** C;

double ** C_alt;

int rows, columns;

long startTime, endTime,seqTime;

if (argc !=2) {

printf("Usage: %s ", argv[0]);

exit(-1);

} // end if

sscanf(argv[1], "%d", &rows);

columns = rows;

// seed the random number generator

srand( time(NULL) );

A = allocate2DArray(rows, columns);

B = allocate2DArray(rows, columns);

C = allocate2DArray(rows, columns);

C_alt = allocate2DArray(rows, columns);

generateRandom2DArray(rows, columns, -1.0, +1.0, A);

generateRandom2DArray(rows, columns, -1.0, +1.0, B);

printf("after initializing matrices ");

time(&startTime);

matrixMultiplication(rows, columns, A, rows, columns, B, C);

time(&endTime);

writeRandom2DArray.c

/* Program to write a random 2D array of doubles to a file.

Command-line arguments are used to specify the # of rows,

columns, and file name.

Compile by: gcc -o write2D writeRandom2DArray.c -lm

Run by: ./write2D 10 10 myFile.dat 5.0 9.0

*/

#include

#include

#include // use the time to seed the random # generator

#include // needed for fabs function

#define TRUE 1

#define FALSE 0

#define BOOL int

// function prototypes

double ** allocate2DArray(int rows, int columns);

void print2DArray(int rows, int columns, double ** array2D);

void generateRandom2DArray(int rows, int columns,

double min, double max, double ** random2DArray);

void write2DArray(int rows, int columns, double ** random2DArray,

FILE * outputFilePtr);

void read2DArray(int * rows, int * columns, double *** array2DRead,

FILE * inputFilePtr);

BOOL equal2DArrays(int rows, int columns, double ** array1, double ** array2,

double tolerance);

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

FILE * outputFilePtr;

FILE * inputFilePtr;

double ** random2DArray; // dynamically allocated array

double ** array2DRead;

double minRandom, maxRandom;

int rows, columns;

int rowsRead, columnsRead;

if (argc !=6) {

printf("Usage: %s "

, argv[0]);

exit(-1);

} // end if

if ((outputFilePtr = fopen(argv[3], "wb")) == NULL) {

printf("%s cannot be opened for writing ", argv[3]);

exit(-1);

} // end if

sscanf(argv[1], "%d", &rows);

sscanf(argv[2], "%d", &columns);

sscanf(argv[4], "%lf", &minRandom);

sscanf(argv[5], "%lf", &maxRandom);

random2DArray = allocate2DArray(rows, columns);

generateRandom2DArray(rows, columns, minRandom, maxRandom, random2DArray);

// if small enough, print to screen

if (rows

printf("Randomly generated 2D array of doubles: ");

print2DArray(rows, columns, random2DArray);

} // end if

write2DArray(rows, columns, random2DArray, outputFilePtr);

fclose(outputFilePtr); // close the file --> flush to disk

/* COMMENTED OUT CODE TO USE IN PART B OF THE LAB

if ((inputFilePtr = fopen(argv[3], "rb")) == NULL) {

printf("%s cannot be opened for reading ", argv[3]);

exit(-1);

} // end if

read2DArray(&rowsRead, &columnsRead, &array2DRead, inputFilePtr);

printf(" 2D array of doubles read from file: ");

print2DArray(rowsRead, columnsRead, array2DRead);

fclose(inputFilePtr);

if (equal2DArrays(rows, columns, random2DArray, array2DRead, 0.0)) {

printf("Arrays match with tolerance of %.10f ", 0.0);

} else {

printf("Arrays DON'T match with tolerance of %.10f ", 0.0);

} // end if

*/

return 0;

} // end main

/*******************************************************************

* Function allocate2DArray dynamically allocates a 2D array of

* size rows x columns, and returns it.

********************************************************************/

double ** allocate2DArray(int rows, int columns) {

double ** local2DArray;

int r;

local2DArray = (double **) malloc(sizeof(double *)*rows);

for (r=0; r

local2DArray[r] = (double *) malloc(sizeof(double)*columns);

} // end for

return local2DArray;

} // end allocate2DArray

/*******************************************************************

* Function write2DArray is passed the # rows, the # columns,

* 2D array, and the binary file pointer. It writes the #row, #columns,

* and the 2D array in row-order.

********************************************************************/

void write2DArray(int rows, int columns, double ** random2DArray,

FILE * outputFilePtr) {

int r;

fwrite(&rows, sizeof(int), 1, outputFilePtr);

fwrite(&columns, sizeof(int), 1, outputFilePtr);

for (r=0; r

// writes a whole row of the array

fwrite(random2DArray[r], sizeof(double), columns, outputFilePtr);

} // end for

} // end write2DArray

/*******************************************************************

* Function read2DArray is passed a binary file pointer containing a

* # rows, # columns, and the 2D array of doubles in row-order.

* It returns the # rows, the # columns, and the 2D array.

********************************************************************/

void read2DArray(int * rows, int * columns, double *** array2DRead,

FILE * inputFilePtr) {

// ADD CODE HERE FOR PART B

} // end read2DArray

/*******************************************************************

* Function generateRandom2DArray is passed the # rows, the # columns,

* min. value, max. value, and returns random2DArray containing

* randomly generated doubles.

********************************************************************/

void generateRandom2DArray(int rows, int columns,

double min, double max, double ** random2DArray) {

int r, c;

double range, div;

// seed the random number generator

srand( time(NULL) );

for (r = 0; r

for (c = 0; c

range = max - min;

div = RAND_MAX / range;

random2DArray[r][c] = min + (rand() / div);

} // end for (c...

} // end for (r...

} // end generateRandom2DArray

/*******************************************************************

* Function print2DArray is passed the # rows, # columns, and the

* array2D. It prints the 2D array to the screen.

********************************************************************/

void print2DArray(int rows, int columns, double ** array2D) {

int r, c;

for(r = 0; r

for (c = 0; c

printf("%8.5lf", array2D[r][c]);

} // end for (c...

printf(" ");

} // end for(r...

} // end print2DArray

/*******************************************************************

* Function equal2DArrays is passed the # rows, # columns, two

* array2Ds, and tolerance. It returns TRUE if corresponding array

* elements are equal within the specified tolerance; otherwise it

* returns FALSE.

********************************************************************/

BOOL equal2DArrays(int rows, int columns, double ** array1, double ** array2,

double tolerance) {

// ADD CODE HERE FOR PART B --> HINT: USE THE fabs FUNCTION TO WHEN

// COMPARING CORRESPONDING ELEMENTS

} // end equal2DArray

Part A: Using an editor on student.cs.uni.edu open the file writeRandom2DArray.cwhich contains a simple C program that allows the user to enter two integers (# rows and # columns), a binary file name, and two reals (min. random # and max. random # range) on the command-line. The program writes to the specified file name binary data consisting of integer number of rows integer number of columns the randomly generated 2D array of doubles in row-major order * Answer the following questions about the writeRandom2DArray.c program a) How does the creation (opening) of the binary file differ than the creation of the text file in lab4? Consider the The generateRandom2DArray function defined as void generateRandom2DArray (int rows, int columns, double min, double max, double ** random2DArray) int r, C double range, div: // seed the random number generator sand time (NULL) ): for (r = 0; r

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions