Deliverables: TwoD.h - includes the TwoD declaration and prototypes for the vari
ID: 3653607 • Letter: D
Question
Deliverables: TwoD.h - includes the TwoD declaration and prototypes for the various functions TwoD.c - includes the definitions for the function prototypes in TwoD.h driver.c - a driver to fully test the functionality of all the functions defined in TwoD.c Where to start? The TwoD.h file must contain the following: //make the columns and rows have a size of 50 to ensure enough memory #define MAX_SIZE 50 typedef struct { int rows, cols; int element[MAX_SIZE][MAX_SIZE]; }TwoD; //function prototypes void columnSum( TwoD, int [ ], int ); //computes the sum of values in each column of the matrix, storing each value in the integer array void rowSum( TwoD, int [ ], int ); //computes the sum of values in each row of the matrix, storing each value in the integer array TwoD twoDAdd( TwoD, TwoD ); //returns the sum of two matrices TwoD twoDSubtract( TwoD, TwoD ); //returns the difference of two matricesExplanation / Answer
use command gcc TwoD.c driver.c to compiler
// TwoD.h
#define MAX_SIZE 50
typedef struct { int rows, cols; int element[MAX_SIZE][MAX_SIZE]; }TwoD; //function prototypes
void columnSum( TwoD, int [ ], int ); //computes the sum of values in each column of the matrix, storing each value in the integer array
void rowSum( TwoD, int [ ], int ); //computes the sum of values in each row of the matrix, storing each value in the integer array
TwoD twoDAdd( TwoD, TwoD ); //returns the sum of two matrices
TwoD twoDSubtract( TwoD, TwoD ); //returns the difference of two matrices
//TwoD.c
#include<stdio.h>
#include "TwoD.h"
void columnSum( TwoD td, int a[], int cols) {
int j,i;
for ( j = 0 ; j < td.cols ; j++ )
a[j] = 0;
for ( i = 0; i < td.cols; i++)
for ( j = 0 ; j < td.rows ;j++)
a[i] = a[i] + td.element[j][i];
}
void rowSum( TwoD td, int a[], int rows) {
int j,i;
for ( j = 0 ; j < td.rows ; j++ )
a[j] = 0;
for ( i = 0; i < td.rows; i++)
for ( j = 0 ; j < td.cols ;j++)
a[i] = a[i] + td.element[i][j];
}
TwoD twoDAdd( TwoD a, TwoD b){
int i,j;
TwoD res ;
res.rows = a.rows;
res.cols = a.cols;
for ( i = 0; i < a.rows; i++)
for ( j = 0 ; j < a.cols ;j++)
res.element[i][j] = a.element[i][j] + b.element[i][j];
return res;
}
TwoD twoDSubtract( TwoD a, TwoD b){
int i,j;
TwoD res ;
res.rows = a.rows;
res.cols = a.cols;
for ( i = 0; i < a.rows; i++)
for ( j = 0 ; j < a.cols ;j++)
res.element[i][j] = a.element[i][j] - b.element[i][j];
return res;
}
// driver.c
#include<stdio.h>
#include "TwoD.h"
void print2darray(TwoD a);
void print1dArray(int a[],int size);
int main(){
int rows,cols;
printf("enter row size and col size ");
scanf("%d%d",&rows,&cols);
printf("please enter the element of first array , (not more than %d * %d ",rows,cols);
TwoD first,second;
int i,j;
first.rows = rows;
first.cols = cols;
for ( i = 0 ; i < rows;i++)
for ( j = 0; j <cols;j++)
scanf("%d",&(first.element[i][j]));
print2darray(first);
printf("please enter the element of second array , (not more than %d * %d ",rows,cols);
second.rows = rows;
second.cols = cols;
for ( i = 0 ; i < rows;i++)
for ( j = 0; j <cols;j++)
scanf("%d",&(second.element[i][j]));
print2darray(second);
int sumcols[cols];
int sumrows[rows];
columnSum(first,sumcols,cols);
printf("column ");
print1dArray(sumcols,cols);
rowSum(first,sumrows,rows);
printf("ros sum ");
print1dArray(sumrows,rows);
TwoD res = twoDAdd(first,second);
printf("result sum ");
print2darray(res);
TwoD res2 = twoDSubtract(first,second);
printf("result sub ");
print2darray(res2);
}
void print2darray(TwoD a){
int i,j;
for ( i = 0 ; i < a.rows;i++){
for ( j = 0 ; j < a.cols;j++)
printf("%d ",a.element[i][j]);
printf(" ");
}
}
void print1dArray(int a[],int size){
int il;
for (il = 0 ; il < size;il++)
printf("%d ",a[il]);
printf(" ");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.