I am writing a code for CS and I need help. #include <stdio.h> #include <stdlib.
ID: 3801581 • Letter: I
Question
I am writing a code for CS and I need help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
int **allocateMatrix(int, int);
void initMatrix(int **, int, int);
int findRange(int **, int, int);
double findAverage(int **, int, int);
void printCorners(int **, int, int);
int main(int argc, char *argv[]) {
srand(0);
int row = atoi(argv[1]);
int col = atoi(argv[2]);
data = allocateMatrix(row, col),
initMatrix(data, row, col);
printf("Range of values in the array is %d ", findRange(data, row, col) );
printf("Average of all array values i %lf ", findAverage(data, row, col) );
printCorners(data, row, col);
return 0;
}
CS 100 Lab Seven Spring 2017 Create a directory called lab7 on your machine using mkdir lab7 Within this directory, complete the program named lab7.c that is shown below. You need to write the five functions shown in red #includeExplanation / Answer
Here iam giving ony the funtion code. Rest you can code as ur template.
//Allocation of matrix
int **allocateMatrix(int r, int c)
{
int k;
int **data = (int**) malloc(sizeof(int) * r);
for (k = 0; k < c; k++) {
data[k] = (int*) malloc(sizeof(int) * c);
return data;
}
// to initialize the matrix
void initMatrix(int **data, int r, int c)
{
int i,j;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
data[i][j] = ((int)rand()/(int)RAND_MAX);
}
}
}
//to fine the range of the matrix
int findRange(int **data, int r, int c)
{
int maximum =data[0][0];
int minimum= data[0][0];
for( int x = 0 ; x < r ; x++ )
{
for( int d = 0 ; d < c ; d++ )
{
if (data[x][d] > maximum )
maximum = data[x][d];
}
}
for( int x = 0 ; x < r ; x++ )
{
for( int d = 0 ; d < c ; d++ )
{
if (data[x][d] > minimum )
minimum = data[x][d];
}
}
int range = maximum-minimum;
return range;
}
// to fine average of the matrix
double findAverage(int ** data, int r, int c)
{
int sum=0, n=0;
for( int y= 0 ; y < r ; y++ )
{
for( int z = 0 ; z < c ; z++ )
{
sum=sum + data[y][z];
n++;
}
}
double average= sum / n;
return average;
}
// to print the corners of the matrix
void printCorners(int **data, int r, int c)
{
printf (“%d,%d”, data[0][0], data[0][c-1]);
printf(“/n%d,%d”, data[r-1][0], data[r-1][c-1]);
}
//Allocation of matrix
int **allocateMatrix(int r, int c)
{
int k;
int **data = (int**) malloc(sizeof(int) * r);
for (k = 0; k < c; k++) {
data[k] = (int*) malloc(sizeof(int) * c);
return data;
}
// to initialize the matrix
void initMatrix(int **data, int r, int c)
{
int i,j;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
data[i][j] = ((int)rand()/(int)RAND_MAX);
}
}
}
//to fine the range of the matrix
int findRange(int **data, int r, int c)
{
int maximum =data[0][0];
int minimum= data[0][0];
for( int x = 0 ; x < r ; x++ )
{
for( int d = 0 ; d < c ; d++ )
{
if (data[x][d] > maximum )
maximum = data[x][d];
}
}
for( int x = 0 ; x < r ; x++ )
{
for( int d = 0 ; d < c ; d++ )
{
if (data[x][d] > minimum )
minimum = data[x][d];
}
}
int range = maximum-minimum;
return range;
}
// to fine average of the matrix
double findAverage(int ** data, int r, int c)
{
int sum=0, n=0;
for( int y= 0 ; y < r ; y++ )
{
for( int z = 0 ; z < c ; z++ )
{
sum=sum + data[y][z];
n++;
}
}
double average= sum / n;
return average;
}
// to print the corners of the matrix
void printCorners(int **data, int r, int c)
{
printf (“%d,%d”, data[0][0], data[0][c-1]);
printf(“/n%d,%d”, data[r-1][0], data[r-1][c-1]);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.