Clo 2P: Politeknik Sultan Idris Shah Jabatan Kejuruteraan Elektrik Dec20012 - Programming Fundamentals
Clo 2P: Politeknik Sultan Idris Shah Jabatan Kejuruteraan Elektrik Dec20012 - Programming Fundamentals
Direction: Complete the practical works. Consult with your lecturer for any
problem encountered.
OBJECTIVES :
1. To construct programs that uses simple if, if else, nested if else and switch
statements
EQUIPMENTS :
Procedures:
3.1.1 Write the following programming below and try to understand it.
1
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.
Procedures:
3.1.4 Write the following programming below and save it as Practical32. In your
own word discuss your understanding of switch statement.
2
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P
Procedures:
#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;
}
3
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
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.
4
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P
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.
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:
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
5
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P
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:
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
6
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P
#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:
7
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P
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:
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
8
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P
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:
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
[80 Marks]
9
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.
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
#include <________________>
int main ()
{
int _______________;
if(years>20)
{
printf(“Give a gold watch”);
}
__________
{
if(years>10)
{
printf(“Give a paperweight\n”);
}
___________
{
printf(“Give a pat on the back\n”);
}
}
_______________;
}
10
PSIS / JKE / DEC20012 – PROGRAMMING FUNDAMENTALS / PRACTICAL WORK 3ii
CLO 2P
#include<stdio.h>
________________
{
int a = 5;
switch(______)
{
case 1:
printf("First");
break;
case 2:
printf("Second");
__________________;
______:
printf("Third");
break;
_____________:
printf("Final");
break;
return 0;
}
Output:
Switch Nested if