COP+1000+Daily+Notes+-+Part+2


 * Week 5 - 8**



In C for loops are very similar to while loops**.** The main difference is where the various pieces are put. In a for loop, there are actually three different parts of the setup: For example, to create a loop that counts from 0 to 10, you could create the following for loop: for(int loopCount = 0; loopCount <= 10; loopCount = loopCount + 1) Most important, just as with an if statement, be sure to not put a ; at the end of the for In addition to demonstrating looping, the Prime Number program presents several programming challenges in order to solve the basic problem and then another set of challenges to improve the program. For example, think about prime numbers, beginning with 2. How do you know a number is prime, or better, how do you know it is NOT prime. In fact, it is much easier to tell if a number, e.g., 117 isn't prime (hint, if you add the digits 1 + 1 + 7 = 9) you will know that it is divisible by not only 9, but therefore also 3. So, when you are testing a number, once you find a divisor that goes into the number evenly, you are done.
 * Initialization
 * Test condition
 * Increment



If you would like to update and improve the Prime Number program, consider the following:
 * Once you test for divisibility by 2, you don't need to test multiples of 2, e.g., 4, 6, 8, ...
 * You never need to test for divisibility by a non-prime number because a non-prime is always made up of other factors by definition, therefore, you only really need to check for divisibility with other primes
 * You don't need to use all the numbers up to one less than the number you are testing, only up to one-half the numbers. Anything greater than one half won't divide evenly anyway.


 * Variable Scope - for loop example**

code format="cpp" for(int ct=0; ct < 10; ct++) { } printf("ct = %d", ct); code in this example, the loop finishes executing when ct = 10. However, when you try to compile this code, ct will be out of scope and in fact, out of existence right before the print statement.

However, the following example shows how to keep the value of ct alive. code format="cpp" int ct; for(ct=0; ct < 10; ct++) { } printf("ct = %d", ct); code This example will compile and print 10 after the for loop finishes

A dangerous example: code format="cpp" int ct; for(int ct=0; ct < 10; ct++) { } printf("ct = %d", ct); code In this example, there are actually there actually TWO ct variables and the printf line will display zero since that ct value is out of the scope of the for loop.


 * Week 6**

This is a working change maker in C using WHILE loops. When I (RJ) first solved this problem, I used FOR loops. This is a great demonstration of how we can get stuck in a particular way of solving a problem. As the old saying goes, "Give a kid a hammer, and everything becomes a nail." We get out our current "hammer" and try to solve new problems the same way we have grown used to or is currently in our easy to get to toolkit. So, think for a moment, what other tool do you have that would provide the correct number of quarters, etc.? Please post your answer program below. Here is an example change maker that only provides the number of quarters. Can you see how to modify it to work correctly?


 * Week 7**

Note that when the "switch" statement is used, you need to use a "break" as the last statement in each case. If not, the code will continue to execute from the first case that is true and onward. Also, you can add a "default" case that catches any value not already handled in the above cases. The default case always is last.

If you leave out the "break" after each statement in each case the program will keep going into the next case. This can be advantageous at times such as the example below. code format="cpp" int x = 4; switch(x) //the switch now jumps to case 4 {    case 0: printf("You typed zero.\n"); break; case 4: //and executes until printf("x is an even number.\n"); case 1: //fall through case 9: //fall through printf("x is a perfect square.\n"); break; //the break; case 2: printf("x is an even number.\n"); case 3: case 5: case 7: printf("x is a prime number.\n"); break; case 6: case 8: printf("x is an even number.\n"); break; default: printf("x is not a single-digit number.\n"); break; } code

Does not work


 * Week 8**

This week we introduced functions and the concept of doing smaller, different tasks in functions of their own. **Most important:** When you pass, e.g., x to a function and x has a value of 5, you aren't really passing x, but you are passing the value of x, i.e., 5. In addition, you can also declare a variable x in your function, in either the header or the body. This is NOT the same variable x. Each of these two x variables has a different memory location and a different scope.



COP 1000 Daily Notes Weeks 9 - 12