Provide functions which do the following: A) initialize a four by four array of
ID: 3670271 • Letter: P
Question
Provide functions which do the following:
A) initialize a four by four array of integers to zero
B) randomly assign values to a two by three array
C) randomly assign values to a four by four array
D) print out a four by four array of integers
E) sum the elements of a four by four array of integers
F) sum the elements of a two by three array of integers
G) sum the values of each row of a two by three array of integers and return a one dimensional array with these values
H) sum the values of each row of a four by four array of integers and return a one dimensional array with these values
I) Test each of these functions appropriately.
Code on C:
#include
#include
void print2DArray(int array[2][3]);
void intToZero2D(int array[2][3]);
int main(int argc, char *argv[]) {
int array1[2][3]={{1,2,3},
{4,5,6}};
int array2[4][4];
print2DArray(array1);
print2DArray(array2);//array size does not match
intToZero2D(array1);//sets array to zero
intToZero2D(array2);
print2DArray(array1);//print to show the elements went to zero
print2DArray(array2);
return 0;
}
void print2DArray(int myArray[2][3]) {
int row;
int column;
for(row =0; row < 2; row++){
for(column = 0; column < 3; column++){
printf("%d ", myArray[row][column]); //printf(" [%d][%d] = %d ", row, column, myArray[row][column]);
}
printf(" ");
}
}
void intToZero2D(int yourArray[2][3]){
int row;
int column;
for(row=0;row<2;row++){
for(column=0;column<3;column++){
yourArray[row][column] = 0;
}
}
}
Explanation / Answer
#include<stdio.h>
#include <time.h>
#include <stdlib.h>
void inZero2D44(int a[4][4]){
int i,j;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
a[i][j]=0;
}
void inrand2D44(int a[4][4]){
int i,j;
srand(time(NULL));
//int r = rand();
for(i=0;i<4;i++)
for(j=0;j<4;j++)
a[i][j]=rand()%1000;
}
void inrand2D23(int a[2][3]){
int i,j;
srand(time(NULL));
for(i=0;i<2;i++)
for(j=0;j<3;j++)
a[i][j]=rand()%1000;
}
void print2D23(int a[2][3]){
int i,j;
printf(" 2X3 : ");
for(i=0;i<2;i++){
for(j=0;j<3;j++)
printf("%d ",a[i][j]);
printf(" ");
}
}
void print2D44(int a[4][4]){
int i,j;
printf(" 4X4 : ");
for(i=0;i<4;i++){
for(j=0;j<4;j++)
printf("%d ",a[i][j]);
printf(" ");
}
}
void sum2D44(int a[4][4]){
int i,j,sum=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
sum+=a[i][j];
printf(" 4X4 sum = %d ",sum);
}
void sum2D23(int a[2][3]){
int i,j,sum=0;
// printf(" 1D row sum of 4X4 ");
for(i=0;i<2;i++)
for(j=0;j<3;j++)
sum+=a[i][j];
printf(" 2X3 sum = %d ",sum);
}
void sumR2D23(int a[2][3],int b[2]){
int i,j,sum=0;
for(i=0;i<2;i++){
sum = 0;
for(j=0;j<3;j++)
sum+=a[i][j];
b[i] = sum;
}
// printf(" sum %d = ",sum);
}
void sumR2D44(int a[4][4],int b[4]){
int i,j,sum=0;
for(i=0;i<4;i++){
sum = 0;
for(j=0;j<4;j++)
sum+=a[i][j];
b[i] = sum;
}
}
void printd(int d[2]){
int i;
printf(" 1D row sum of 2X3 ");
for(i=0;i<2;i++)
printf("%d ",d[i]);
}
void printc(int c[4]){
int i;
printf(" 1D row sum of 4X4 ");
for(i=0;i<4;i++)
printf("%d ",c[i]);
}
void main(){
int i,j;
int a[4][4],b[2][3],c[4],d[2];
inZero2D44(a);
print2D44(a);
inrand2D23(b);
print2D23(b);
inrand2D44(a);
print2D44(a);
sum2D44(a);
sum2D23(b);
sumR2D23(b,d);
printd(d);
sumR2D44(a,c);
printc(c);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.