Exercise Solutions
Concepts Questions:
1. 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:
forloop because we have a known iteration count (10 students) - Reading user input until they enter a valid number between 1 and 100:
whileloop because we do not know how many times it will take before the user enters a valid number - Ensuring a user enters a valid menu choice (1-4) without any error message:
do-whileloop because this loops must execute once to get valid input and there are no error messages so the second iteration does not look different from the first - Ensuring a user enters a valid menu choice (1-4) displaying an error message if the user enters the wrong data:
whileloop because we need to display an error message. We would require additional logic inside the loop body if we had used a do-while - Print the numbers 1 through n on the screen with a newline every 10 numbers:
forloop because we know that we need to run this loop n times. How we got the value for n is not something we need to worry about for this loop. We just need to make sure the loop runs that many times.
2. What will cause this loop to stop?
int number = 1;
while (number <= 100 && number % 7 != 0){
number = number + 2;
}
The loop terminates when number exceeds 100 OR when number is divisible by 7. Since number starts at 1 and increments by 2 each iteration (1, 3, 5, 7...), it will stop when number equals 7.
3. What will cause this loop to stop?
int count;
for (int count = 0; count < 5; count++) {
printf("hello\n");
}
The loop terminates after exactly 5 iterations when count reaches 5.
starts at 1 and increments by 2 each iteration (1, 3, 5, 7...), it will stop when number equals 7
4. What will cause this loop to stop?
int total = 0;
while (total < 50){
total = total + 5;
}
The loop terminates when total reaches or exceeds 50. Since total starts at 0 and increments by 5 each iteration, it will stop when total equals 50.
5. Rewrite the following for loop using while loop
int i;
for(i = 0;i<5;i++){
...
}
using while loop:
int i = 0;
while (i < 5){
...
i++;
}
6. Rewrite the following while loop using for loop
int count = 5;
while(count < 10){
count +=2;
}
using for loop:
int count;
for (count = 5; count < 10; count += 2) {
// loop body would go here if there was one
}
Walkthrough:
Exact output
batch: 5, total: 5
batch: 0, total: 5
batch: 4, total: 9
Debugging:
1. Errors identified:
- Syntax errors:
- Missing
intdeclaration for variable A - Missing semicolon after
printfstatement - Incorrect
scanfformat string (should be%d, not descriptive text)
- Missing
- Logical errors:
- Function counts every number entered:
- negative numbers should not be counted
- Function counts every number entered:
- Stylistic flaws:
- Variable names A and B are:
- not descriptive
- not lowerCamelCase
- single characters
- Poor indentation and spacing
- put open curly of function on next line
- no code should immediately follow an open curly
- Inconsistent spacing and indentation
- closing curly of while loop needs to be on its own line
- Variable names A and B are:
2. Corrected function:
int countPositive()
{
int count = 0;
int num = 1;
while (num != 0) {
printf("Enter a number (0 to stop): ");
scanf("%d", &num);
if (num > 0) {
count++;
}
}
return count;
}
Programming:
Problem 1: Draw Triangle
void drawTriangle(int size)
{
int row;
int col
for(row = 0; row < size; row++){
for(col = 0; col < row; col++){
printf("*");
}
printf("\n");
}
}
Problem 2: Draw Hollow Square
void drawHollowSquare(int size)
{
int row;
int col
for(row = 0; row < size; row++){
for(col = 0; col < size; col++){
if (row == 0 || row == size || col == 0 || col == size) {
printf("*");
}
else{
printf(" ");
}
}
printf("\n");
}
}
Problem 3: Find Largest Digit
int findLargestDigit(int number)
{
int largest = 0;
int digit;
while(number > 0){
digit = number % 10;
if(digit > largest){
largest = digit;
}
number = number / 10;
}
return largest;
}