600.120 Intermediate Programming Spring 2009 Midterm WebCT _______________________ "I agree to complete this exam without unauthorized assistance from any person, materials, or device." Signature ___________________________________________ Date ________________ Do all work on the test itself. Total point value is 140. Individual questions are worth the points indicated in []s. Show work for partial credit! =============================================================================== ) What is the relative order of the following four operations when creating and running a C program? (number them 1-4) [4] ____ edit ____ pre-process ____ execute ____ compile Write a single C statement for each of the following tasks. Include all required punctuation (even ;) for exact syntax. ) Define a symbolic constant (not variable) named 'INIT' for the first letter of your last name. [3] ) Declare an uninitialized character array called 'word' just large enough to hold "ansi". [3] ) Display a float called x and an integer called n on the screen. The float must be displayed in a field width of 12 with exactly 3 digits after the decimal place. The integer must be displayed with at least 4 digits. Separate the values with a tab and include an end of line. [6] ) Write the function prototype for a function called 'thing' that has two float arguments and returns nothing. [3] ) Write a pre-processor macro called 'RATEP' that computes the ratio of two values as a real number percent. For example RATEP(161,200) would compute 80.5 (not 80). [5] ) Write a void C function called 'newRA' that has two parameters. This function should make the first parameter refer to an array of doubles, with all elements initialized to 0, of the integer size specified by the second parameter. [10] ) Write C statements which use the above function to create an array of doubles called 'dray' with 100 elements. [4] ) What is the output of the following program? /**************************************************************/ #include int fa (int a, int *b) { int c = 5; a = 20; *b = a / 2; return *b / 2; } int main(void) { int c = 6, b = 12, d = fa(3, &b); printf("1) %d %d %d\n", d, c, b); c = 6; b = 12; d = fa(b, &c); printf("2) %d %d %d\n", d, c, b); } >>> OUTPUT: [10 pts] 1) 2) ) Write a C function called shifty that takes a string (character array) and an integer shift value as parameters. The function should change the string such that each alphabet charater in the original string is replaced by a new character which is shifted forward in the alphabet by the specified shift value. When the end of the alphabet is reached, wrap around to the beginning when shifting. Preserve capitalization. For example, char *s = "Good luck! 2u"; shift(s, 10); puts(s); // results in "Weet bksa! 2k" because abcdefghijklmnopqrstuvwxyz -> qrstuvwxyzabcdefghijklmnop with shift 10. You can use functions from the ctype library. [15] ) What is wrong with the following code segment? [2] char *s1, s2="good luck"; strcpy(s1, s2); ) What is wrong with the following code segment? [2] int ra[20]; ra = realloc(ra, 10); Multiple choice: Choose exactly ONE C statement for each of the following tasks. There may be more than one correct answer, but only choose one. Circle your choice. Do NOT be ambiguous. Assume the appropriate declarations have been made. Each question is worth 2. ) Declare the variable 'dptr' which will be a pointer to a double number. a) double & dptr; b) dnum * dptr; c) double * dptr; d) dptr -> double; ) Assign the address of character variable 'ch' to character pointer 'cptr'. a) &ch -> cptr; b) cptr = ch; c) *cptr = *ch; d) cptr = &ch; ) Assign the value '*' to char variable 'ch' using the pointer 'cptr' (assume it points to 'ch' from above). a) &cptr = '*'; b) cptr = '*'; c) *cptr = '*'; d) cptr = & '*'; ) Print the memory address of char variable 'ch' (assume above). a) printf("%p", cptr); b) printf("%c", ch); c) printf("%p", &ch) d) printf("%s", '*'); ) Declare a string initialized to "work it". a) char str[]='work it'; b) char str[7]="work it"; c) string str="work it"; d) char str[8]="work it"; ) Print the character 'i' using the string 'str' from above. a) printf("%p", str+5); b) printf("%c", str+6); c) printf("%c", *(str+5)); d) printf("%c", *(str+6)); ) Read a string from standard input into character array 'word'. a) scanf("%c", word); b) scanf("%s", &word); c) scanf("%s", word); d) scanf("%s", *word); ) Read a line of standard input (including spaces) into string 'buf'. a) scanf("%s", buf); b) getline(buf); c) scanf("%l", buf); d) gets(buf); ) Concatenate the string from character array 's1' to string 's2'. a) strcat(s1, s2); b) strcpy(s1, s2); c) s2 += s1; d) strcat(s2, s1); ) Declare a structure containing a character array which will hold words with up to 6 letters, and two real number fields. a) struct s5 { char car[7]; float n1, n2; }; b) struct s5 { char car[6]; float n1; float n2; }; c) struct s5 { char car[7]; double n1; double n2; }; d) struct s5 { char car[6]; double n1; n2; }; ) Declare a structure containing an integer, a character and a field of the structure type in the above question. a) struct s6 { int num, char ch, s5; }; b) struct s6 { int num; char ch; s5 s5var; }; c) struct s6 { int num; char ch; struct s5 s5var; }; d) struct s6 { int num, char ch, struct s5; }; ) Declare a variable to be of the structure type from the above question. a) s6 s6var; b) typedef struct s6 s6var; c) typedef s6 s6var; d) struct s6 s6var; ) Give the type name 'MYTYPE' to the s5 struct defined above. a) #define MYTYPE s5 b) #define s5 MYTYPE c) typedef s5 MYTYPE; d) typedef struct s5 MYTYPE; ) Assuming 'var5' has been declared a variable of the structure type s5 above, initialize its character array to the empty string. a) var5.car = ""; b) var5->car = ""; c) var5->car = '\0'; d) var5.car[0] = '\0'; ) Declare variable 'sptr' to be a pointer to an s5 struct from above. a) struct s5 * sptr; b) struct s5 & sptr; c) s5 <- sptr; d) MYTYPE * sptr; ) Use sptr to give the value 32.3 to the first real number field in the s5 struct it points to (from above). a) sptr.n1 = 32.3; b) sptr->n1 = 32.3; c) (*sptr).n1 = 32.3; d) *sptr->n1 = 32.3; The next few questions assume the following definitions: struct it { int num; char word[5]; }; typedef struct thing { float f; struct it t; } TYPEthing; struct it s2 = {3, "yo"}; TYPEthing t1; ) Read the current record in the random access file pointed to by 'rptr' into structure variable 't1' from above. a) scanf(rptr, "%t", t1); b) fscanf(rptr, &t1); c) fread(&t1, sizeof(TYPEthing), 1, rptr); d) fread(rptr, t1, sizeof(TYPEthing), 0); ) Read the next word in the sequential text file pointed to by 'fptr' into the word field of variable s2 above. a) scanf(fptr, "%s", s2.word); b) fscanf(fptr, "%s", s2.word); c) fscanf(fptr, "%s", &s2.word); d) fgets(s2.word, fptr); ) Declare a text file pointer named 'fptr'. a) fptr = FILE of TEXT; b) TEXT FILE fptr; c) FILE fptr; d) FILE * fptr; ) Assuming that 'fptr' points to a sequential text file opened for writing, write the current value of the integer in s2. a) fwrite(fptr, "%d", s2.num); b) fwrite(&s2.num, "%d", fptr); c) fprintf(fptr, "%d", s2.num); d) printf("%d", s2.num); ) Set file pointer 'fptr' for reading the existing file 'f1.txt'. a) fptr = fopen("f1.txt", "r"); b) fopen(fptr,"f1.txt",r); c) fptr = fopen('f1.txt', 'r'); d) fopen(fptr,"f1.txt",e); ) Overwrite/create the random access file 'r1.dat' to be used with file pointer rptr. a) rptr = fopen('r1.dat', 'c'); b) fwrite("r1.dat", rptr); c) rptr = fopen("r1.dat", "w"); d) fcreate("r1.dat", rptr); ) Set random access file pointer rptr to the beginning of its file. a) reset(rptr); b) fseek(rptr, 0, SEEK_SET); c) rewind(rptr); d) fset(rptr, BEGIN); ) Look at the program on the next page very carefully (do the problem first). Then write a RECURSIVE function that could be added to the program which would delete every occurrence of a character parameter c from the list. Make sure you don't leave any memory leaks. Here is the header. [12] void deleteAll(List *hptr, char c) /*******************************************************/ #include #include struct node { char ch; struct node * next; }; typedef struct node * List; void pout(List head) { if (head) { printf("%c ", head->ch); pout(head->next); } else printf("\n"); }; void pal(List *hptr) { List temp, curr = *hptr; while (curr) { temp = (List) malloc(sizeof(struct node)); temp->ch = curr->ch; temp->next = *hptr; *hptr = temp; curr = curr->next; } }; int main(void) { List head, temp; char ch = 'A'; int i; head = (List) malloc(sizeof(struct node)); head->ch = ch; head->next = NULL; temp = head; for (i=1; i<=4; i++) { temp->next = (List) malloc(sizeof(struct node)); temp = temp->next; temp->ch = ch + i; temp->next = NULL; } pout(head); pal(&head); pout(head); return 0; } /***********************************************************/ ) What is the output of the program above? [15 points]