Question: 2. Modify the program as follows: 1) Make the global variable sum local to main() and make the program do exactly the same thing as
2. Modify the program as follows:
1) Make the global variable sum local to main() and make the program do exactly the same thing as Lab2A.C. Save the program as Lab2B.C
Hint: Return the square of the number from the function square() and do the summing inside main() or initialize sum in main and pass it to square().
2) Make the global variable sum local to the function square(). Keep the for loop in main but do the summing inside the square() function. Save the program as Lab2C.C and submit it. Hint: Review what is a static variable. With a static variable, modify the function square() so that sum can be made local to function square().
*Lab2A.c:
#include
int sum; /* This is a global variable */
void header(void); /* function prototype/declaration */ void square(int number);
void main(void) { int index; header(); /* This calls the function named header */ for (index = 1; index <= 7; index++) square(index); /* This calls the square function */ getch(); }
void header(void) { /* This is the function named header */ printf("This is the header for the square program "); }
void square(int number) { /* This is the square function */ int numsq; numsq = number * number; /* This produces the square */ sum = sum + numsq; printf(" The square of %d is %d ", number, numsq); printf("The sum of the squares is %d ", sum); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
