Self Check
Concepts Questions:
-
For each scenario below, explain which type of loop (for, while or do-while) would be most appropriate and why:
- Calculating the grade for exactly 10 students
- Reading user input until they enter a valid number between 1 and 100
- Ensuring a user enters a valid menu choice (1-4) without any error message
- Ensuring a user enters a valid menu choice (1-4) displaying an error message if the user enters the wrong data
- Print the numbers 1 through n on the screen with a newline every 10 numbers
-
What will cause this loop to stop?
int number = 1;
while (number <= 100 && number % 7 != 0) {
number = number + 2;
} -
What will cause this loop to stop?
int count;
for (int count = 0; count < 5; count++) {
printf("hello\n");
} -
What will cause this loop to stop?
int total = 0;
while (total < 50) {
total = total + 5;
} -
Rewrite the following for loop using while loops
int i;
for(i = 0;i<5;i++){
...
} -
Rewrite the following while loop using for loops
int count = 5;
while(count < 10){
count +=2;
}
Walkthrough:
a) Using tables to track your variables and function calls show how you would go through the execution of this program. b) What is the exact output of the following program?
#include <stdio.h>
int applesauce(int sugar);
int main(void)
{
int first = 97531;
int second = 24680;
int third = 3091623;
int total = 0;
int i;
int batch;
for (i = 0 ;i< 3;i++){
if(i == 0){
batch = applesauce(first);
}
else if (i == 1){
batch = applesauce(second);
}
else{
batch = applesauce(third);
}
total += batch;
printf("batch: %d, total: %d\n",batch,total);
}
return 0;
}
int applesauce(int sugar)
{
int apples = 0;
while (sugar > 0) {
if (sugar % 2 == 1) {
apples++;
}
sugar = sugar / 10;
}
return apples;
}
Debugging:
The following function is supposed to count how many positive numbers are entered by the user. The function should keep reading numbers until the user enters 0, then return the count of positive numbers (not including the 0).
int countPositive(){A = 0;
int B = 1;
while (B != 0){
printf("Enter B number (0 to stop): ")
scanf("the number is %d", &B);
A++;}
return A;
}
- In the above code identify the following:
- all the syntax errors
- all logical errors
- all stylistic flaws that do not adhere to the style guide
- Making as few changes as possible to address all of the errors above, rewrite the function so that it functions according to spec
Programming:
Problem 1:
Write the following function:
void drawTriangle(int size);
This function draws triangle using the * chacter. The triangle has a right angle at the bottom left.
For example, drawTriangle(5) prints:
* ** *** **** *****
Problem 2:
Write the following function:
void drawHollowSquare(int size);
This function draws a hollow square using the '*' character as the border. The function accepts the size of the square as a parameter and prints the square to the screen.
For example, drawHollowSquare(5) prints:
***** * * * * * * *****
Problem 3:
Write the following function:
int findLargestDigit(int number);
This function finds and returns the largest digit in a positive integer. The function accepts a positive integer as a parameter and returns the largest digit found in that number.
For example, findLargestDigit(3947) returns 9, and findLargestDigit(12) returns 2.