Microsoft Word - Program 1.docx Program 1. 1. Add the following four function prototypes in proj4_1.c whether they are the same or different from the ones shown below, be sure they are changed to meet...

1 answer below »
c program modification


Microsoft Word - Program 1.docx Program 1. 1. Add the following four function prototypes in proj4_1.c whether they are the same or different from the ones shown below, be sure they are changed to meet the following. void ask_user(int spec[3]); void make_array(int s, int d[s][s], int low, int high); void row_sum(int s, int d[s][s]); void column_sum(int s, int d[s][s]); 2. Implement (i.e., define) the above four functions such that none of them is recursive as described below: • ask_user will interact with user by displaying messages and read data entered by user. It accepts a 3-element int array called spec as its input parameter. It stores data entered by user including the size of the array, the min and max of desired random numbers in spec[0], spec[1], and spec[2], respectively, so the caller, i.e., main, can use these data to complete its job. • make_array will base on its input parameter s to set up a two-dimensional square array d of size s with all elements of random numbers between low and high. Then, it displays the array on screen as shown in the screenshot given for proj3_1.c. When the caller, i.e., main, calls this function, it will have the array printed and ready for further process. row_sum will use its input array d whose size is specified by another parameter s and calculate the sum of each row of d. Like proj3_1.c, all row sums are printed in one line of the form "Row totals: n1 n2 n3 n4 ......" • column_sum will do similar job as row_sum does. It uses its input array d whose size is specified by another parameter s and calculate the sum of each column of d. All column sums are printed in one line of the form "Column totals: n1 n2 n3 n4 ......" 4. Modify the main function so it no longer performs the details of all tasks as in proj3_1.c, instead, it does all by declaring necessary variables, initializing random number generator, and calling the above functions. 5. From user's perspective, he/she still interacts with the program exactly the same as proj3_1.c does. There should be no difference in the output display. Program 2. …… Making Recursive Function Calls Based on the file proj4_1.c completed above, save an extra copy of this file as proj4_2.c as a new version of the same program and modify it to satisfy the new requirements listed below. 1. proj4_2.c runs the same main function as that in proj4_1.c with no code change. 2. This program also keeps all four functions defined in proj4_1.c but with two modified below. 3. row_sum is modified so it becomes a recursive function. There are several ways to turn it into recursion but, for this assignment purpose, you only modify it in three parts described below so each time it is called, it calculates and prints the sum of only one row. It adds a 3rd input int parameter called which_row. It tells which row's sum the function needs to calculate. For example, the main will first call this function with a value of 0 for this parameter. No nesting (or double-layered) loops is used. If nesting or double loops is how you implement this function in proj4_1.c, you will eliminate one in this version. (Hint: The only loop used here is the one to accumulate the sum from each column element of the row. Since outer loop is not allowed, that's why it calls itself to calculate the sum of next row with which_row incremented by one.) Because the row header "Row totals: " should be printed only once before printing the sum of the top row, this recursive function needs to detect if it is calculating the sum of the top row. If no, it would skip printing the row header. **This function will use the parameter which_row to determine if it's the end of recursion. That is, when its value equals to the total number of rows of the array, it stops calling itself and it returns to its caller. 4. column_sum is modified so it also becomes a recursive function. Please follow item 3 above for details of how it is modified to complete column sum calculation. 5. From user's perspective, he/she still interacts with the program exactly the same as the previous version. There should be no difference in the output display. Program 3 ….. Using External Variables Based on the file proj4_2.c completed above, save an extra copy of this file as proj4_3.c as a new version of the same program and modify it to satisfy the new requirements listed below. 1. proj4_3.c keeps all four functions defined in proj4_2.c but, except ask_user, the reset three are modified as described below. 2. proj4_3.c let the square array and its size be file scope, that is, they are shareable to all functions in the file by moving them out of main function and declare them as external (i.e., global) variables. However, because the size of this array should be determined by user at run-time but a variable length array can't be a global variable in C, you will declare this array as a global variable of 10 x 10 and assume user never enters a size bigger than 10. In the case that user wants a smaller array, this 10x10 array becomes only partially used, which is acceptable for the purpose of this assignment. dynamic memory allocation techniques to manage this issue. 3. Modify all functions except ask_user but including main such that they all use the two external variables. In other words, the array and its size are no longer passed between main and other functions. 4. Both row_sum and column_sum remain recursive as in proj4_2.c. 5. From user's perspective, he/she still interacts with the program exactly the same as previous two versions except he/she never enters a size bigger than 10. There should be no difference in the output display. proj4_1.c #include #include #include int main(void) { int n, min, max; printf("\nEnter the size of a 2D square array: "); scanf("%d", &n); printf("\nEnter min and max of random numbers to fill in the array.\n"); printf("For exampe, enter -12...8 if min is -12 and max is 8, they must be separated by '...': "); scanf("%d...%d", &min, &max); int a[n][n], i, j, sum; srand((unsigned int)time(NULL)); // set up the array for (i = 0; i < n;="" i++)="" {="" for="" (j="0;" j="">< n;="" j++)="" a[i][j]="rand()" %="" (max="" -="" min="" +="" 1)="" +="" min;="" }="" print="" the="" array="" printf("\n");="" for="" (i="0;" i="">< n;="" i++)="" {="" for="" (j="0;" j="">< n;="" j++)="" printf("%5d="" ",="" a[i][j]);="" printf("\n");="" }="" row="" sums:="" printf("\nrow="" totals:");="" for="" (i="0;" i="">< n;="" i++)="" {="" sum="0;" for="" (j="0;" j="">< n;="" j++)="" sum="" +="a[i][j];" printf("="" %d",="" sum);="" }="" column="" sums:="" printf("\ncolumn="" totals:");="" for="" (j="0;" j="">< n;="" j++)="" {="" sum="0;" for="" (i="0;" i="">< n; i++) sum += a[i][j]; printf(" %d", sum); } printf("\n"); return 0; } n;="" i++)="" sum="" +="a[i][j];" printf("="" %d",="" sum);="" }="" printf("\n");="" return="" 0;="">
Answered Same DayJun 10, 2021

Answer To: Microsoft Word - Program 1.docx Program 1. 1. Add the following four function prototypes in...

Aditya answered on Jun 11 2021
136 Votes
#include
#include
#include
void ask_user(int spec[3])
{
print
f("\nEnter the size of a 2D square array: ");
scanf("%d", &spec[0]);
printf("\nEnter min and max of random numbers to fill in the array.\n");
printf("For exampe, enter -12...8 if min is -12 and max is 8, they must be separated by '...': ");
scanf("%d...%d", &spec[1], &spec[2]);
}
void row_sum(int s, int d[s][s])
{
int sum,i,j;
printf("\nRow...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here