Practical Work 4 AFIQ Saidin PDF PDF

Title Practical Work 4 AFIQ Saidin PDF
Author syazwani saidin
Course Electrical Engineering
Institution Universiti Teknologi MARA
Pages 20
File Size 1.4 MB
File Type PDF
Total Downloads 586
Total Views 929

Summary

Download Practical Work 4 AFIQ Saidin PDF PDF


Description

ELECTRICAL ENGINEERING DEPARTMENT ACADEMIC SESSION: _____________ DEC20012: PROGRAMMING FUNDAMENTALS CLO2 PRACTICAL WORK 4 : PRACTICAL WORK DATE :

build programs written in C language for assigned mini project during practical work session ( P4 , PLO 5 ) SWITCH Selection Statements 5/5/2021

LECTURER’S NAME: NAME MUHAMMAD AFIQ AKMAL BIN SAIDIN STUDENT ID : 04DET20F1052

Practical Skill Assessment [CLO2]

Report Assessment

1. 2. 3. 4. 5. 6. 7.

Fixes Complete Manipulates Builds Display Lab Participation Efficiency

Total Practical Skill Assessment A. Total Practical Skill Assessment (80%) = /35 *80 1. Theory 2. Discussion 3. Conclusion

Total Report Assessment B. Total Report Assessment (20%) = /15*20 Total A+B (100%) DATE SUBMIT :4/5/2021 DATE RETURN : 1 LEARNING OUTCOMES (LO): 1. Understand Switch selection statements. 2. Apply Switch selection statements. 2 OBJECTIVE 1. To fixes, Complete and Manipulates the Switch selection statement. 2. To builds flowchart of Switch selection statement. 3. To display the output of Switch selection statement. 3 THEORY ▪ ▪ ▪ ▪ ▪ ▪

4

The C statements that are used for altering the normal flow of a program are called control statements. Various control statements, such as selection and looping statements, are available in C. Selection statements are used to execute a set of instructions, just once, when the specified condition is satisfied. The types of selection statements available in C are if and switch. The types of looping statements available in C are for, while and do-while. The selection statement, which allows you to choose from a number of choices, is the switch statement.

EQUIPMENT / TOOLS Computer, C programming language ( Dev-cpp, codeblock, dll)

PART A : Independent Learning (NF2F) (Student must do this task at home or before entering a computer lab)

Define control statements. Answer: A control statement is a statement that determines whether other statements will be executed. An if statement decides whether to execute another statement, or decides which of two statements to execute. A loop decides how many times to execute another statement.

List the types of control statements.

Answer; • Conditional/Selection statements • Iteration/Loop statements • Jump statements

Define selection statement Answer: Selection statements are used to evaluate expression and direct the execution of the program, depending on the result of the evaluation

list selection statements Answer: ➢ simple if statements ➢ if-else statements ➢ Nested if-else statement ➢ switch statements Define SWITCH statement Answer: ➢ The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression ➢ Switch is a control statement that allows a value to change control of execution. ➢ The switch statement is used when you choose from a number of choices.

PART A : Independent Learning (NF2F) (Student must do this task at home or before entering a computer lab)

Write the syntax for SWITCH Statement

Draw flowchart for SWITCH Statement

Answer:

Answer;

switch () { case : ; break; case : ; break; ... case : ; break; default: ; }

PART B: PRACTICAL WORK AT LABORATORY Practical Assignments 1 (Fixes Program) To calculate series and parallel circuits.

Series parallel Find an errors and correct the programme to illustrate the working of SWITCH statements to calculate the total resistance for series and parallel circuit. Get the output after fixes all errors Correction: #include main() { int selection; float series, parallel; float R1=100,R2=300, R3=500; printf("1: Series Circuit\n");

printf("2. Parallel Circuit \n"); printf("Please Select your choice : "); SCANF("%d", &selection); switch(SELECTION) { case 1: series = R11+R12+R13; printf(" Total resistance = %.2f Ohm\n",series); breaks; case 2: parallel = 1/(1/R1 +1/R2+1/R3) ; printf(" Total resistance = %.6f Ohm\n",parallel); breaks; Default: printf(" wrong choice \n "); } return 0; }

Output: If selection =1

Output: If selection =2

Output:

if selection =20

Practical Assignments 2 (Complete Program)

RT = R1 + R2 + R3 Series parallel Complete all statements in the programme below to illustrate the working of SWITCH statements to calculate the total resistance for series and parallel circuit. Get the output after fixes all errors #include main() { Int____________________________________; float series, parallel,R1,R2,R3; printf("******************************************"); printf("\nEnter the value of R1= "); scanf( _______________________________); printf("\nEnter the value of R2= "); scanf("%f",&R2); printf("\nEnter the value of R2= "); scanf ( _______________________________); printf("******************************************"); printf("\n1: Series Circuit"); printf("\n2. Parallel Circuit "); printf("\nPlease Select your choice "); printf("******************************************"); scanf("%d",&selection); switch(selection) { case 1: series = R1+R2+R3; printf(" _______________________________________”, ______________); break; case 2: parallel = 1/(1/R1 +1/R2+1/R3) ; printf(" _______________________________________”, ______________); break; default:

printf(" wrong choice \n "); } return 0; }

Program:

Output: If choice =1

If choice =2

If choice =3

If choice =100

Practical Exercise 1: Manipulates Program 1 By referring to the Practical Assignments 2, draw flowchart and write a C program using SWITCH statements to calculate the total capacitance of three capacitors of series and parallel connected. Get the output of the programme. ( the formula of series and parallel capacitors are given below

Series Ask input from user the value of C1,C2 and C3

parallel

Flowchart :

start

Display, Please enter the value that you want

false

Case 1

False Wrong choice

Case 2

true

true

Series circuit

Parallel circuit

stop

Program:

Output: If choice =1

If choice =2

Practical Exercise 2: Manipulates Program 2: By referring to the Practical Assignments 2, draw flowchart and write a C program using SWITCH statement to make a choice for calculate : Total resistance of the circuit, R total

Choice 1:

Current, I

Choice 2: Voltage drop across resistor 8Ω, VR3

Choice 3: Current through resistor 4Ω, IR1

referring to the circuit below

Ask input from user R1,R2,R3 and E

Flowchart :

START

I= VR3= IR1=

RT=1/((1/R1)+(1/R2)+(1/R3))

false

CASE 1: 1=E/RT

false

CASE 2: VR3=E

true

I=

stop

Program:

true

VR3=

False

CASE 3: IR1=E/R1

true

E/R1

Default: Wrong selection

Output: Case 1

Case 3

Discussion for practical work 2 Knowledge: IF, IF-ELSE, and SWITCH statements work Understanding: Concepts and Relate theory to practice

❖ The C statements that are used for altering the normal flow of a program are called control statements.Various control statements, such as selection and looping statements, are available in C.Selection statements are used to execute a set of instructions, just once, when the specified condition is satisfied.The types of selection statements available in C are if and switch.The if statement is used for checking conditions and transferring the control to the body of the if statement based on the condition. A switch statement comes in handy when code is

needed to handle a small amount of precise conditions that would lead to a particular action. Based on the input variable in switch statement, case statements are executed until it reached the end. From this theory, I can build the

codding the working of SWITCH statements to calculate the total resistance for series and parallel circuit and get the output. Next, I also learn how to use switch statement like a in many case.

Conclusion for practical work 2 1. The conclusion thoroughly and accurately explains the results of the experiment and why they occurred

❖ In conclusion, from this experiment I learn about theory of switch statement and how the switch statement working in C programming. The switch statement is an essential feature when it comes to programming and is a great alternative to the if-else statement. It is great to use especially when looking at its readability and comprehension aspects. It has been in use for long and is a worthy companion for a variety of languages from c++.It is useful when comparing variables and presents the ease in debugging which is a feature to look out for when coming up with codes. However, it is advisable to know some of its cons for a worthy moment using it.The findings I can take from the experimental process is how to builds the

flowchart and codding to switch statement working in C programme to display the output and last is to fixes, complete and manipulates the switch selection statement. Lastly, The switch statement is used when to choose from a number of choices....


Similar Free PDFs