Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
SOLUTIONS
1. Read the following statements and fill in the correct answers for the values of each variable AFTER this block of statements has been executed. { int a = 1; int b = 2; a = b--; } When complete a = 2, b = 1 NOTE b is copied into a BEFORE b is decremented by 1 Same as: a=b; b=b-1; 10 pts
2. Complete main() below, so that main() prints n, n-1, n-2 ... 1 using a for loop. The output should look like this with input = 5: Enter n: 5 5 4 3 2 1 //Complete the program: #include <stdio.h> void main(void) // print 1..n { int n; printf(\n Enter n: \n); scanf(%d, &n); for ( n=5; n>0; n--) // count down from n..1, and print n { printf("%d ", n); } } // note n is modified
20 pts Page 1
SOLUTIONS
3. Complete main() so that it count up and prints the numbers 1..n substituting T if the number is a multiple of 3, and F if it is a multiple of 5, and TF if it is a multiple of both 3 and 5. Like this: Enter n: 17 1 2 T 4 F T 7 8 T F 11 T 13 14 TF 16 17 #include <stdio.h> void main(void) { int n; int counter = 1; printf(\n Enter n: \n); scanf(%d, &n); while ( counter <= n ) // count up 1..n using while { if (counter%3 == 0) // alterantively: if (counter/3 == 0) printf("T"); // print T if (counter%5 == 0) // alterantively: if (counter/5 == 0) printf("F"); // print F if ((counter%3 != 0) && (counter%5 != 0)) //not 3 or 5 printf("%d", counter); // and print count printf(" "); counter++; } } // note n is unchanged 30 pts
4. Write a function with a prototype: float plustax(float subtotal, float tax); Calculates and returns the total price plus tax, given a pre-tax subtotal and the tax rate. If called with arguments subtotal = 10.00 and tax = 0.08, it would return 10.8 float plustax(float subtotal, float tax) { float taxamount; taxamount = subtotal * tax; return subtotal+taxamount; } // or just: return (1+tax)*subtotal; 20 pts Page 2
SOLUTIONS
5. Write the code for the function in the prototype: int str_spaceremaining ( char *s, int size ); Assume the string argument s has been initialized in the calling function as a char array of size characters containing a string of unknown length. The length must be somewhere in the range 0 and size-1 chars. Calculate and return the number of remaining available and unused characters available in the string s. It should indicate how many additional characters could be stored in string s without exceeding the size of s, in addition to whatever string is currently stored there. Do not use any string.h library functions. int str_spaceremaining ( char *s, int size ) { int s_length = 0; while (s[s_length] != '\0') s_length++; // now return remaining space in s[] return size-s_length-1; // allowing for the '\0' null at the end of the string } 20 pts 6. For the following code fill in the blanks: #include <stdio.h> void mysteryxy ( int *x, int *y ); void main (void) { int a=1, b=2; mysteryxy (&a, &b); printf("\n a = %d, b = %d", a, b); } void mysteryxy ( int *x, int *y ) // fill in the values // of variables a & b below { //............................. a = 1 b = 2 *x=*x+*y; // a = a+b = 1+2 = 3 //............................. a = _3_ b = _2_ *y=*x-*y; // b = a-b = 3-2 = 1 //............................. a = _3_ b = _1_ *x=*x-*y; // a = a-b = 3-1 = 2 } What does main() print out: a = _2_, b = _1_ 30 pts Page 3
SOLUTIONS
7. For the function below, when mystery2() is called with arguments x=2 and n=3, what value is returned? int mystery2(int x, int n) return value: _23 = 8_ { int i, p; // called with x=2, n=3 p = 1; for (i = 1; i <= n; i++) // iterations: { // i = 1 2 3 p = p * x; // p = 2 4 8 } return p; } 20 pts
Page 4