|
#include <stdio.h> |
|
|
|
/* |
|
* Basics of a while loop |
|
*/ |
|
int main() { |
|
|
|
/* |
|
* Part 1 Instructions |
|
* |
|
* There is nothing to change here. Just read |
|
* the instructions, compile the program, and |
|
* observe the output for this section. Make |
|
* sure you understand how the while loop works |
|
* before proceeding. |
|
* |
|
* Loops are used to repeat a section of code. |
|
* The first type of loop we will use in this |
|
* class is the while loop. A complete while loop |
|
* has three parts. |
|
* 1. The keyword "while" |
|
* 2. A conditional expression (an expression |
|
* that is either true or false). |
|
* 3. The loop body. This section of code will |
|
* will repeat so long as the conditional is |
|
* true. The conditional is re-evaluated on |
|
* every execution (iteration) of the loop. |
|
*/ |
|
printf("Start part 1\n"); |
|
|
|
int number_1 = 0; |
|
|
|
/* To start the while loop, use the keyword "while" |
|
* followed by the conditional expression in |
|
* parenthesis. In order for the loop to execute at least |
|
* one time, the conditional has to be true the first |
|
* time it is evaluated. Note that to satisfy this |
|
* requirement the variable "number_1" is initialized to 0 |
|
* - since (0 < 10) is true when the program first reaches |
|
* the loop. |
|
*/ |
|
while (number_1 < 10) |
|
{ |
|
/* |
|
* The loop body is this section of code between the |
|
* braces. It will be executed if the conditional |
|
* is true. After the body is executed the conditional |
|
* is checked again and if it is still true, the body |
|
* is executed again. This continues until the |
|
* conditional is false. |
|
*/ |
|
|
|
printf("%d\n", number_1); |
|
|
|
/* |
|
* Note that since we increment the value by one each time, |
|
* the conditional will be different each time (1 < 10, |
|
* 2 < 10, 3 < 10, 4 < 10, etc). This will eventually be false. |
|
* In general, a loop will always need some kind of terminating |
|
* conditional like this. Otherwise, the loop will execute forever. |
|
*/ |
|
number_1 = number_1 + 1; |
|
} |
|
|
|
printf("End part 1\n\n\n"); |
|
|
|
|
|
|
|
|
|
/* |
|
* Part 2 Instructions |
|
* |
|
* Write a while loop that prints the first 10 numbers starting with 10 |
|
* counting back by 1 (10, 9, 8, etc). Print each number on its own line. |
|
*/ |
|
printf("Start part2\n"); |
|
|
|
int number_2 = 10; |
|
while (number_2 > 0) { |
|
printf("%d\n", number_2); |
|
number_2 = number_2 - 1; |
|
} |
|
|
|
printf("End part 2\n\n\n"); |
|
|
|
|
|
|
|
|
|
/* |
|
* Part 3 Instructions |
|
* |
|
* There is nothing to change here. This is another |
|
* example of a simple while loop. |
|
*/ |
|
printf("Start part3\n"); |
|
|
|
int number_3 = 0; |
|
int sum_3 = 0; |
|
|
|
while (number_3 < 10) |
|
{ |
|
sum_3 = sum_3 + number_3; |
|
number_3 = number_3 + 1; |
|
} |
|
|
|
printf("The sum of of the first 10 non negative integers is %d\n", sum_3); |
|
|
|
printf("End part 3\n\n\n"); |
|
|
|
|
|
|
|
|
|
/* |
|
* Part 4 Instructions |
|
* |
|
* Write a while loop that prints the sum of the first 10 even numbers. |
|
*/ |
|
printf("Start part4\n"); |
|
|
|
int number_4 = 0; |
|
int sum_4 = 0; |
|
|
|
while (number_4 < 20) |
|
{ |
|
if (number_4 % 2 == 0) { |
|
sum_4 = sum_4 + number_4; |
|
} |
|
number_4 = number_4 + 1; |
|
} |
|
|
|
printf("The sum of of the first 10 non negative even integers is %d\n", sum_4); |
|
|
|
printf("End part 4\n\n\n"); |
|
|
|
|
|
|
|
|
|
/* |
|
* Part 5 Instructions |
|
* |
|
* There is nothing to change here. This example demonstrates |
|
* local variables within a loop body |
|
* |
|
* Recall that a set of braces defines a scope - any variables |
|
* defined within a scope exist only in that scope. Furthermore |
|
* a loop body represents a different scope per execution. In |
|
* other words variables declared within the loop body are not |
|
* shared between iterations. |
|
*/ |
|
printf("Start part5\n"); |
|
|
|
int number_5 = 0; |
|
while (number_5 < 10) { |
|
/* |
|
* This declaration and initialization happens every time |
|
* the loop executes - i.e. oneLess represents a new variable |
|
* every time the loop body is executed. |
|
*/ |
|
int oneLess = number_5 - 1; |
|
printf("%d\n", oneLess); |
|
number_5 = number_5 + 1; |
|
|
|
/* |
|
* since oneLess is declared within the loop body, it is |
|
* discarded at the end of each of the loop body on each |
|
* iteration |
|
*/ |
|
} |
|
|
|
// oneLess doesn't exist outside the loop body |
|
|
|
printf("End part 5\n\n\n"); |
|
|
|
|
|
|
|
|
|
/* |
|
* Part 6 Instructions |
|
* |
|
* The code in this sections has one or more |
|
* bugs. Fix the code so that it correctly |
|
* calculates and prints the result of 5! |
|
*/ |
|
printf("Start part6\n"); |
|
|
|
int number_6 = 5; |
|
int factorial_6 = 1; |
|
|
|
while (number_6 > 0) { |
|
factorial_6 = factorial_6 * number_6; |
|
number_6 = number_6 - 1; |
|
} |
|
|
|
printf("5 factorial is %d\n", factorial_6); |
|
|
|
printf("End part 6\n\n\n"); |
|
|
|
|
|
|
|
|
|
/* |
|
* Part 7 Instructions |
|
* |
|
* When you get to this section delete the two |
|
* lines that say "DELETE THIS LINE" (make sure you |
|
* delete everything on the line). |
|
* |
|
* Compile and run the program but try to read and |
|
* understand what it is doing first. |
|
* |
|
* There is nothing else to change here. This example |
|
* demonstrates using a loop to get valid input from |
|
* a user. |
|
*/ |
|
printf("Start part 7\n"); |
|
|
|
// this variable both controls the loop and holds |
|
// input from the user. We set it to an invalid |
|
// value at first so that the loop will execute. |
|
int number_7 = 1; |
|
|
|
while (number_7 % 2 != 0) { |
|
// repeatedly ask for input until the user enters it |
|
// correctly |
|
printf("Type an even number and press enter: "); |
|
scanf("%d", &number_7); |
|
if (number_7 % 2 != 0) { |
|
// be sure you let the user know what is wrong |
|
// with the input. |
|
printf("%d is not even. ", number_7); |
|
} |
|
} |
|
printf("Congrats you got it right.\n"); |
|
|
|
printf("End part 7\n\n\n"); |
|
|
|
|
|
|
|
|
|
/* |
|
* Part 8 Instructions |
|
* |
|
* Write the code to get two numbers from the user - |
|
* a floating point numerator and floating point denominator. |
|
* Print the result of dividing the numerator by the |
|
* denominator. The code should not allow the user to enter |
|
* 0 for the denominator. |
|
* |
|
*/ |
|
printf("Start part8\n"); |
|
|
|
float numerator; |
|
float denominator = 0; |
|
|
|
printf("Enter the numerator: "); |
|
scanf("%f", &numerator); |
|
|
|
while (denominator == 0) { |
|
printf("Enter the denominator: "); |
|
scanf("%f", &denominator); |
|
if (denominator == 0) { |
|
printf("The denominator cannot be zero. "); |
|
} |
|
} |
|
|
|
printf("%f divided by %f is %f\n", numerator, denominator, numerator / denominator); |
|
|
|
printf("End part 8\n\n\n"); |
|
|
|
|
|
|
|
|
|
/* |
|
* Part 9 Instructions |
|
* |
|
* Loops can be nested inside other loops. After you have |
|
* reviewed and understand the code below, modify it so that |
|
* each row prints the number of starts equal to successive odd |
|
* numbers starting with 1 (the first row prints 1 star, |
|
* the second row prints 3 stars, the third row prints 5 stars, etc ) |
|
*/ |
|
// printf("Start part9\n"); |
|
|
|
int numberRows = 0; |
|
int starCount = 1; |
|
// the outer loops controls the number of rows |
|
while (numberRows < 10) { |
|
int numberStars = 0; |
|
/* |
|
* The inner loop controls the number of stars in a row. |
|
* this loop is executed once per execution of the outer |
|
* loop body. This means the inner loops body will run |
|
* a total of 10 x 20 times. Also note that since the |
|
* assignment of the numberStars variable happens at the |
|
* beginning of the outer loops body, it "resets" the |
|
* inner loop each time the outer loop body executes. |
|
*/ |
|
while (numberStars < starCount) { |
|
printf("*"); |
|
numberStars = numberStars + 1; |
|
} |
|
printf("\n"); |
|
numberRows = numberRows + 1; |
|
starCount = starCount + 2; |
|
} |
|
printf("End part 9\n\n"); |
|
|
|
|
|
|
|
return 0; |
|
} |