90 likes | 211 Views
Quiz 3. Question 1. What is printed by char str1[100] = “bob”; puts(str1); printf ( sizeof (str1)); printf ( strlen (str1)); printf (“%d”,str1[2]);. bob. 100. 3. 98. don’t memorize the ASCCI table but do know that characters are represented by numeric codes. Question 2.
E N D
Question 1 • What is printed by • char str1[100] = “bob”; • puts(str1); • printf(sizeof(str1)); • printf(strlen(str1)); • printf(“%d”,str1[2]); bob 100 3 98 don’t memorize the ASCCI table but do know that characters are represented by numeric codes
Question 2 • What is printed by • intarr[] = {1,2,3}; • int* p = arr; • printf(“%d”, arr[2]); • printf(“%d”, *++p) • printf(“%d”,p); 3 2 although you don't know exactly which address in this example an address
Question 3 typedefstruct{ int x; int y; }point; int main() { point pt1 = {1,1}; point pt2 = {2,2}; point pt3 = {3,3}; point arr[3]; arr[0] = pt1; arr[1] = pt2; arr[2] = pt3; return 0; } print pt1's x coordinate printf("pt1.x = %d\n", pt1.x); what is the value of arr.y[2]? trick question: it's a compiler error what is the value of arr[2].y? 3 (arr[2] is a struct equal to pt3)
Question 4 NULL • What does fopen return if it fails? • What do scanf and fscanf return? • What is the difference between text files and binary files? • What happens to an existing file if it is opened for writing? Number of values successfully read Text files store character representation of data, binary files store the bit representation of data the contents of the file are over-written
Question 5 • Write a while loop to print the numbers between 6 and 66 (including 6 and 66) • What does this print? intstart = 6; intend = 66; while (start <= end){ printf("%d\n", start); start++; } inti; for(i = 6; i <= 66; printf("%d\n",i++)); the numbers between 6 and 66
Question 6 • Write a function to reverse the contents of its integer array parameter • The function should not return anything and should have an additional parameter that specifies the size of the array • e.g. if arr = {1,2,3,4}, • reverse(arr, 4) • changesarrto {4,3,2,1} void reverse(intarr[], int size) { int lo = 0; int hi = size-1; while(lo < hi){ int temp = arr[lo]; arr[lo] = arr[hi]; arr[hi] = temp; lo++; hi--; } }
Question 7 • Write a function to sum the contents of its two input arrays, return a pointer to an array in dynamic memory, or null if input array sizes are different int* sumArrays(int in1[], int size1, int in2[], int size2) { inti; int* result; if(size1 != size2) return NULL; result = malloc(size1 * sizeof(int)); for(i = 0; i < size1; i++){ result[i] = in1[i] + in2[i]; } return result; }
Bonus Question – What the *#@*! char * fun(char*s1, char*s2) { char*s = s1; while((*s++ = *s2++) != 0); return s1; } it's a common implementation of the strcpy function it uses pointer arithmetic ... dereferencing ... and the fact that the null character (\0) has the ASCII value of 0