Docsity

C Programming: Final Answers for Max, Simple Sum, and Mystery Functions, Exams of Computer Science

Toggle sidebar
bg1
S
AMPLE
F
INAL
A
NSWERS
ECS 30-A โ€” S
PRING
2002
Version of June 3, 2002 8:20 am Page 1 of 3
Sample Final Answers
1. Here is a macro for computing the maximum of two munbers:
#define max(x, y)((x) > (y) ? (x) : (y))
and here is a function:
int max(int a, int b)
{
return(a > b ? a : b);
}
In the following code fragment, give the values of a, b, c, d, and e at the end if max is the above macro, and then
if max is a function call:
a = -1;
b = 0;
c = 1;
d = max(++a, b);
e = max(c++, b);
Answer:
2. Here is a very simple program which is designed to sum its arguments:
#include <stdio.h>
int main(int argc, char *argv[])
{
int sum = 0;
int i;
for(i = 1; i < argc; i++)
sum += argv[i];
variable value if max is macro value if max is function
a
b
c
d
e
variable value if max is macro value if max is function
a0 0
b0 0
c3 2
d0 0
e2 1
pf3

Partial preview of the text

Download C Programming: Final Answers for Max, Simple Sum, and Mystery Functions and more Exams Computer Science in PDF only on Docsity!

Sample Final Answers

  1. Here is a macro for computing the maximum of two munbers: #define max(x, y)((x) > (y)? (x) : (y)) and here is a function: int max(int a, int b) { return(a > b? a : b); } In the following code fragment, give the values of a, b, c, d, and e at the end if max is the above macro, and then if max is a function call: a = -1; b = 0; c = 1; d = max(++a, b); e = max(c++, b);

Answer:

  1. Here is a very simple program which is designed to sum its arguments: #include <stdio.h>

int main(int argc, char *argv[]) { int sum = 0; int i;

for(i = 1; i < argc; i++) sum += argv[i];

variable value if max is macro value if max is function

a

b

c

d

e

variable value if max is macro value if max is function

a 0 0

b 0 0

c 3 2

d 0 0

e 2 1

printf(โ€œ%d\nโ€, sum);

return(0); } a. Suppose this program is invoked as sum 1 2 3 What are the values of argc and the elements of argv? Draw a picture if that would help you indicate what the values are. Please note I want the value of argc, argv, and each element of argv, and so forth, either as a number or character, or as arrows in a picture (for addresses). Answer: argc is 4, and the following summarizes argv:

b. When I ran this programs, I got 2147431340. Why does the program print the incorrect value, and how would you fix it? (If you cannot write the code, for partial credit simply say what you would do.) Answer: The program adds in the addresses of the strings, not the numbers those strings represent. The eas- iest way to fix the program is to replace the line sum += argv[i] with the line sum += atol(argv[i])

a. What does the following function do? Remember, its purpose can be stated in a succinct sentence; for par- tial credit, you can describe what each line does, but for full credit, you must state its function in one very short sentence. int mystery(char a[]) { register int i;

for(i = 0; a[i]; i++) if (!isdigit(a[i])) return(0); return(1); } Answer: It returns 1 if the array a[] contains all digits; that is, it is an unsigned number. Otherwise, it returns

b. Rewrite the function using pointers rather than an array. Answer: int mystery(char *a) {

argv[0]

argv[1]

argv[2]

argv[3]

argv s^ u^ m^ \

1 \

2 \

3 \