Prerequisite Evaluation for Intermediate Programming:
The purpose of this exercise is to find out what you do NOT know, and, therefore, what I need to teach. PLEASE do not ask anyone for help on these questions. Submit the answers to me by Monday by 10 am in my mailbox at Buckman 233, so that I can prepare next week's lecture properly. Please don’t email it to me. Full homework credit will be given if you attempt all the questions, even if your answers are wrong. If you don't know what the question is asking, write a sentence to explain your confusion. (I need to see those wrong answers!) 1. Characters.
(a) What type should we declare ch to be? ch = 'E'; (b) What type should we declare b to be? b = isupper(ch); (c) What will be stored in d by this assignment: d = 'E' - 'A'; (d) What will be stored in ch by this assignment: ch = toupper('p'); (e) What will be stored in ch by this assignment: ch = toupper('#');
2. Character Input. Consider the following code segment:
char input[5]; int k; for (k = 0; k < 5; k++)
scanf("%c", &input[k]);
Assume the user input typed this input (five letters with spaces between them): a b c d e (a) Diagram the contents of the array after the loop is finished. (b) If the scanf() format is change to " %c ", what will the contents of the array be?
3. Pointers, objects and values. (a) Draw a diagram showing variable names, boxes and the contents of boxes created by the declarations below. For the array, also show the subscripts. Use an arrow to represent a pointer.
int b, c = 10; int arr[3] = {8, 19, -2}; int *p = arr; int *q = &arr[1];
(b) What is stored in arr[3]? (c) What is stored in b after this assignment?
b = *p * c + *q; (d) Diagram the contents of p and arr after this assignment:
*p = *q; (e) What is stored in p after this assignment?
p = q; 4. Arrays. Consider the memory diagram, below. Write the declarations and initializations for the three variables shown in the diagram.
Prerequisite Evaluation for Intermediate Programming:
5. Functions. (a) The function return statement can return exactly one value. Describe two ways that you can use parameters to return multiple values from a function. (b) What is a global variable? (c) Suppose you are given a program with a global variable. Explain how to change the program to eliminate the global.
6. What is wrong here? Happy Hacker likes to write code but he doesn't like to study the theory. He wrote the following chunks of code and was puzzled why they didn't work. Can you help Happy?
(a) This loop should generate a hundred thousand random numbers between 0 and 99 and count the 99's. But it doesn't stop, why?
short n, targets, k; for (targets = k = 0; k<100000; ++k ) { n = rand() % 100; if (n == 99) ++targets; }
(b) Happy's loop should print a table of values of sin(x), where x goes from 0 to 1 in steps of 0.01. But it doesn't stop, why?
double x = 0; while (x != 1) { printf( "%9.6f\n", sin(x) ); x += .01; }
(c) Happy used a float variable, star, to store the distance from here to the edge of our galaxy (74,000 light years away), in miles. One light year is 5.879e+12 miles. He used another float variable, mall, to store the distance from here to the nearest shopping mall, in miles. He can't understand why star + mall is the same value as star. Help him explain why.