I get 6,4,3 for the first 3 questions respectively, but I don't know how to figure out the last one. However, the solution manual indicated 7,5,4,18 as the answers.
int sum(int x[], int N) {
int k = 0;
int s = 0;
while (k < N) {
s = s + x[k];
k = k + 1;
}
return s; // the activation record for sum will be ____________ locations
}
int fred(int a, int b) {
return a + b; // (2) the activation record for fred will be ____________ locations
}
void barney(int x) {
x = fred(x, x);//(2) the activation record for barney will be ____________ locations
}
void main(void) {
int a[4];
int x = sum(a, 4);
barney(x);
} // (3) the stack must have at least _____________ locations to run this program
I dont know what is the convention of your book, but I assume, there is always a place for return address, address of return value and intermediate results
a)return address, address of return result, x, N, k, s, intermediate result for s + x[k] = total 7
b)ret. addr, addr of ret result, a, b, int. res. a+b = total 5
c)ret. addr, addr of ret result, x, space for return result of fred = total 4
d)last one is not asking the activation record max required stack size at any given point. It calls sum, It calls barney and barney calls fred that is 7+5+4 = 16. And 16 + a + x = total 18 locations to run this program.
Note that, this calculations are based on my wild guess about your books convention.