The Following Contains Notes for The CISC105 class. The date in which these notes pertain to is December 6th, 2004. Transcribed by Jimmy Wheatley, edited by P. Conrad /*I give Prof. Conrad full permission to change/use/edit any of my notes in this file.*/ December 6th is the day that we received our midterm 2 grades back. We generally went over the exam, the programming parts in particular. There were some interesting points made, but i will generally go over the whole days events. Since many individuals did not have enough time finishing the exam, Prof. conrad is giving an extra opportunity to make up for missed points. On the final exam, there will be an extra section, if you finish in time, that has questions relating to the questions 23 and 24. If you complete these extra questions at the final exam, they may be used to "replace" your question 23/24 combined total, if that would give you a higher grade on Midterm 2. In essence, these final exam "extra questions" are a no-risk "do over" of questions 23/24 on Midterm 2. Prof. Conrad then went on to explain some things about the final exam. He strongly encourages all of us to look at the array excercises, which are located on the web in his code directory. Link: http://udel.edu/~pconrad/cisc105/04F/code arrayExercises arrayExercises2 etc... He also said that we can look over the midterm 2 questions which are online as of yesterday 12/05, with the answers included. http://udel.edu/~pconrad/cisc105/04F/exam/E02_F04_postExamNotes.html Prof. Conrad then went over to talk about number 23 and how many of us answered poorly and in the same way. Here is an example of what he commonly saw. /* The following is a POOR way to do this, */ /* although it does work. */ /* A for loop is a better way, because it */ /* scales. */ scanf("%d %d %d %d %d %d %d %d %d %d", &a[0], &a[1], &a[2], &a[3], &a[4], &a[5], &a[6], &a[7], &a[8], &a[9]); /* Likewise, the following is a poor way to */ /* print out the values; use a for loop instead! */ printf("array is: %d %d %d %d %d %d %d %d %d %d \n", a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9] ); He instructed us that you must use a for loop because in the end, using too many scanf's would be illogical. The correct version of how to do this problem is listed in his code directory. [Note from Prof. Conrad: In particular, using scanf in this way does not "scale". That means, when you go from having 10 elements to 100 or 1000 elements, this way of doing things isn't very effective. On the other hand, using a for loop, it doesn't matter much whether you have 10, 100, or 1000 elements; the amount of code you have to write is about the same.] /*side comment*/ He also went over some random topics pertaining to the midterm. 1. a[5]={3, 2, 5, 4, 8}; What are the "indexes" (or if you prefer the correct plural, the "indices" of this array?) The answer would be that the indexes are 0 1 2 3 4... that is, the numbers that would go inside the [ ] when we access the elements of this array. If there was a question, "Where/What is a[5]?". The answer would be it is outside the parameters of the array. Therefore "It is outside of the array. It is placed in memory right before or after the array." 5 is not a legal index for an array of 5 elements; only 0 through 4 are legal indices for such an array. 2. Does a function call have brackets? No...only the name of the array should be present. WRONG: findFirstOpenTable(tables[], numTables, tableCapacities[]); CORRECT: findFirstOpenTable(tables, numTables, tableCapacities); [Note from P. Conrad... on the other hand, if it is a function defintion or function prototype, the [] should be there, e.g.: void findFirstOpenTable( int tables[], int numTables, int tableCapacities[]); or void findFirstOpenTable( int tables[], int numTables, int tableCapacities[]) { /* CODE FOR BODY OF FUNCTION GOES HERE */ } 3. When a program outputs two different results from two separate compilers, there is a big hint that something is wrong. [Note: e.g. gcc vs. cc] /*end of side comments*/ Prof. then went back to the computer and pulled up two web browsers showing the final exam sample questions. He went over number 3 of the final exam sample questions, which pertains to number 23 on the midterm two exam. Link: http://udel.edu/~pconrad/cisc105/04F/exam/E03_F04_sampleQuestions1.html YOUR CHALLENGE: Find out why the incorrect answer below (which looks correct at first glance) is wrong, and how you would fix it. CLUE: The problem with the code pertains to the variable "min" > cat q23c.c /* q23.c Incorrect Answer to Question 23, CISC105 Exam 2, Fall 2004 P. Conrad */ #include int main(void) { int i; /* loop counter */ int a[10]; /* array with 10 elements */ int min = a[0]; /* minimum element in array */ /* prompt user for ten integers */ printf("Please enter ten integers: "); /* use scanf to get the integers and put the integers into an array */ for (i=0; i<10; i++) scanf("%d",&a[i]); /* print out the array on a single line */ printf("array is: "); for (i=0; i<10; i++) { printf("%d ",a[i]); if (a[i] < min) min = a[i]; } printf("\nmin is: %d\n",min); return 0; } > gcc q23c.c -o q23c > ./q23c Please enter ten integers: 3 2 5 4 8 2 7 8 3 4 array is: 3 2 5 4 8 2 7 8 3 4 min is: -12791248 > Answer: The code is in the wrong order. In the incorrect version, at the time we set min=a[0], a[0] has not yet been assigned a value. Therefore it is imperitive that we initialize "min" AFTER a[0] has been assigned a value, for example, above /*print out the array on a single line*/ [Note from P. Conrad... more to the point, it needs to be AFTER the scanf that gives a[0] a value.] FOR QUESTION 3 on final exam sample questions. Multiple choice: which of these goes in the spot where we see @@@@@ (a) min = a[0]; (b) min = a[i]; (c) min = 0; (d) min = 10; #include int main(void) { int i; /* loop counter */ int a[10]; /* array with 10 elements */ int min; /* minimum element in array */ /* prompt user for ten integers */ printf("Please enter ten integers: "); /* use scanf to get the integers and put the integers into an array */ for (i=0; i<10; i++) { scanf("%d",&a[i]); } /* print out the array on a single line */ printf("array is: "); for (i=0; i<10; i++) { printf("%d ",a[i]); } printf("\n"); /* print the minimum value in the array */ @@@@@ /* initialize min = ??? */ for (i=1; i<10; i++) { if (a[i] < a[min]) min = i; } printf("min is: %d\n",a[min]); return 0; } ANSWER: The correct answer is (c), min = 0; To see why answer a, min=a[0] is wrong, if we used that code with array {1,2,3,4,5,6,7,8,9,10}, it would print out the value 2 (which is not the correct min). The important thing to understand: In this program, min represents the index of the minimum element, rather than the value of the minimum element. Prof. Conrad then went over the general rules of the Final Exam. A lot of the exam will be multiple choice. It is cumulative. Emphasis on arrays, functions, proj 2 and 3. END OF NOTES