Write a program that ask user to enter two integers X and Y, and two doubles min
ID: 3880283 • Letter: W
Question
Write a program that ask user to enter two integers X and Y, and two doubles minZ maxZ. It then allocates a dynamic 2D array consisting of X rows and Y columns of double values, and sets each value in the 2D array by randomly generating a number between minZ and maxZ. If (minZ == maxZ), then simply set each value in the 2D array to minZ. Then print the overall sum, the sum of each row, and the sum of each column.
Here is a sample output: When input is 5 3 2.0 2.0 for X Y minZ maxZ Standard output starts with Overall sum = 30.0
Sum of each row
Row0 = 6.0
Row1 = 6.0
Row2 = 6.0
Row3 = 6.0
Row4 = 6.0
Sum of each column
10.0 10.0 10.0
#include #include
double RandomReal(double low, double high){
double d;
d = (double) rand() / ((double) RAND_MAX + 1);
return (low + d * (high - low));
}
int main(int argc, char* argv[]) {
int X, Y;
double minZ, maxZ; double **Arr2D; printf("Enter X Y minZ maxZ:");
scanf("%d %d %lf %lf", &X, &Y, &minZ, &maxZ);
/* dynamically create 2D array of doubles (X row, Y col),
* For each i, j * if (minZ == maxZ) Arr2D[i][j] = minZ;
* else Arr2D[i][j] = RandomReal(minZ, maxZ);
* Then find the sums we want using the given Arr2D !
*/
/* YOUR CODE */ return 0; } /* Don’t forget to include comments about the problem, yourself and each major step in your program! */
Explanation / Answer
#include <stdio.h>
#include <stdlib.h>
double RandomReal(double low, double high){
double d;
// srand(time(0));
if (low == high) return low;
d = (double) rand() / ((double) RAND_MAX + 1);
return (low + d * (high - low));
}
int main(int argc, char* argv[]) {
int X, Y;
double minZ, maxZ;
double **Arr2D;
int i, j;
printf("Enter X Y minZ maxZ:");
scanf("%d %d %lf %lf", &X, &Y, &minZ, &maxZ);
//dynamic allocation
Arr2D = (double **)malloc(X * sizeof(double *)); // rows
for (i=0; i<X; i++)
Arr2D[i] = (double *)malloc(Y * sizeof(double)); // columns
// fill in data
for (i = 0; i < X; i++)
for (j = 0; j < Y; j++)
Arr2D[i][j] = RandomReal( minZ, maxZ);
// display content
for (i = 0; i < X; i++){
for (j = 0; j < Y; j++){
printf("%lf ", Arr2D[i][j]);
}
printf(" ");
}
double sum = 0.0, column_sum = 0.0, row_sum = 0.0;
for (i = 0; i < X; i++)
for (j = 0; j < Y; j++)
sum += Arr2D[i][j];
printf("overall sum is %lf ", sum);
//sum of each row
printf("Sum of each row");
for (i = 0; i < X; i++){
row_sum = 0.0;
for (j = 0; j < Y; j++){
row_sum += Arr2D[i][j];
}
printf("row %d =%lf ",i, row_sum);
}
//sum of each column
printf("Sum of each column ");
for (i = 0; i < Y; i++){
column_sum = 0.0;
for (j = 0; j < X; j++){
column_sum += Arr2D[j][i];
}
printf("column %d = %lf ",i, column_sum);
}
/* dynamically create 2D array of doubles (X row, Y col),
* For each i, j * if (minZ == maxZ) Arr2D[i][j] = minZ;
* else Arr2D[i][j] = RandomReal(minZ, maxZ);
* Then find the sums we want using the given Arr2D !
*/
/* YOUR CODE */
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.