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_a
ay(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 a
ay called spec as its input parameter. It stores
data entered by user including the size of the a
ay, the min and max of desired
andom 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_a
ay will base on its input parameter s to set up a two-dimensional square
a
ay d of size s with all elements of random numbers between low and high.
Then, it displays the a
ay 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 a
ay
printed and ready for further process.
ow_sum will use its input a
ay 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 a
ay 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
elow.
3. row_sum is modified so it becomes a recursive function. There are several ways to turn it
into
ecursion but, for this assignment purpose, you only modify it in three parts described
elow 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
ow. Since outer loop is not allowed, that's why it calls itself to calculate the sum of next
ow 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
ecursion. That is, when its value equals to the total number of rows of the a
ay, 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 a
ay 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 a
ay should be determined by
user at run-time but a variable length a
ay can't be a global variable in C, you will
declare this a
ay as a global variable of 10 x 10 and assume user never enters a size
igger than 10. In the case that user wants a smaller a
ay, this 10x10 a
ay 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 a
ay 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 a
ay: ");
scanf("%d", &n);
printf("\nEnter min and max of random numbers to fill in the a
ay.\n");
printf("For exampe, enter XXXXXXXXXXif 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 a
ay
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++)
a[i][j] = rand() % (max - min XXXXXXXXXXmin;
}
print the a
ay
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;
}