You are on page 1of 24

CLO 2P

DEPARTMENT OF ELECTRICAL ENGINEERING


DEC20012 – PROGRAMMING FUNDAMENTALS (PRACTICAL REPORT)

19/11/2021

Muhammad Amir Farhan Bin Mohd Dom 17DTK20F2030

1 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

2 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

POLITEKNIK SULTAN IDRIS SHAH


JABATAN KEJURUTERAAN ELEKTRIK
DEC20012 - PROGRAMMING FUNDAMENTALS
Title Selection Structure in C
Practical
3ii
Work
Course DEP 2A / DEP 2B / DTK 2A / DTK 2B
Session June 2019

Direction: Complete the practical works. Consult with your lecturer for any
problem encountered.

OBJECTIVES :

Upon completion of this practical, students should be able:

1. To construct programs that uses simple if, if else, nested if else and switch
statements

EQUIPMENTS :

1. Personal computer / Laptop


2. Dev-C++ software and Arduino IDE
3. Maker UNO Board

Lab 3.1 Conditional Structure (Nested if-else)

Procedures:

3.1.1 Write the following programming below and try to understand it.

// This program demonstrates the use of nested if structure


#include <stdio.h> int
main ()
{ int A, B,
C;

printf("Enter any three integer number here:\n");


scanf("%d%d%d",&A,&B,&C); if(A>B) {
if(A>C) printf("Number %d is largest",A);
else
printf("Number %d is largest",C);
} else { if(B>C)
printf("Number %d is largest",B);
else printf("Number %d is
largest",C);
} getch();
}

3 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

3.1.2 Write the above program and save it as Practical31. To compile, click on
Execute and choose Compile. Alternatively the program can be compiled by
using Ctrl + F9 hotkey.
3.1.3 Run the program and capture the codes, Compile progress and output.
To Run, simply click on Execute > Run. Alternatively hit the Ctrl + F10.

Coding

Compile Progress

4 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Output

Lab 3.2 Conditional Structure (switch)

Procedures:

3.1.4 Write the following programming below and save it as Practical32. In your
own word discuss your understanding of switch statement.

// This program demonstrates the use of if else switch case structure


#include<stdio.h> int main() { int choice;
printf("Please choose your favourite fruits: \n");
printf("1: Strawberry\n"); //Selection provided
printf("2: Apple\n"); //Selection provided printf("3:
Banana\n"); //Selection provided printf("4: Durian\n");
//Selection provided printf("5: Watermelon\n");
//Selection provided scanf("%d",&choice);
switch(choice) //select the
choice
{ case(1):
printf("\nYour favourite food is Strawberry\n"); break;
case(2):
printf("\nYour favourite food is Apple\n"); break;
case(3):
printf("\nYour favourite food is Banana\n"); break;
case(4):
printf("\nYour favourite food is Durian\n"); break;
case(5):
printf("\nYour favourite food is Watermelon\n"); break;
default: //not entering the correct choice
printf("\nInvalid input\n"); //output for default
} getch();
return 0;
}

5 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Coding

Compile Progress

6 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Output1

Output2

7 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Lab 3.3 Conditional Structure (switch)

Procedures:

3.1.5 Write the following programming below and save it as Practical33.

#include<stdio.h> int main() { char x;


printf("Please indicate your choice: \n");
printf("M: Merah \n"); //Selection provided
printf("B: Biru \n"); //Selection provided
printf("K: Kuning \n\n"); //Selection provided
scanf("%c",&x);

switch(x)
{
case ‘M’:
case ‘m’: printf("Merah\n"); break;
case ‘B’:
case ‘b’: printf("Biru\n"); break;
case ‘K’:
case ‘k’: printf("Kuning\n"); break;
default: puts("pilihan salah");
}
system("pause"); return
0;
}

Coding

8 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Compile Progress

Output 1

Output 2

3.1.6 Discuss the importance of break in a switch case statement.

9 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Lab 3.4 Demonstrating Hardware & Software Operation (LED Chase Effect)

Procedures:

3.1.7 Open a new sketch in Arduino IDE and type the following code.

//initializing variables
byte ledPin[] =
{2,3,4,5,6,7,8,9,10,11,12,13}; int
ledDelay(65); int direction = 1; int
currentLED; unsigned long changeTime;
void setup() { for(int x =
0;x<10; x++){
pinMode(ledPin[x], OUTPUT);
}
changeTime=millis();
} void changeLED(){ for(int x
= 0;x<10;x++){
digitalWrite(ledPin[x],LOW);
}
digitalWrite(ledPin[currentLED], HIGH);
currentLED += direction;
if(currentLED==9){
direction=-1;
}
if(currentLED==0){
direction=1;
}
} void loop() { if((millis()-
changeTime)>ledDelay){
changeLED();
changeTime=millis();
}
}

3.1.8 Compile the sketch. If the coding is error free proceed with program upload
by pressing the Upload button.
3.1.9 It should immediately start running the code and your light should be racing
from one to the other.

10 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Circuit

11 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Coding

12 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Lab 3.5 Now you try!

Procedures:

3.1.10 By taking Lab 3.1 until Lab3.3 into consideration, solve the following task.
3.1.11 Write your codes by using Dev-C++ software and check for any errors.
3.1.12 Rewrite your codes in the spaces provided.

Task A (if statement)

Create a program that ask for the user’s nationality and age. If the user is
a Malaysian with the age of 21 and above. The program will pass a remarks
“eligible to vote”.
Answer:

Coding

13 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Compile Progress

Output Valid

Output Invalid

14 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Task B (if statement)

Transfer the following pseudocode to a complete C program


START
1. Prompt for and get doYouHaveJob
2. if(doYouHaveJob ==‘Y’|| doYouHaveJob ==‘y’)
2.1 Prompt for and get doYouSmoke
2.2 if (doYouSmoke==‘N’||doYouSmoke==‘n’)
2.2.1 Print "you can marry my daughter"
2.4 else
2.4.1 Print "I’m sorry, you’re not good enough"
END
Answer:

Coding

15 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Compile Progress

Output 1

Output 2

16 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Task C (Conditional Structures: Switch Case & Break)

Determine the outputs of the following programming code.

#include<stdio.h>
int main()
{ int x; printf("Please indicate your choice:
\n"); printf("1 \n"); //Selection provided
printf("2 \n"); //Selection provided printf("3
\n\n"); //Selection provided
scanf("%d",&x);

switch(x)
{
case 1: puts("satu"); break;
case 2: puts("dua"); case 3:
puts("tiga"); default:
puts("pilihan salah");
}
system("pause");
return 0;
}

Answer:

Input: 1 Input: 2 Input: 3 Input: other than


1, 2 @ 3
Output: Output: Output: Output:

Discuss the needs and function of break statement.

The purpose the break statement is to break out of a loop early. For example if the
following code asks a use input a integer number x. If x is divisible by 5, the break
statement is executed and this causes the exit from the loop.

17 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Task D (Conditional Structures: Nested if)

Change the following pseudocode to a complete C program


START
1. Read status
2. if status is married
2.1 Print “Enter number of children”
2.2 Read numOfChildren
2.3 if numOfChildren is more than 5
2.3.1 Print “ you have a big happy family”
2.4 else if numOfChildren is less than or equal 5
2.4.1 Print “ you have a nice family”
3. else if Status is single
4. print “ you will be married, insyaallah”
END

Answer:
Coding

18 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Compile progress

Output Single

Output Married 1

19 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Output Married 2

Task E (Conditional Structures: Switch Case & Break)

Change the following code snippet by applying the switch case selection
statement.

if (grade ==’A’) printf (“


Excellent\n”); else if
(grade ==’B’) printf (“
Good\n”); else if (grade
==’C’) printf
(“Average\n”); else if
(grade ==’D’) printf
(“Poor\n”); else if (grade
==’E’) printf (“Very
Weak\n”); else if (grade
==’F’) printf (“
Fail\n”);

Answer:

20 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Coding

[80 Marks]

21 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

Questions

1. What is required to avoid falling through from one case to the next? (1 marks)
A. End; B. break;
C. Stop; D. A semicolon.

2. What is the result of the following code? (1 marks)

int x=0;
switch(x)
{
case 1: printf("One");break; case
0: printf("Zero"); break; case 2:
printf(“Hello World");break;
}
A. One B. Zero
C. Hello World D. ZeroHello World

3. a) Fill in the blank (6 marks)

#include <stdio.h>
int main ()
{

Int years;

printf(“\n\n How many years of service?”);


scanf (“%d”, &years);

if(years>20)
{
printf(“Give a gold watch”);
}
else
{
if(years>10)
{
printf(“Give a paperweight\n”);
}
else
{
printf(“Give a pat on the back\n”);
}
}

return 0;
}

22 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

4. Fill in the blank (5 marks)

a) Trace the output : ( 1 Mark)


#include<stdio.h>

Int main___
{
int a = 5;

switch(a)
{

case 1:
printf("First");
break;
case 2:
printf("Second");
break;

case 3:
printf("Third");
break;

case 4:
printf("Final");
break;

return 0;
}

Output:

5. What is the difference between switch and nested if-else. (2 Marks)


Switch Nested if
The switch statement is easy to edit as Nested if-else statemnets it becomes
it has created the separate cases for difficult to identify the statements
different statements to be edited

Prepared by: Checked by: Approved by:

23 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii


CLO 2P

MOHD ZEID BIN ABU BAKAR


PENSYARAH JABATAN
KEJURUTERAAN ELEKTRIK
POLITEKNIK SULTAN IDRIS SHAH

24 PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii

You might also like