In this lab, you will learn to use conditional statements, loops, and solve simple problems in C. You will submit
your finished Lab on Canvas. We will provide a link and make an announcement when submission opens.
Download and unzip lab1.zip. Create an empty solution in Visual Studio and add the file lab1.c to it. Write your
code in the designated spaces ONLY, any code outside of these areas will be disregarded during grading. You
should be able to build and run the solution even if you haven’t added code for all the questions. Submit your
final solution to Canvas.
1. Conditional statements
Simplest form. of a conditional statement in C looks like this.
if (statement_1) {
// set of statements
}
If the statement_1 is evaluated to be TRUE, set of statements will be executed. Additionally it can be paired
with an else statement.
if (statement_1) {
// set of statements
} else {
// set of statements
}
In the same manner, If the statement_1 is evaluated to be TRUE, the first set of statements before else will be
executed. Otherwise, the second set of statements after else will be executed. If there are only one statement
in each set, you can skip enclosing them in {}. Conditional statements can be nested. For example, this code
snippet uses a conditional statement to find the maximum value among three numbers (a, b and c are
variables).
int max = a;
if (b > c)
if (b > a)
max = b;
else
if (c > a )
max = c;
Also conditional statements can include multiple expressions combined using different operators such as
(logical and) and || (logical or). This is another way to calculate maximum of three numbers.
int max = a;
if (b > a || c > a)
if (b > c)
max = b;
else
max = c;
Apply conditional statement to solve the problem below.
Problem 1 – Check parity (10 points):
Ask the user to enter an integer number. If that number is an odd number, print “Odd” on the screen. If that
number is an even number, print “Even” on the screen. Don’t forget that zero is even too.
Hint: Check the code in section 2.2 on how to use the functions scanf_s() and printf() to ask a user for input
and printing to the screen, respectively. You can use operator “%” (modulo) to check for remainder when
dividing an integer by another integer.
2. Loops
Loop is an iterative structure in C which allows you to repeatedly execute some code.
for, while and do .. while are useful iterative structures.
2.1. For loops
The structure of a for loop is as follows
for (initialization; condition; update) {
// Set of statements
}
This loop starts by initializing a variable such as counter. It will then execute the statements inside the body of
the loop for a number of iterations as long as condition holds. At the end of each iteration, variables are
updated according to update statement. For example, the code snippet below uses a for loop to compute sum
of integer numbers from 1 to 10.
int sum = 0;
int i;
for (i = 1; i
int main()
{
int num1;
int num2;
printf(" Enter the first number :");
scanf_s("%d", num1);
printf(" Enter the second number :");
scanf_s("%d", num2);
while (num1 != num2) {
if (num1 > num2)
num1 = num1 - num2;
else
num2 = num2 - num1;
}
printf(" The GCD is: %d \n", num1);
return 0;
}
Enter the first number: 49
Enter the second number: 28
The number of loop
The GCD of 49 and 28 is 7
Problem 4 - While (20 points):
Write a program that reads arbitrary number of letter grades (A, B, C, D, F) from input and counts the number
of each different letter grade students have earned. End of input is specified by character ‘Z’. You shouldn't
assume correct input for this problem and it shouldn't matter. If you use the right conditional statements typing
any other letters besides A-F and Z will just have no effect. Also, lower case letters should be taken into
account.
Note: Since you are reading characters, the type of the variable you need to use for reading a
letter grade should be char and you need to use %c in scanf_s.
char grade;
scanf_s(" %c", grade);
Adding the space to the format (%c) enables scanf_s to consume the newline character from the input that
happens every time you press return. A sample output for your program would look like this:
Total Number of Scores Entered: 12
A: 5
B: 3
C: 3
D: 1
F: 0
Problem 5. Replacing for construct with while loop (10 points)
Consider the following c code. Replace the for body with equivalent while loop without changing the
functionality.
#include
int main()
{
int thresh;
int max = 500;
printf(" Enter threshold value (between 0 and 500) :");
scanf_s("%d", thresh);
int i, j;
int sum = 0;
for (i =0, j = max; i thresh; ++i, j -= 2) {
sum += (i+j);
}
printf(" Sum equals to: %d \n", sum);
return 0;
}
Problem 6. Swapping numbers with pointers (20 points)
Write a program that swaps the values of two integers using pointers. Your code contains 2 integer pointers,
p1 and p2, that point to integers a and b, respectively. Fill in the function p6_swap(int* p1, int* p2) that swaps
the values of the two variables. For example, if a = 5 and b = 4, after the function call we expect a = 4 and b=
5.