Dailyhomework Do as much as you can before the end of October 18 ================================================== 16.1) Test 3 will have 4 questions. The fourth is similar to Prac Test 3 Question 13. The third is similar to Prac Test 3 Question 12. The second is a mixture of the skills in Prac Test 3 Question 4, Program ptr6.c (at Update 15), and Programs ptr1.c and ptr2.c at Update 16. The first is based on ptr5.c at Update 15, but it will involve more variables and their pointers. These topics are also covered in Labs 6, 7, and 8 (yesterday's Lab). 16.2) Today, we will start a new topic, Structs. We start working on Chapters 24 and 25 of the Lectures. STRUCTS are extremely important to the future work you will do in this class and in the next class for majors (Computer Science One). Different from arrays (which collect data of the same type), a struct holds data together that is held together by some concept that we humans want to keep the parts of that concept together. To prepare for this question of Test Four, we will spend two weeks of Lectures, and Lab 9. 16.3) For Practice Test 4, start Question 4. Use the rules in Dailyhomework 16.4 (below). 16.4) This will prepare for a question on the Test FOUR. Notes on how to do the questions on passing structures to functions (Test 4) Step 1) write the function name (typically f, or some given required name) Step 2) write a blank underscore to the left of this name Step 3) put a pair of parens (with lots of space in between), to the right of the function name Step 4) read the question carefully to determine how many parameters are needed; put that number minus one, number of commas in parens. Eg. if four params are needed, then use 3 commas. Step 5) carefully put each param in with its type. The type always precedes the variable name. Types can be such as int, char, float, int*, or struct gas. Note that if it is a struct, then it needs both words: struct and the required name for that struct. This requirement to say both words is only if the struct is defined in the way shown in class, this would not be used if using typedef. Step 6) carefully figure out if any of the params are pass by value or pass by reference (pass by reference is also sometimes called the address of something). Step 7) read carefully several times to ensure that the function header line is completed correctly. Step 8) Put a pair of open-close braces (with many lines in between them) to contain the function code. Step 9) declare any obviously needed local variables Step 10) start to write up the logic that is needed to access the struct components and manipulate any data (such as, filling in new struct components of local variables, or pass by reference params). Ensure that components are accessed by using the period between the variable and the component, e.g., a.part or (*a).part (which could/should be written as a->part) Step 11) carefully read the instructions to see what is to be returned and under what logic e.g., return 1, or return 0, or return a (where a is a struct) Step 12) when you know the type of what is being returned (only one such type can exist in the function) write that type in for note 2 (the blank underscore that is to the left of the function name). Step 13) if nothing is to be returned by the return statement, then do not put any return statements, and write void in the place for note 12. Do as much as you can before the end of October 16 ================================================== 15.1) Today we are going to study how to use files for input and output. We will look at Update 19 to understand the steps needed to work with files. The example problem used will be Question 10 from Prac Test 4 (Update 18). 15.2) Look at Question 10 about files on Prac Test 4 (Update 18). Go through the steps in Update 19 to solve it. 15.3) This files question is important both to students barely trying to scrape a passing grade of C, and to those trying to get an A. Both groups MUST get this question perfectly perfect on the final exam. So, definitely try hard to study for this before Test Four, and use Test Four as a means to see how good you are, and what additional studying you need to do to get this question perfectly finished on the final exam. Do as much as you can before the end of October 11 ================================================== 14.1) Last class and in this week's Lab (Lab 7), we did the case of functions that have simple variables as their parameters (that is, no And symbol). These are examples Question 6, 9 and 12 of Practice Test Three. 14.2) The new topic today is cases where the parameter has an And symbol. These are called pass-by-reference or pass-by-address. The earlier, simple case, is called pass-by-value. The only difference in the pass-by-address case, is that when we come to the RETURN statement, we need to do one more new step just before we quit the function, we must transfer the values from within the DUNGEON to outside to the caller, only for those which are pass-by-address. 14.3) We are doing Prac Test Three's Question 7, and then Question 13 in class today. The Lab 8 next week, and just before Test 3) will cover Question 13 and 10. Do as much as you can before the end of October 4 ================================================= 13.1) Practice Test 3, Question 6. This is a trace of a function, which we will sometimes refer to as a DUNGEON. In this style of problem, we see the "int main" line, and we immediately do our first step: we write MAIN then we see the main variables, so we write our workspace to look like MAIN a | b | c ---|-----|----- | | 2 | 3 | 5 then we do the printf (line 7), so in our output box, we get ______________ | a=2 b=3 c=5 | |______________| then, we try to do line 9, and say uh-oh, we have a function call, we write it in our workspace: MAIN a | b | c ---|-----|----- | | 2 | 3 | 5 a = f (b, a, b+c) and we start to do new work, that will let us carefully trace the function. We do steps S1, S2, and S3. MAIN a | b | c ---|-----|----- | | 2 | 3 | 5 a = f (b, a, b+c) S1 MAIN b | a | b+c S2 ########################### we BUILD THE WALL | | | S3 DUNGEON c | b | a | (I have said this since 2010 long before other people twisted its meaning.) Then we start to do the work of tracing: MAIN a | b | c ---|-----|----- | | 2 | 3 | 5 a = f (b, a, b+c) S4 3 2 8 (came from 3+5) S1 MAIN b | a | b+c S2 ########################### we BUILD THE WALL S3 DUNGEON c | b | a We drop those values of our Step 4 (S4) into the Dungeon MAIN a | b | c ---|-----|----- | | 2 | 3 | 5 a = f (b, a, b+c) S4 3 2 8 (came from 3+5) / S1 MAIN / b | a | b+c / S2 #######|################### we BUILD THE WALL | | | S3 DUNGEO| c | b | a | ---|-----|------ \ | | \ | | S5 3 | 2 | 8 We start doing the Dungeon steps: We see that we have a local variable, sum, so we write it to the table's side MAIN a | b | c ---|-----|----- | | 2 | 3 | 5 a = f (b, a, b+c) S4 3 2 8 (came from 3+5) S1 MAIN b | a | b+c S2 ########################### we BUILD THE WALL S3 DUNGEON c | b | a | sum <---- S6 ---|-----|-------|------ | | | S5 3 | 2 | 8 | Now we do Line 18 (USING ONLY DUNGEON VALUES): MAIN a | b | c ---|-----|----- | | 2 | 3 | 5 a = f (b, a, b+c) S4 3 2 8 (came from 3+5) S1 MAIN b | a | b+c S2 ########################### we BUILD THE WALL S3 DUNGEON c | b | a | sum <---- S6 ---|-----|-------|------ | | | S5 3 | 2 | 8 | 13 (add 3,2,8) Line 19 tests sum (which is 13) versus 24 (which is a*c i.e. 8*3) Because the test is false, we skip line 20. Doing line 21, sum (i.e., 13) is not Less than b*c (i.e., 3*2), so skip line 22. Then line 24 says "return"; when we see return we need to do THREE new steps. These 3 steps are: Fill Envelope; Fly Envelope; DUNGEON goes poof, up in smoke. Fill Envelope _______ |\ /| | \___/ | | | | 16 | |_______| Fly Envelope to replace the calling "f" and its parenths and contents. Then poof the DUNGEON. So, our workspace looks like: MAIN a | b | c ---|-----|----- | | 2 | 3 | 5 _______________ a = |\f (b, a, b+c)/| <--------- Erase this f and parens | \ / | Replace by 16. | \_________/ | | | | 16 | |_______________| So, our line (number 9) is a = 16, so our final workspace looks like MAIN a | b | c ---|-----|----- | | 16 | 3 | 5 Hence, line 10 causes our updated output box to be _______________ | a=2 b=3 c=5 | | a=16 b=3 c=5 | |_______________| 13.2) Next, do Question 9. It is a double DUNGEON, so need to solve the inner dungeon first, and then use its result (envelope) as a number that is needed by the outer dungeon. Questions 12 and 9 will also be covered in the Lab next week. Do as much as you can before the end of October 2 ================================================= 12.1) Do Update 16, the two programs coordinated with Lab 6. Do as much as you can before the end of September 27 ==================================================== 11.1) Food Bank, Option 2 is simply an easier version of option 1, you only always need to put the new entry in the Req_Inv_Type table, and the amount in Req_Amount Table. This means you do not need to search for the word in the Request Table. 11.2) Food Bank, Option 3 is: if (choice==3) { if (req_count == 0) { message 1 : No hunger } else if (don_count == 0) { message 2 : Scrooges in this town } else { // request does not find a match in donations found = -99; for (i=0; ireq_amt[0]) { don_amt[found] -= req_amt[0]; //reduce the donation table's amount; //remove the request as above for (i=1;i int main(void) { int i, start=3, days=31, p; for (i=1; i16;i--)" runs 10 times. 3.8) Let us focus on Practice Test One's Question 4. Its solution involves first writing a for loop that runs 50 times. So, as your first step, write for (i=0;i<50;i++) Then, you are told that the loop must read in 50 numbers: So, Write: for (i=0;i<50;i++) { scanf("%d", &NumFromKeyboard); } Note how you must get to the point where you are comfortable writing a simple for-loop as above, based on the problem sheet. Now that you have the 50 numbers coming in, one at a time, each one of them being temporarily available in NumFromKeyboard, you can examine each one immediately after the scanf. The problem sheet tells us to check to see if the number is greater than 100. So, our next step is to expand the code to: for (i=0;i<50;i++) { scanf("%d", &NumFromKeyboard); if (NumFromKeyboard > 100) ... } The ... should be replaced by whatever we are being asked to do. We are being asked to add each number into an adder that will tally up these big numbers. The tally needs to be initialized to zero (per the problem sheet). So, our next step is: sum = 0; for (i=0;i<50;i++) { scanf("%d", &NumFromKeyboard); if (NumFromKeyboard > 100) sum += NumFromKeyboard; } Finally, we incorporate the requirement that the tally gets multiplied by 85. We get: sum=0; for (i=0;i<50;i++) { scanf("%d", &NumFromKeyboard); if (NumFromKeyboard > 100) sum += NumFromKeyboard; } sum *=85; printf("%d", sum); To wrap up, we merely need to declare all our variables, and put the starting lines (#include, etc.) and the ending lines (return zero, etc.). 3.9) Now, try Practice Test One's Question 5. Next week's Lab will cover Question 6, and maybe (if time permits) Question 7. 3.10) Look at Update 6, about the help available for this class Do as much as you can before the end of August 28 ================================================= 2.1) Ensure that you know that any calculation that is INT DIVIDE BY INT (e.g., 7/2) produces a TRUNCATED result, which is an int (7/2 yields 3). To ensure that you do not truncate (if you do not INTEND to truncate), you can convert one of the values to float or double before the division, so can do: (1.0*7)/2, or 7/(2*1.0), or you can CAST by saying ((float)7)/2. 2.2) Ensure that you understand the percent operator that is used to get the remainder of a calculation. Examine and study the program digits.c. A % B means you must mentally do the steps of taking away as many B's from A to get closer to zero, and then what you are finally left with, is the answer needed. So, (-17) % (4) yields -1. While, (-17) % (-4) also yields -1. 2.3) When writing a program such as area of a rectangle (or, another may be, E = M times cSquared), make sure that we are not tempted to state the equation atop of (at the top of) the lines that give the values for M and c, or, in the rectangle program, the lines that read in length and width. This is simply re-emphasizing the issue in 1.8 seen earlier. 2.4) Ensure that you complete the submission of Assignment Zero before Friday night. (See Update 2 on the class webpage). 2.5) Read Chapter 5's sections Conditional Expressions, if-else Construct (Note that the author of Lectures webpage may have an error, so you need to change .pdf to .doc, if needed) 2.6) Practice Test One (see Update 3 on class page). Do Question 1. These are all you must know for Test One Question 1. = += ++ 8%3 %3d %7.4f scanf printf ("P=_%7d ... do not ignore the single _ int/int gives truncated answer %M.Pf means *** . -------- \ P / \ P+1 / \ D / 1. P spaces after . 2. then P+1 for the . 3. then put in the quant that goes before . 4. Then, count up all used, say D 5. if D