Short Answer 3 Points Each 1. What would the unix command gcc somefile.c -o someotherfile.exe do? 2. Name two basic data types in C. 3. A pointer data type holds what piece of information? 4. This key word is used to create a variable local to a function that won t die when the function returns. In other words, it can only be accessed in the function it is declared in, and once initialized, its values remains for the life of the program. 5. What would be the value of x after the following statement? float x = 3.4 * (1 + 1) - 0.7; 6. What would be the value of x after the following statement? int x = 3 * 5 % 4 + 4; 7. What would the following evaluate to? (4 > 8 && 30 < 10) 5!= 9 8. What would the following evaluate to? 4 > 8 && (30 < 10 5!= 9) 9. Given the following structure definition, give TWO different ways to initialize the month field to April, the day field to 20, and the year to 1972. typedef struct char month[10]; int day; int year; Date; 1
CODE SNIPPET 1 CODE SNIPPET 2 CODE SNIPPET 3 int x = 7; if(x > 8) printf("peas "); printf("and Carrots\n"); double x = 72.5; if(x > 90) printf("a\n"); else if(x > 80) printf("b\n"); else if(x > 70) printf("c\n"); else if(x > 60) printf("d\n"); else printf("better luck next semester\n"); int i = 1; for(i = 1; i < 9; i *= 2) printf("%i\n", i); printf("is your letter grade\n"); 10. What does CODE SNIPPET 1 print to the screen? 11. What does CODE SNIPPET 2 print to the screen? 12. What does CODE SNIPPET 3 print to the screen? 2
CODE SNIPPET 4 CODE SNIPPET 5 CODE SNIPPET 6 int i; int sum = 0; int N = 10; i = 1; while(i < 10) sum += i; i = i + 2; printf("%i\n", sum); int a[10], i; for(i = 0; i < 10; i++) a[i] = 2 * i; printf("%i\n", a[7]); int c[10], i, s; int a[10] = 2, 5, 3, 7, 3; int b[10] = [0] = 1, [4] = 5, [6] = 8, [9] = 23; for(i = 0; i < 10; i++) c[i] = a[i] + b[i]; s = c[4] + c[9]; printf("%i\n", s); 13. What would be the output of CODE SNIPPET 4? 14. Write a program that does the same thing as CODE SNIPPET 4 but uses a for loop instead of a while loop. 15. What would be the output of CODE SNIPPET 5? 16. What would be the output of CODE SNIPPET 6? 3
#include <string.h> CODE SNIPPET 7 CODE SNIPPET 8 CODE SNIPPET 9 void FUBAR(char str1[], char str2[]) int j; for(j = 0; j < strlen(str1); j++) str2[j] = str1[strlen(str1) - 1 - j]; str2[j] = '\0'; int main() char s[50] = "I AM"; char t[50] = "CAPTAIN PLANET"; printf("%s %s\n", s, t); FUBAR(s, t); printf("%s %s\n", s, t); 1. 2. int V = 4; 3. void foo(int W) 4. 5. int X = 3; 6. V = 7; 7. W = 7; 8. X = 7; 9. Y = 7; 10. Z = 7; 11. 12. int main() 13. 14. int Y; 15. int Z; 16. foo(z); 17. V = 7; 18. W = 7; 19. X = 7; 20. Y = 7; 21. Z = 7; 22. 23. void YOOHOO(int x, int y) int temp = x; x = y; y = temp; int main() int a = 7, b = 12; printf("%i, %i\n", a, b); YOOHOO(a, b); printf("%i, %i\n", a, b); 17. For CODE SNIPPET 7, describe in English what the function FUBAR does? 18. What would be the output of CODE SNIPPET 7? 19. Which lines in CODE SNIPPET 8 would cause compilation errors(note the numbers to the left are line numbers for identification, not part of the code.)? 20. What would be the output for CODE SNIPPET 9? 4
CODE SNIPPET 10 1. 2. #include <string.h> 3. main() 4. 5. /*This program is supposed to prompt for a string and make all lower case 6. characters there upper case equivalent you don't need to worry about how 7. it works for these questions, just find the errors, use the comments to 8. figure out how to fix them*/ 9. /*only one character needs to be added/removed/changed for 10. each error */ 11. char str[50]; 12. int i; 13. print("enter a string: "); 14. scanf("%s", &str); 15. for(i = 0; i < strlen(str); i++) 16. 17. //if str[i] is between 'a' and 'z' then set it to str[i] - 'a' + 'A' 18. //otherwise set it to str[i] 19. str[i] = (str[i] >= 'a' && str[i] <= 'z')? str[i] - 'a' + 'A' str[i]; 20. 21. printf("the modified string is %s\n", str); 22. return 0 23. 21. When you try to compile CODE SNIPPET 10, you get the error main.c:19: error: parse error before "str" Identify the error and what needs to be done to fix it. 22. You also get the error main.c:23: error: parse error before '' token Identify the error and what needs to be done to fix it. 23. Assuming you fixed those errors correctly, when you recompile, you get the error Undefined first referenced symbol in file print /var/tmp//cccdmvlb.o ld: fatal: Symbol referencing errors. No output written to a.out collect2: ld returned 1 exit status 5
Identify the error and what needs to be done to fix it. 6
Coding 15.5 Points Each Choose 2 of the 4 questions below and write the C code to create a program that meets the specified criteria. 24. Write a program that prompts the user for three integers. The program then calls a function called sortthem. sortthem takes three parameters. (The parameter type is for you to decide.) The return type of sortthem is void. When the function returns, the three arguments are in ascending order, so the first argument is the smallest, the second argument is the second smallest and the last argument the largest. After it returns, main then prints out the variables in sorted order. 7
25. Write a program that prompts the user for an integer and then prints out a multiplication table for the integers from 1 to N where N is the number entered. A multiplication is a table where the rows and columns are numbered from 1 to 5 and the intersection of the i th row and the j th column is i * j. So if the user entered 5, your program would print out: 1 2 3 4 5 1 1 2 3 4 5 2 2 4 6 8 10 3 3 6 9 12 15 4 4 8 12 16 20 5 5 10 15 20 25 The spacing should be such that each number takes up 8 spaces. Don t forget to put an extra space at the intersection of the header row and header column (i.e. the first cell in your table).. 8
26. Write a program that prompts the user for an unsigned integer. It then takes that number and prints out every other digit. So if the user enters 123456, the output would be 135. If the user entered 5788743 the output would be 5873. Notice it starts with the first digit and skips every other digit after that. You may not use the string library or sprintf. You may assume you have access to the following functions. unsigned length(unsigned x); - returns the number of digits in x unsigned power(unsigned a, unsigned b); - returns a raised to the b th power (a b ). 9
27. Write a program that prompts the user for 100 floating point numbers. (You may assume the user will input the correct data.). After the user enters all of the data, the program then prints out, the minimum value entered, the maximum value entered, then the mean and variance of all of the values entered. Recall that for N numbers, x 1, x 2,, x N: Mean N N x i AND = i = 1 Var N = i = 1 2 ( x Mean) i N 10