Spring 1996
This study sheet is composed of questions at all levels. You should try to solve them all. If you can not solve one, do not stay in that question for the rest of your life. Move to the following one. It is a long study sheet and a hard one. Solving these questions you will be in great shape for the quiz. It does not mean that the quiz will be on the same level as this study sheet. It is just a way of preparing yourself for all kind of possible questions involving C. G O O D L U C K !!! = = = = = = = = === 1. Give the function header for each of the following. a) Function hypotenuse that takes two double-precision floating point arguments side1 and side2, and returns double-precision floating point value. b) Function IntToFloat that takes an integer argument Number and returns a floating point value. 2. Label the elements of a 5-by-3 two dimensional array SALES to indicate the order in which they are set to zero by the following program segment. for (row = 0; row <= 2; row++) for (column = 0; column <= 4; column++) SALES[column][row] = 0; 3. What does the following program do? #includeint mystry2 (const char *); void main( void ) { char string[80]; printf("Enter the string: "); scanf("%s", string); printf("\n%d", mystry2(string)); } int mystry2 (const char *s) { int x = 0; for (; *s != '\0'; s++) x++; return x; } 4. What does the following program do? #include #define SIZE 5 int whatIsThis (int [], int); void main( void ) { int a[SIZE]={1,2, 3, 4, 5}; printf("\nThe result is: %d", whatIsThis(a, SIZE)); } int whatIsThis (int b[], int num) { if (num == 1) return b[0]; else return b[num -1] + whatIsThis(b, num - 1); } 3 5. Given the equation y = ax + 7, which of the following, if any, are correct C statements for this equation? Why? a) y = a * x * x * x + 7; b) y = a * x * x * (x + 7); c) y = (a * x) * x * x + 7; d) y = a * (x * x * x) + 7; e) y = a * x * (x * x + 7); 6. What does the following program do, if the user inputs two numbers 6 and 9. #include main() { int i, j, x, y; printf ("enter two integers : "); scanf("%d%d", &x, &y); for (i=1; i<= y; i++){ for (j=1; j<= x; j++) printf("@"); printf("\n"); } } 7. Write a program that reads in a hexadecimal number one character at a time and converts it to a decimal number. Your program should take care of sign of the number. If the input number is negative, the final decimal number should also be negative. You can assume that the number will be entered with no spaces. 8. Write a program that reads input characters one by one until end of file. As it reads it should convert lower case alphabets to upper case, and vice-versa, and print them. Tabs should be printed as 3 spaces. At the end it should print the number of blanks, tabs, newlines in the input data. 9. A number n is said to be a perfect number if it is equal to the sum of all its divisors, excluding itself. For example, the divisors of 6 are 1, 2, and 3. Since 6 = 1 + 2 + 3, it is a perfect number. Write a function called is_perfect that takes an integer as argument and returns 1 if it is a perfect number and 0 otherwise. Write a main program that reads in a number n and prints all perfect numbers between 1 an n by using the function is_perfect. 10. Which of the following you can NOT use as identifiers and why? 4th star* scanf one-to-1 11. What is the minimum size of a character array to store the following string: "\a\"This is a string\"\n" 12. What gets printed? Explain. (If you need, you can assume the address of i to be 2279468.) int i = 3, *p = &i; printf( "%d %d %d %d\n", p, *p + 7, **&p, p - (p-2)); 13. Which statement is legal, which is not, why and why not? int i = 9, a[3], *p; char b[4], *q; a = p; p = a; q = &i; q = b; 14. Here are groups of macros followed by source code lines that use them. Will these source codes compile and if they compile what gets printed? a) #define NEW(X) X+5 #define PR(y) printf("y is %d\n",y) void main( void ) { int x = 2, y = 3, z = 5; PR(z); PR(NEW(x)); PR(x*NEW(-x)/z-y); } b) #define SIX=6 void main( void ); { printf("%d\n", SIX*2 ); } c) #define square(x) x*x #define PRT(u) printf("u is %d\n",u) void main( void ) { int y = 7; PRT(100/square(y)); PRT(square(y++)); PRT(square(++y)) } 15. Write a program to draw the following pictures. First starting writing a function that prints the n-th line of the object and use to print the picture required. (Use for-loops, while, switch.) a) Parallelogram: ******** ******** ******** ******** b) Pyramid: 0 *1* ++2++ $$$3$$$ ****4**** +++++5+++++ $$$$$$6$$$$$$ ...... 16. Write a program that prints out the sum of the first 50 odd numbers. 17. Write a program that reading a text counts the number of punctuation characters there are in the it. 18. What is printed by the following piece of code? void main( void ) { int x; int a[10] = {12, 11, 10, 9, 8, 7, 6, 5, 4, 3 }; int *p; p = &x; x = 5; *p = 8; p = a + 5; printf( "%d, %d\n", x, *p ); } 19. Exercise 1 page 440 in KP. 20. Exercises 1 and 2 page 392 in KP. 21. Exercises 3 and 4 page 393 in KP. 22. Exercise 27 page 399 in KP. 23. Exercises 14 and 15 page 319 in KP. 24. Exercise 4 page 275 in KP. 25. Exercise 9 page 276 in KP. 26. Exercises 10 and 11 page 277 in KP. 27. Exercises 12, 13 and 14, page 278 in KP. 28. Exercise 15 page 214 in KP. 29. Exercise 4 page 210 in KP. 30. Exercise 7 page 168 in KP. 31. Exercises 9, 10 and 11 page 169 in KP.
Page Created: Feb 20, 1996 Last Modified: Feb 20, 1996 Maintained by: Stan Sclaroff