Notes 21 ======== Extra version of Baseball: A baseball player's batting record is to be read in and the number of hits, the number of walks, the number of sacrifices, and the number hit by the pitch, are to be outputted. Write a complete program that reads a positive integer N, and then N player numbers and batting records. Each player's record ends with an asterisk. Trips to the plate are coded in the batting record as follows: H--hit, O--out, W--walk, S--sacrifice, P--hit by pitch. The program should output for each player the input data followed by the four numbers described above. {\bf Additionally, after processing all individual players, write the Grand Total of Hits scored} (see the final line in output below). (You can assume that N is at least 2, for the grammar of the final line). The Grand Total is produced by adding up all the players' hits. Sample input (from the keyboard, not from a file): 3 12 HOOOWSHHOOHPWWHO* 4 OSOHHHWWOHOHOOO* 7 WPOHOOHWOHHOWOO* Output (to the screen, not a file): Player 12's record: HOOOWSHHOOHPWWHO Player 12's hits: 5 Player 12's walks: 3 Player 12's sacrifices: 1 Player 12's hit by pitch: 1 Player 4's record: OSOHHHWWOHOHOOO Player 4's hits: 5 Player 4's walks: 2 Player 4's sacrifices: 1 Player 4's hit by pitch: 0 Player 7's record: WPOHOOHWOHHOWOO Player 7's hits: 4 Player 7's walks: 3 Player 7's sacrifices: 0 Player 7's hit by pitch: 1 Grand Total of Hits scored by the 3 players: 14 Notes 20 ======== Extra versions of Random Words: 1) Write a complete program that reads in a large positive integer N in order to create and print out N random words separated by single spaces (here, word simply means a sequence of randomly chosen alphabetical letters, not necessarily an actual word that can be found in the dictionary). The catch is that if N is prime, the 'words' that are produced should be of random lengths between 11 and 16 letters (inclusive); if N is not prime, the random lengths should be between 7 and 17 letters (inclusive). The program should print out each of the N computed strings (printed out with the "\%s" format). Remember to use the end-of-string special symbol. Also, remember to call srand. 2) Write a complete program that writes to screen-output 2000 random 'words', separated by single spaces (here, word simply means a sequence of alphabetical letters, not necessarily an actual word that can be found in the dictionary). The 'words' you write should be of random lengths between 5 and 8 characters. THE ADDED REQUIREMENT IS THAT each generated word must begin with the letter 'm' and must end with the letter 'i'. (Examples of such random words might be ``merci'', ``muapppi'', ``mmmloboi'', ``mcdonali'', ``mkjtwpi''.) The program should print out each of the 2000 computed strings (printed out with the "\%s" format). HINT: Once you know the length of the word because you called rand(), your growing string should put the letter m in the zeroth position, and the letter i in the last position (at position length minus 1); fill the positions in between with random letters chosen from the 26 possibilities. Remember to use the end-of-string special symbol. Also, remember to call srand at the beginning. Notes 19 ======== For Practice Test 4, For Files questions, there are two phases: First write the answer to the non-file version of the question. This will seem a little more difficult than Test 1's program. Then, in phase two, you must make sure you do four steps, which will give you 8 points. 1) put in the FILE statement for the input file and the output file. 2) put the fopen statement for both the input file and the output file. 3) write the scanf's as fscanf(ifp, AND THEN SAME AS OLD WAY write the printf's as fprintf(ofp, AND THEN SAME AS OLD WAY 4) fclose (input file) and fclose (output file) before step 2, you will need to read in the two file names as strings. Notes 18 ======== Here is how to approach the the actual question Two for Test 4. Notes on how to do the questions on passing structures to functions (Test 4) 1) write the function name (typically f, or some given required name) 2) write a blank underscore to the left of this name 3) put a pair of parens (with lots of space in between), to the right of the function name 4) read the question carefully to determine how many parameterss are needed; put that number minus one, number of commas in parens. Eg. if four params are needed, then use 3 commas. 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. 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). 7) read carefully several times to ensure that the function header line is completed correctly. 8) Put a pair of open-close braces (with many lines in between them) to contain the function code. 9) declare any obviously needed local variables 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 be written as a->part) 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) 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 undersore that is to the left of the function name). 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. Notes 17 ======== Start working on getting expertise for the picture question of Test Four (It will be the third question on Test Four). Practices for it are Q11, Q13, Q14 on Practice Test Four. To start doing the problem, first write the function header. Typically, it will look like void f (int** pic1, int NRows, int NCols, int value1){ int i,j; Then you need to put in one (or two loops) to go through the i and j values. Typically, most questions will be asking you to write code to copy from one part of the picture to another. If that is the case, then at this point you need to make a decision: will the loops traverse (loop through, paint, highlight, scratch-off, touch, all have same meanings in this context) the positions of the Source positions, or the Destination positions? Once you make that choice, write down those loop/s and write the next line (the one with the equal sign to achieve the copy) So, for example, if it said copy from upper left quadrant to lower right quadrant, and you decided to make the loops do the Source, then your loops would look like: 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;i0;i--) ( for (j=0; j for (index=0; index<5; index++) { This starts a for loop that wil run (apparently, but we need to verify) 5 times Everything that is within this loop's { and } will run (5 times). the first of such (to be repeated 5-times) things is > for (stars=0; stars< (maxfreq - freq[index]); stars++) > printf(" "); So, you need to figure out what this new for-loop does. Well, it is a loop controlled by the counter "stars". To figure out how many times this new loop will run, we need to make sense of (maxfreq - freq[index]). We know that maxfreq is 4, and we know that index is zero. So, we look up our array table and find that freq[zero] is 2 (because the prior code had seen 2 a's.) This give us that (maxfreq - freq[index]) is 4 - 2, which is 2. Hence we can suspect that this new loop will run twice.What does it do each time it runs? Within it, it prints one space. Hence, it must be that the net effect of it running is that it "prints two blanks". The next thing in the original for-loop is a third for-loop > for (stars=0; stars< (freq[index]); stars++) > printf("*"); that we can see runs up to (freq[index]), which is 2. Its controllee is to print one "*", that means the net effect of running this third for-loop will be to "prints two stars". Thus in the output so far we have: __** The next thing is a "%c \n", ('A' + index). > printf("%c \n", ('A' + index) ); Well, index is still zero, so 'A' + zero is just 'A', so our output line will be: __**A__ Finally, we print the "\n" to leave that line and go to the next. The original loop has now completed one pass through itself, so it now does index++, which means index becomes One, and the original loop now goes through for a second pass (with index now being 1). This same process will have been repeated five times, before quitting completely. 8.2) We have done prime testing in class today (Prac Test Two, Q 6). You must work out a deep understanding of how prime-ness is tested. Understand how first you assume something is prime, and then have a set of potential challengers who are trying to disprove the assumption. If (any) one of them succeeds, the assumption is undermined/shattered; if none succeeds, the assumption must be true. The deep issue here is understanding how to use the logic of the prime-property (that there is no number in between 2 and N-1 that will evenly divide N) to set up a process (with a bunch of steps) that sets up a hypothesis and then will systematically provide the challengers. 8.3) Work on Required Assignment Two Part A. Finish the steps below by end of Jan 26, 2014 ============================================= 7.0) Do final preparations for the Test One on Jan 24. 7.1) Finish Practice Test 2, Question 1. For the input, type in: aaabbCCCdddBDD7 At this point, you must understand how line 7 (the while loop) does multiple steps, how line 9 (the ++ line) does multiple steps. You need to have mastery over those steps, try to re-write them yourself without the notes. Then ensure you are understanding how lines 5-6 together achieve a zeroing-out of the tally array; how lines 7-10 together are responsible for the tallying task; how lines 11-15 compute the maximum tally; how lines 17-24 print out the histogram. Finally, you need to start to grasp how the task of drawing the histogram is achieved by those subtasks (zeroing-out of the tally array; tally the input letters; compute the maximum tally; and lastly print out the histogram) in that specific order. The relationships between these three levels of description are at the heart of our work this semester. To summarize, we are understanding how the groups of statements achieve some small tasks; and then, how the small tasks achieve the overall goal. I will be repeating this theme of the three levels of description a lot for a few days. 7.2) The three levels of description from 7.1, for the histogram plotting code. Actual code | English single sentences English Terse Phrase =========== | ======================== ==================== | | Lines 5-6 | Zero out histogram array | | | Lines 7-10 | Put tally marks in histogram array | Tallies up the | | freq and plots hist Lines 11-15 | Compute Maximum Tally | | | Lines 17-24 | Use max tally to get plot of Hist | There are other terse programs descriptions that we will see soon. 7.3) Make sure you understand how a called function (like lower(c) ) is replaced by the value that it returns. Read "How to use Prewritten C functions" from Lectures Chapter 5. 7.4) Definitely make sure you have already done Practice Test Two, Question 2. 7.5) Look at the program arraymax.c Make sure you can follow the various steps involved with finding the max, and the array position that contains that max value. 7.6) Start Required Homework Two Problem A. Read it fully and start to implement it. Finish the steps below by end of Jan 21, 2014 ============================================== 6.1) Make sure you did Practice Test One's Question 4 (look at Daily HW 5.5). 6.2) You should try Practice Test One's Questions 5, 6 and 7. 6.3) Given that you should have finished Required Assignment Parts A and B by now, work on Part C, and finish it over the long weekend. 6.4) By Tuesday, you will receive Assignment Zero graded back to you; examine it and see if all is ready for your submission of Required Assignment One, Parts A, B and C. They need to be submitted by late Wednesday. 6.5) In Chapter 7, skip to the For Loop, then read Some Examples Using a For Loop. Download, run and understand forloop.c 6.6) Examine loopclearscreen1.c very well, understand, change to do a variety of things, including bouncing off the bottom of the screen, moving large chunks of text, etc. 6.7) From the Lectures page, download, run and understand stars.c. Remember to erase and re-write. 6.8) Then, from Update ??, download stars2.c, run and understand. 6.9) Read Chapters 7 and 8. 6.10) For Practice Test Two, start to work on Question 4. Then, work on Question 6. Finish the steps below by end of Jan 16, 2014 ============================================= 5.-1) Note that there is a new link to the SI sessions on the class website. 5.0) Remember to turn in Assignment Zero before tonight. 5.1) Do Practice Test One's Question 3. 5.2) Make very sure that you know how to write a program (from the very first line to the last line) that will simply read in a number and do something to it, like compute a percentage of it, or convert it to farenheit, or compute its squared value, or find its distance from the number 100, or whatever. 5.3) Make very sure that you now know how to write a program (from the very first line to the last line) that will read a quantity in and will do something involving an IF statement (maybe, also include an ELSE statement). 5.4) Now, the next thing to focus on is how to embed the idea from 5.2 or 5.3 within a FOR loop. So, for example, you could read in a number and square it (and print out the squared value), but the new task is to put this code within a for-loop that runs 25 times. This means that 25 numbers will be read in and their squared values will be printed out, one at a time. 5.5) Now, 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++) { 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.). 5.6) Now, try Practice Test One's Question 5, then Question 6, then 7. Finish the steps below by end of Jan 14, 2014 ============================================= 4.1) When studying chapter 4's sleep.c, ensure you are learning how to use the division and the mod (percent) operations correctly to get to parts of numbers. That is, this a quantitative skill you are learning, not just a way to do a particular programming sentence. The quantitative skill is that of how to get at the integer parts that make up a bigger number, and then how to get to the left-over remainder when the integer parts are taken away. You need to think of at least four other situations where this operation to separate out the parts, would come in handy. 4.2) Read Chapter 5's sections How to use Prewritten ..., How to use C Math ... 4.3) Download quadratic.c run it, and erase and re-create. 4.4) Download quadratic2.c, tempconvert.c, tempconvert2.c, priceisright.c, ltrgrade.c, wage.c, slope.c, scholarship.c. Run them, examine the programs carefully, try to re-create them one at a time from a blank canvas. 4.5) Completely Finish Part B of Required Homework Assignment 1. 4.6) Complete Question 1 from Practice Test 1. 4.7) Complete Question 2 from Practice Test 1. 4.8) Download the .pdf file forloop.pdf (update 6) and read it and understand it. 4.9) Download the program forloop.c (from Chapter 7) and run it, and study it carefully. 4.10) Read and start Part C of Required Homework Assignment 1. Next class we will do additional loops, so you can complete it then. Finish the steps below by end of Jan 12, 2014 ============================================= 3.1) Read Update 4, and finish submitting Assignment Zero (deadline Jan 15). 3.2) Finish Part A of Required Homework Assignment 1 (do not submit yet). 3.3) Download sphere.c, easytax.c, tall.c, sleep.c, digits.c and run all. 3.4) Try to re-create easytax.c, tall.c, sleep.c, and digits.c from a blank canvas. It is very important that you begin to learn how to manipulate data to get the kind of desired effect of digits.c 3.5) Think up and create new versions of the programs in 3.4 3.6) Think and create totally new math applications using skills in 3.5 3.7) Do (Update 3) Practice Test 1's question 1. 3.8) Go to www.cs.ucf.edu/~dmarino/ucf/transparency/cop3223/bookprob/ On this Practice Programs page, do some of the programs listed in C-Basics 3.9) Ensure that for Chapter 4's sections Use of Printf, and Formatting Output Spacing, ensure that you tried out a variety of formatting spacings. 3.10) Ensure that you understand Integer Division, i.e. why 13/4 evaluates to 3 3.11) Read Chapter 5's sections Conditional Expressions, if-else Construct 3.12) Do Practice Test 1's question 2. 3.13) Work on Part B of Required Homework Assignment 1. Finish the steps below by end of Jan 9, 2014 ============================================ 2.1) At this point you need to have been able to run at least three programs, and got the black screens working for them. You need to have understood what integers are, then you should have tried something with floats, doubles, and chars. Ensure that you know that for printing them, you know that you use %d, %f, %lf, and %c respectively. Ensure that you understand that in a program, first you write the six-ish lines that you always need, then you put a gap in to write the rest of the code, then in that gap you first declare your variables, and then you do something with them. But, you cannot use a variable on the right-hand-side of an EQUAL sign if you have not yet given it a value. (also, cannot use it in printf yet till you give it a value). See ExtraNotes-1 for an example of what needs a fix. 2.2) Read the Notes for Chapter 3, IO.doc. 2.3) Make sure you write programs that have statements such as a++; or a += 10; ensure you understand the meanings of these statements. 2.4) Download feettomile2.c, and learn about the use of scanf. For additional programs that you might have written for 1.5 (such as a sales tax program), modify them to read in most of the quantities, and get expertise using the scanf in a variety of situations. 2.5) Invent other simple mathematical calculations, such as, converting kilograms to pounds; write programs for these cases. 2.6) Download arithmetic.c and grade.c. Run both of them and study them carefully. Ensure you can explain why the outputs turn out to be what they are. 2.7) Read Part A of Required Homework Assignment 1. Start Part A. 2.8) Ensure that you understand the percent operator that is used to get the remainder of a calculation. 2.9) Read the Notes for Chapter 4. IF YOU WANT TO GET AHEAD, continue 2.10) Read Part B of Required Homework Assignment 1. You will not be able to complete it till you have read Notes for Chapter 5. Finish the steps below by end of Jan 7, 2014 ============================================ 1.1) Install the compiler DevCpp, or equivalent, on your computer (if you do not have a computer, contact me immediately). 1.2) Download from class website (the Lectures link) the programs: hello.c, feettomile.c Remember to add the line #include as the first line, and to add the line system("pause"); just before the return statement. (These two lines are only needed for DevC++, and are to be removed for CodeBlocks or for Linux systems. Make sure both programs run on your computer. When you run each one, it should produce output on the black screen. 1.3) After you have verified that you can run both programs in 1.2, erase each one, and try to write it yourself without looking at any notes. Get help from the notes to finish each program. Finally, keep repeating 1.3 until you are able to write each program by starting from an empty page and without any help from notes. 1.4) Read Lecture notes, Chapter 2 (the title is Introduction, First Program), skipping the sections called A Brief Intro to Pointers, and Another Method To Visualize Pointers. 1.5) Understand all the four types of variables in Chap 2. Try writing feettomile.c as floats. Make up additional calculations, such as computing the amount of tax from a six percent sales tax on a ten dollar purchase. Do these (new) programs both ways, first with ints and then with floats. 1.6) Look in "Notes" under "Language Basics Chapter 3"; download the "Notes", the file is titled IO.doc. Skip to the ending sections titled Increment and Decrement Operators, and Other Shorthand Assignments; read them carefully. Make sure you write programs that have statements such as a++; or a += 10; ensure you understand the meanings of these statements. IF YOU WANT TO GET AHEAD, continue 1.7) Download feettomile2.c, and learn about the use of scanf. For additional programs that you might have written for 1.5 (such as a sales tax program), modify them to read in most of the quantities, and get expertise using the scanf in a variety of situations.