CPT109 C programming and SW engineering 1 Lab Practice 3b – Operations, Branching and Looping
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
CPT109 C programming and SW engineering 1
Lab Practice 3b - Operations, Branching and Looping
Use your preferred compiler to investigate the programming exercises below. This laboratory concerns the use of relational and logical operators together with branching flow control statements if/else and switch and looping.
Logical Operators
Here is a table of the common logical operators you have learned about in your lectures.
Value of exp1 |
Value of exp2 |
exp1&&exp2 (AND) |
exp1||exp2 (OR) |
!exp1 (NOT) |
0 |
0 |
0 |
0 |
1 |
0 |
non-zero |
0 |
1 |
1 |
non-zero |
0 |
0 |
1 |
0 |
non-zero |
non-zero |
1 |
1 |
0 |
Exercise 1 |
Give equivalent expressions for the following logical expressions without negation
!(a>b) |
!(a<=b&&c<=d) |
!(a+1==b+1) |
!(a<1||b<2&&c<3) |
|
|
|
|
Example
!(a<5 || a>10) is equivalent to a>=5 && a<=10
Hint: Apply operator precedence rules given during lectures.
Exercise 2 |
For the following declarations complete the table by correctly parenthesizing the expression and then finding the value:
int a=1, b=2,c=3;
double x=1.0
Expression |
Parenthesize ( ) Expression |
Value |
a>b && c<b |
|
|
a<!b || !!a |
|
|
a+b<!c+c |
|
|
a-x || b*c && b/a |
|
|
Write a C program to print the values on the screen to see if your answers are correct.
Exercise 3 |
What gets printed by the following program?
char c=’A’;
int i=5, j=10;
printf(“%d\t%d\t%d\n”,!c, !!c, !!!c);
printf(“%d\t%d\t%d\n”,-!i, !-i, !-i-!j);
printf(“%d\t%d\t%d\n”,!(6*j+i-c), !i-5, !j- 10);
Note: The ASCII code for ‘A’ is 65. Be careful about following the precedence rules!
Exercise 4 |
In each case below, construct a logic expression to express the following conditions and then write a program to test your expression:
. Determine if an int variable number is equal to or greater than 1 but smaller than 9.
. Determine is a char variable ch is not the value q or k.
. Determine if an int variable number is between 1 and 9 but is not 5.
. Determine if an int variable number is not between 1 and 9
Example:
number is 5 or 10
Solution
number ==5 || number ==10
The if, if-else and switch Statements
Exercise 5 |
What gets printed by the following programs? Do it on paper first and then use the computer.
Program 1
int i=7, j=2;
if(i==1)
if(j==2)
printf(“%d\n”,i=i+j);
printf(“%d\n”,i=i-j);
printf(“%d\n”,i);
Program 2
What gets printed when n=101 or n=10
#define LIMIT 100
main(){
intn;
printf (“Enter an integer: ”);
scanf(“%d”, &n);
switch (n){
case LIMIT- 1 : printf (“slightly less”); break;
case LIMIT : printf (“exact”); break;
case LIMIT+1 : printf (“slightly high”); break;
default : printf (“too far”); break;
}
}
Exercise 6 |
The mathematical operation min(x,y) can be represented by the conditional expression: z=(x<y) ? x : y;
Or
if(x<y)
z=x;
else
z=y;
Write your own expression (in either format) to define the mathematical operation min(x,y,z).
Exercise 7 |
Write a program to take the depth (in kilometers) inside the earth as input data. The program should use this information to compute and display the temperature at this depth in degrees Celsius and degrees Fahrenheit. The relevant formulae are:
Celsius temperature at depth in km formula:
Celsius = 10 x depth + 20
Celsius to Fahrenheit conversion formula:
Fahrenheit = 1.8 x Celsius + 32
Exercise 8 |
Write a program that computes the square root of a given real number. The program should let its user know that negative numbers are not accepted. Its execution should look like the following:
Give anon negative number: 2
Its square root is 1.4142214
Give anon negative number: - 1
This is a negative number; please provide anon negative one!
Hint: Include the math.h header file and use the function sqrt() to calculate the square root.
Looping
The following exercises concern the use of loops: while, do while, for; together with relational and logical operators and branching flow control statements if/else or switch.
Exercise 9 |
Write two programs, first using a while loop and second using a for loop to print the numbers from 1 to 10 and their squares (See the example output below):
1 1
2 4
3 9
...
10 100
Exercise 10 |
Write a program using two nested for loops (see your lecture 4 notes) to print the following
triangle
*
**
***
****
*****
Note: don’t use multiple printf statements or one long printf statement, try to achieve it with loops.
Exercise 11 |
Write a program to print the numbers between 1 and 10, along with an indication of whether the number is even or odd (see below):
1 is odd
2 is even
3 is odd
Hint: Use an if/else statement, which is controlled by determining if the number divided by 2 has a reminder (remember the % operator x%y = the remainder of x/y i.e. if x was 6 and y 4 then 6%4 = 2, the remainder of the division).
Exercise 12 |
Write a program to print the first 7 positive integers and their factorials. Compute the factorials inside a loop. Your program’s output should look like:
The factorial of 1 is 1
The factorial of 2 is 1 * 2 = 2
The factorial of 3 is 1 * 2 * 3 = 6
The factorial of 4 is 1 * 2 * 3 * 4 = 24 etc …
Exercise 13 |
Write a program that takes as an input a positive integer n and then computes the following sum:
S = 1 + 1/2 + 1/3 + 1/4 + …+ 1/n
2023-12-22