1 decision making if (score
TRANSCRIPT
![Page 1: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/1.jpg)
1
Decision Makingif (score<40) {
printf(“Fail\n”);
} else {
printf(“Pass\n”);
}
Not recommended in practice since it does not follow general fashion of programming language; other language does not offer this shorthand.
Also because only ONE statement is allowed for each condition (not much use practically)
Might be useful in understanding other people’s coding
/* shorthand version of if else statement */
(score<40) ? printf (“Fail\n”) : printf(“Pass\n”);
![Page 2: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/2.jpg)
2
….printf(“Game Menu\n”);
printf(“1 – Star Craft 2 – Red Alert 3 – War Craft\n”);
printf(“Enter your selection:”);
scanf(“%d”,&choice);
if (choice==1) {
/* Lauch Star Craft */
} else {
if (choice==2) {
/* Lauch Red Alert */
} else {
/* Lauch War Craft */
}
}
switch…case Multiple Selection StructureConsider creating a selection menu
What if there are 10 options or more? The if…else statement would have to be nested for 10 levels or more. It doesn’t seems like a neat solution, does it?
![Page 3: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/3.jpg)
3
….printf(“Enter your selection:”);
scanf(“%d”,&choice);
switch(choice) {
case 1:
/* Launch Star Craft */
break;
case 2:
/* Launch Red Alert */
break;
case 3:
/* Launch War Craft */
break;
}
switch…case Multiple Selection StructureLet’s see how switch..case statement can help...
Isn’t it looks much better?
![Page 4: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/4.jpg)
4
switch…case Multiple Selection Structure
• Flowchart of the switch structure
true
false
.
.
.
case a case a action(s) break
case b case b action(s) break
false
false
case z case z action(s) break
true
true
default action(s)
![Page 5: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/5.jpg)
5
….int choice = 0;
switch(choice) {
case 1:
printf(“Launching Star Craft\n”);
break;
case 2:
printf(“Launching Red Alert\n”);
break;
case 3:
printf(“Launching War Craft\n”);
break;
default:
printf(“Invalid Options!\n”);
}
switch…case Multiple Selection StructureGuess what’s the output of this program….
Output
Invalid Options!
![Page 6: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/6.jpg)
6
switch…case Multiple Selection StructureExercise
1. Create a program that read in an integer from user. The value range from 0 to 9. Next, print out the value in Bahasa Malaysia. Example, if user input 7, the output is ‘Tujuh’. If user input any value not within the range, display message: ‘Jangan nakal-nakal…nanti Sir rotan!’
![Page 7: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/7.jpg)
7
//Exercise Answer#include <stdio.h>
main(){
int number;
printf("Please enter a number: ");scanf ("%d", &number);
switch(number) {case 0:
printf("Sifar");break;
case 1:printf("Satu");break;
case 2:printf("Dua");break;
case 3:printf("Tiga");break;
case 4:printf("Empat");break;
case 5:printf("Lima");break;
case 6:printf("Enam");break;
case 7:printf("Tujuh");break;
case 8:printf("Lapan");break;
case 9:printf("Sembilan");break;
default:printf("Jangan nakal nakal, nanti sir rotan!");
}}
![Page 8: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/8.jpg)
8
….
int counter;
counter = 0;
while (counter<5) {
printf(“%d, “,counter);
counter = counter + 1;
}
While Repetition/Loop StructureGuess what’s the output of this program….
Output
0, 1, 2, 3, 4
![Page 9: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/9.jpg)
9
While Repetition/Loop Structure
while (loop continuation test) {
statement 1
statement 2
…...
}
//statement to executed after exiting the while loop
If the loop continuation test is TRUE, the statements within the braces { } will be executed. When it reach the point right before ‘}’ , it will return to the beginning of the loop and repeat the process.
if FALSE, exit the loop (go to first statement after ‘}’)
true
Loop continuation
test
Statement 1Statement 2
...
false
WHILE LOOP
![Page 10: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/10.jpg)
10
….
int counter = 1, fact=1, n=4;
while (counter<=n) {
fact = fact * counter;
counter = counter + 1;
}
printf(“ %d factorial is %d\n”, n, fact);
While Repetition/Loop StructureExercises – What’s the output?
Output
4 factorial is 24
![Page 11: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/11.jpg)
11
….
int counter = 0, row=3, col;
while (counter<row) {
col = 0;
while (col<(counter+1)) {
printf(“*”);
col = col + 1;
}
printf(“\n”);
counter = counter + 1;
}
While Repetition/Loop StructureExercises – What’s the output?
Output
*
**
***
![Page 12: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/12.jpg)
12
….
int counter=0, row=3, counter2, col=4;
while (counter<row) {
counter2 = 0;
while (counter2<col) {
printf(“*”);
counter2 = counter2 + 1;
}
printf(“\n”);
counter = counter + 1;
}
While Repetition/Loop StructureExercises – What’s the output?
Output
****
****
****
![Page 13: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/13.jpg)
13
….int counter;
counter = 0; //counter initialisation
while (counter<5) { //repetition condition checking
printf(“%d, “,counter);
counter = counter + 1; //counter increment
}
While Repetition/Loop StructureThe earlier examples are typical Counter-controlled repetition structure.
Counter-controlled repetition is a definite repetition: number of repetition is known before loop start.
while statement is seldom used for this type of repetition; for statement is more popular. (Discuss later)
while statement is catered for another type of repetition….
![Page 14: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/14.jpg)
14
….
int score=0;
while (score >= 0) {
printf(“Enter a score [enter negative score to exit]: “);
scanf(“%d”,&score);
}
While Repetition/Loop StructureCan you tell how many loops there will be?
This is a Sentinel-controlled repetition: number of repetition is UNKNOWN.
The sentinel value (in this case, a negative score) shall indicate the termination of the loop
![Page 15: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/15.jpg)
15
….
int number, total_digit=0, divider=1;
printf(“Please enter a decimal number”);
scanf(“%d”, &number);
while ((number/divider) != 0) {
total_digit=total_digit + 1;
divider = divider * 10;
}
printf(“There are %d digit in %d\n”,total_digit, number);
While Repetition/Loop StructureLet’s use it to count the number of digits that form the input integer
![Page 16: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/16.jpg)
16
Ali took a RM100,000 house loan from bank. The interest rate is 8% per annum, compound monthly. The monthly instalment is RM 700. Write a program to find out how long (in years & months) Ali need to fully settle his loan.
While Repetition/Loop StructureAnother example
Can u tell what’s the sentinel value in the above while loop?
![Page 17: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/17.jpg)
17
float balance, monthly_interest, monthly_interest_rate, monthly_instalment;
int months = 0;
balance = 100000;
monthly_interest_rate = (8.0/100.0)/12.0;
while (balance>0.0) {
monthly_interest = monthly_interest_rate * balance;
balance = balance + monthly_interest - monthly_instalment;
months = months + 1;
}printf (“%d years %d months\n”, months/12, months%12);
While Repetition/Loop Structure
![Page 18: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/18.jpg)
18
While Repetition/Loop StructureExercises
1. Write a program that reads a positive integer n and then computes and prints the sum of all integers from 1 to n and divisible by 3
2. Write a program that interactively reads test score values until a negative test score is entered and then computes and outputs the average test score. The test score must be validated to ensure that they are between 0 and 100.
3. Write a program that accepts a positive integer and determines whether it is a numerical palindrome (a number that is the same forward and backward, such 13431) [BONUS]
![Page 19: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/19.jpg)
19
….
int counter;
counter = 1;
do {
printf(“%d, “,counter);
counter = counter + 1;
} while (counter<4);
do...while Repetition/Loop StructureGuess what’s the output of this program….
Output
1, 2, 3,
![Page 20: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/20.jpg)
20
do...while Repetition/Loop Structure
do {
statement 1
statement 2
…...
} (loop continuation test);
//statement to executed after exiting the while loop
Execute the statements within the braces { }. When it reach the end of the segment, it will perform the loop continuation test. If TRUE, it will return to the beginning of the loop and repeat the process.
if FALSE, exit the loop (go to first statement after ‘}’)
trueLoop continuation
test
Statement 1Statement 2
...
false
DO..WHILE LOOP
![Page 21: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/21.jpg)
21
….
int counter;
counter = 3;
do {
printf(“%d, “,counter);
counter = counter + 1;
} while (counter<1);
do...while Repetition/Loop StructureExercise: What’s the output?
Output
3
![Page 22: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/22.jpg)
22
….
int counter;
counter = 3;
do
printf(“%d, “,counter);
counter = counter + 1;
while (counter<1)
do...while Repetition/Loop StructureExercise: Any syntax error?
1. The braces {}
2. The semicolon ; {
} ;
![Page 23: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/23.jpg)
23
counter = 3;
do
counter = counter + 1
while (counter<1);
do...while Repetition/Loop StructureConsider this example...
When there is only one statement, it can be written as
do statement while (expression);
Note that no semicolon is required at the end of statement.
![Page 24: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/24.jpg)
24
int score;
printf (“Enter a negative score to exit\n“);
do {
printf (“Enter a score: “);
scanf(“%d”, &score);
} while (score>=0);
do...while Repetition/Loop StructureAny difference in the output of the programs?
int score;
printf (“Enter a negative score to exit\n“);
while (score>=0) {
printf (“Enter a score: “);
scanf(“%d”, &score);
}
do…while will go into the loop at least once
while might not go into the loop at all.
![Page 25: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/25.jpg)
25
int i;
for (i=0; i<5; i=i+1) { printf(“%d, “,i);
}
for Repetition/Loop StructureGuess what’s the output of this program….
Output
1, 2, 3, 4,
int i;
i = 0;
while (i<5) {
printf(“%d, “,i);
i = i + 1;
}
Exactly the same as this program except on the way of writing it only
![Page 26: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/26.jpg)
26
for Repetition/Loop Structurefor (initializations; loop continuation test; counter modifications) {
block of statements
}
//statement to executed after exiting the for loop
1. Perform the initializations (if any)
2. Perform loop continuation test. (if any)
3. If FALSE, exit the loop (go to first statement after ‘}’).
4. If TRUE, the statements within the braces { } will be executed.
5. When it reach the point right before ‘}’ , it will return to the beginning of the loop, perform counter modification (if any)
6. Repeat step 2.
![Page 27: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/27.jpg)
27
int i;
for (i=4; i>0; i=i-1) {
printf(“%d, ”, i);
}
for Repetition/Loop StructureExercise: What’s the output?
Output
4, 3, 2, 1
int row, col;
for (row=0; row>2; row=row+1) {
for (col=0; col>3; col=col+1) {
printf(“*”);
}
printf(“\n”);
}
Output
*
**
***
Nested for structure
![Page 28: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/28.jpg)
28
for Repetition/Loop StructureExercise: Any syntax errors?
int i;
for (i=4 i>0 i=i-1;) {
printf(“%d, ”, i);
}
1. Missing semicolon ;
2. No semicolon at the end of counter modification
Anything wrong with this program logically?
int i;
for (i=4 i>0 i=i+1;) {
printf(“%d, ”, i);
}
• Infinite loop (System Hang)• This is called Run-time error.• The program will still run except that the result is something undesired
Hence, You Must Learn How To Debug Your Program!
![Page 29: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/29.jpg)
29
Exercises
1. Ah Beng save RM500 in his bank account every year and the bank pay him interest of 4% per annum on daily compound basis. Write a program that will calculate the balance of his account at a specific period of saving in year basis. Use for loop to solve this problem.
2. Write a program that can take in a series of number of any length specified by user and find out the smallest and the largest number in the series. Use for loop to solve this problem.
![Page 30: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/30.jpg)
30
Exercises
3. Write a program that prints the following diamond shape. You may use printf statements that print either a single asterisk (*) or a single space. Maximize you use of repetition (with nested for structures) and minimize the number of printf statement.
*
***
*****
***
*
![Page 31: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/31.jpg)
31
Exercises Answers
//No. 1#include <stdio.h>
main(){
int number_of_year, counter_year, counter_day;double daily_interest, balance;
printf("Enter the number of years: ");scanf("%d", &number_of_year);balance = 0;counter_year = 0;for (counter_year=0; counter_year<number_of_year; counter_year++) {
balance = balance + 500;
for (counter_day = 0; counter_day<365; counter_day++) {daily_interest = balance * (0.04/365);balance = balance + daily_interest;
}}printf("The balance after %d years is $%.2lf\n", number_of_year, balance);
}
![Page 32: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/32.jpg)
32
Exercises Answers
//No. 2#include <stdio.h>
main(){
int series_length, number, min, max, counter;
printf("Enter the length of the series: ");scanf("%d", &series_length);
counter = 1;printf("Enter number no.%d: ", counter);scanf("%d", &number);max = number;min = number;
for (counter=2; counter<=series_length; counter++) {printf("Enter number no.%d: ", counter);
scanf("%d", &number);
if (min>number) {min = number;
}
if (max<number) {max = number;
}}
printf("The minimum is %d and the maximum is %d\n", min, max);
}
![Page 33: 1 Decision Making if (score](https://reader036.vdocuments.mx/reader036/viewer/2022062518/5697bf7a1a28abf838c83136/html5/thumbnails/33.jpg)
33
Exercises Answers
//No. 3include <stdio.h>
main(){
int cnt_space, cnt_ast, cnt_row;
for (cnt_row=1; cnt_row<=3; cnt_row++) {for (cnt_space=1; cnt_space<=3-cnt_row; cnt_space++) {
printf(" ");}for (cnt_ast=1; cnt_ast<=cnt_row; cnt_ast++) {
printf("*");}for (cnt_ast=cnt_row-1; cnt_ast>=1; cnt_ast--) {
printf("*");}printf("\n");
}for (cnt_row=2; cnt_row>=1; cnt_row--) {
for (cnt_space=1; cnt_space<=3-cnt_row; cnt_space++) {printf(" ");
}for (cnt_ast=1; cnt_ast<=cnt_row; cnt_ast++) {
printf("*");}for (cnt_ast=cnt_row-1; cnt_ast>=1; cnt_ast--) {
printf("*");}printf("\n");
}}