Professional Documents
Culture Documents
Cluster 2 Worksheet
Cluster 2 Worksheet
GENERAL INSTRUCTIONS:
1. Copy each of the following exercises and save them with the provided filename formats (for organization). Familiarize
yourselves with what you are typing and try to experiment on the provided exercises.
2. You are not required to submit the source codes you will be creating in this worksheet. Store these files in you own
folders for your review.
3. Create a document named SURNAME_ws2.docx, which will contain one screenshot of the interface of each exercise
programs during execution (basically, the command prompt), and your answers to the debugging exercises. Upload
it in the corresponding submission bin in our course page.
/*
Save this file as SURNAME_ex2-1.c
#include<stdio.h>
#include<ctype.h>
int main() {
int grade;
return 0;
}
/*
Save this file as SURNAME_ex2-2.c
#include<stdio.h>
int main() {
int int1, int2, result, choice;
switch(choice) {
case 1:
result = int1 + int2;
break;
case 2:
result = int1 - int2;
break;
case 3:
result = int1 * int2;
break;
case 4:
result = int1 / int2;
break;
case 5:
puts("Bye!");
return 0;
break;
default:
puts("Invalid input. Bye!");
return 1;
break;
}
return 0;
}
Exercise 2-3: do-while and while statements and additional input validation
/* This exercise introduces using the do-while and while repetition structures.
As well as additional methods for input validation.
Save this file as SURNAME_ex2-3.c */
#include <stdio.h>
#include <stdlib.h>
int main() {
int input, tmp;
double factorial;
factorial = 1;
// scanf() actually returns the number of items in the argument list successfully matched.
inputstatus = scanf("%d", &input);
// Reject the input if its value is not a positive or if it was not properly matched by scanf()
if(!(input > 0) || (inputstatus == 0)) {
printf("\nError! Invalid input.\nPlease enter any positive integer: ");
input = 0;
}
} while(!(input > 0));
// Save the value of input to another variable so we can display the original value later on
tmp = input;
// Keep multipying factorial by tmp and subtract 1 from tmp each time until tmp = 0
// Note that factorial is initialized to have a value of 1 first
while(tmp > 0) {
factorial *= tmp--;
}
return 0;
}
/*
Save this file as SURNAME_ex2-4.c
#include <stdio.h>
#include <math.h>
int main() {
int input, i, prime;
prime = 1;
do {
int inputstatus;
fflush(stdin);
if(prime) {
printf("The number %d is prime!", input);
} else {
printf("The number %d is composite!", input);
}
return 0;
}
/*
Save this file as SURNAME_ex2-5.c
#include <stdio.h>
int main() {
int grade, students = 0;
double result = 0;
do {
printf("Enter grade of student %d or enter -1 to end input: ", students+1);
scanf("%d", &grade);
if(grade != -1) {
result += grade;
students++;
}
} while(grade != -1);
if(students > 0) {
printf("The average of %d students is %.2lf", students, result/students);
}
return 0;
}
/*
Save this file as SURNAME_ex2-6.c
#include<stdio.h>
int main() {
int input;
printf(“Enter an integer:”);
scanf(“%d”,&input);
do{
if(input<0 || input>10)
break;
if(input==4){
input++;
continue;
}
printf(“%d\n”,input);
input++;
} while(input<=10);
}
Debugging Exercises
b) The following code should print whether a given integer is odd or even:
switch (value) {
case (value % 2 == 0):
puts("Even integer");
case (value % 2 != 0):
puts("Odd integer");
}
a. Correct any errors that would prevent the program from compiling or running.
#include<stdio.h>
int main
{
int num1, num2;
int found = 0;
puts(“”);
found = num1>num2;
if (found
switch (num1 % num2);
{
case 0:
num2 = num1 / 2;
break;
case 1:
num1 = num2 / 2;
break;
default:
num1 = num1 / num2;
num2 = num1 * num2;
};
else
{
num1 = num1 - num2;
num2 = (num1 + num2) / 10;
}
return;
}
b. After correcting the code, what is the output if the input is:
i. 16 and 5?
ii. 13 and 27?
a) The following code should solve for the product of all integers from 1 to 10.
int x = 1, product = 0;
while ( x <= 10 ); {
product *= x;
++x;
}
4. The following program is designed to input two numbers and output their sum. It asks the user if he/she
would like to run the program. If the answer is Y or y, it prompts the user to enter two numbers. After
adding the numbers and displaying the results, it again asks the user if he/she would like to add more
numbers. However, the program fails to do so. Correct the program so that it works properly.
#include<stdio.h>
int main ()
{
char response;
double num1, num2;
return 0;
}
5. The do…while loop in the following program is supposed to read some numbers until it reaches a
sentinel (in this case, -1). It is supposed to add all the numbers except for the sentinel. If the data looks
like:
12 5 30 48 -1
the program does not add the numbers correctly. Correct the program so that it adds the numbers
correctly.
#include<stdio.h>
int main()
{
int total = 0, count = 0, number;
do
{
scanf(“%d”, &number);
total = total + number;
count++;
return 0;
}
BONUS KNOWLEDGE
Conditional Operator ( ? : )
▪ A more concise way of writing if…else statements.
▪ C’s only ternary operator – takes three operands. These together with the conditional operator form a
conditional expression.
SYNTAX
expression1 ? expression2 : expression3
▪ If expression1 evaluates to true or a nonzero integer, the result of the conditional expression is
expression2. Otherwise, the result is expression3.
Examples:
▪ First Refinement – divide the top into series of smaller tasks and list these in the order in which they
need to be performed.
▪ This refinement process is stopped when the pseudocode algorithm is specified in sufficient detail for
you to be able to convert into a source code.