The words you are searching are inside this book. To get more targeted content, please make full-text search by clicking here.
Discover the best professional documents and content resources in AnyFlip Document Base.
Search
Published by amirsw881, 2022-05-11 10:24:14

Lab41 (1) (1)

Lab41 (1) (1)

CLO 2P

POLITEKNIK SULTAN IDRIS SHAH

JABATAN KEJURUTERAAN ELEKTRIK

DEC20012 - PROGRAMMING FUNDAMENTALS

Title Repetitions Structure in C

Practical 4i
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 for, while, do-while loop statements
2. To construct programs that use break, continue and goto statements

EQUIPMENTS :

1. Personal computer / Laptop
2. Dev-C++ software

Lab 4.1 Looping Structure (while)

Procedures:

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

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

char chYes;
printf("Please insert character:");
chYes = getchar();
while (( chYes == 'y') || ( chYes == 'Y'))
{

printf("Malaysia Boleh\n");
fflush(stdin);
printf("Please insert character:");
chYes = getchar();
}
}

4.1.2 Write the above program and save it as Practical41. To compile, click on
4.1.3 Execute and choose Compile. Alternatively the program can be compiled
by using Ctrl + F9 hotkey.
To Run, simply click on Execute > Run. Alternatively hit the Ctrl + F10.

1

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

CLO 2P

Lab 4.2 Conditional Structure (do while)

Procedures:

4.1.4 Write the following programming below and save it as Practical42.

#include <stdio.h>

void main()
{
int nRM50, nRM10, nRM5, nRM1, n50sen, n20sen, n10sen, n5sen, n1sen;
float fTotal=0.00;
char chrChoice;

do {
printf("\nHow many notes/coin do you have for the following currency:\n");

printf("RM 50: ");
scanf("%d", &nRM50);

printf("RM 10: ");
scanf("%d", &nRM10);

printf("RM 5: ");
scanf("%d", &nRM5);

printf("RM 1: ");
scanf("%d", &nRM1);

printf("50 Sen: ");
scanf("%d", &n50sen);

printf("20 Sen: ");
scanf("%d", &n20sen);

printf("10 Sen: ");
scanf("%d", &n10sen);

printf("5 Sen: ");
scanf("%d", &n5sen);

printf("1 Sen: ");
scanf("%d", &n1sen);

fTotal = ((nRM50 * 50) + (nRM10 * 10) + (nRM5 * 5) + (nRM1 * 1) + (n50sen *
0.50) + (n20sen * 0.20) + (n10sen * 0.10) + (n5sen * 0.05) + (n1sen * 0.01));

printf("\nYour total money is: RM %.2f", fTotal);

printf("\nDo you want to continue (Y/N): ");
scanf("%s", &chrChoice); }
while (toupper(chrChoice) == 'Y');
}

2

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

CLO 2P

Lab 4.3 Conditional Structure (for)

Procedures:

4.1.5 Write the following programming codes below and save it as Practical43.

#include <stdio.h>

char strName[50];
int nMarks[4], ctIndex;
float fAverage;
void main()
{

printf("\nInsert your name: ");
scanf("%s", &strName);

for(ctIndex = 1;ctIndex < 4;ctIndex = ctIndex + 1)
{
printf("\nInsert marks %d: ",ctIndex);
scanf("%d", &nMarks[ctIndex]);
}

fAverage= ((nMarks[1] + nMarks[2] + nMarks[3])/3);
printf("\nThe average marks for %s is: %.2f", strName, fAverage);
getch();
}

Lab 4.4 Break statement

Procedures:

4.1.6 Break statement can be used to exit from a loop. This is particularly useful
when a certain condition has been met other than the loop end condition.

4.1.7 Write the following programming below and save it as Practical44.

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

int i;
i = 0;
while ( i < 20 )
{

printf("\n i:%d ", i);
i++;
if ( i == 10)

break;
}
system("pause");
return 0;
}

4.1.8 Continue with the second example below which uses the break statement
to terminate a loop whenever the input is negative.

3

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

CLO 2P

#include <stdio.h>
int main()
{
float num,average,sum;
int i,n;
printf("Maximum no. of inputs\n");
scanf("%d",&n);

for(i=1;i<=n;++i)
{

printf("Enter n%d: ",i);
scanf("%f",&num);
if(num<0.0)

break; //for loop breaks if num<0.0
sum=sum+num;
}
average=sum/(i-1);
printf("Average=%.2f",average);
system("pause");
return 0;
}

LAB 4.5 Continue Statement

Procedures:

4.1.9. Write the following programming codes as shown below and try to
understand it.

#include<stdio.h>

int main()
{

int i;

i = 0;
while ( i < 20 )
{

printf("\n i:%d ", i);
i++;
continue;
printf("Nothing to see\n");
}
system("pause");
return 0;
}

4.1.10 Continue statement skip the rest of the commands in the current loop and
start from the top again. Note that the loop variable is still incremented.

4.1.11 Continue with the next example below. 4

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

CLO 2P

#include <stdio.h>
int main()
{
int i,num,product;
for(i=1,product=1;i<=4;++i)
{

printf("Enter num%d:",i);
scanf("%d",&num);
if(num==0)

continue; / *In this program, when num equals to zero, it skips
the statement product*=num and continue the loop. */
product*=num;
}
printf("product=%d",product);
system("pause");
return 0;
}

LAB 4.6 Goto Statement

Procedures:

4.6.1 Write the following programming codes as shown below and try to
understand it.

#include <stdio.h>
int main()
{
float num,average,sum;
int i,n;
printf("Maximum no. of inputs: ");
scanf("%d",&n);

for(i=1;i<=n;++i)
{

printf("Enter n%d: ",i);
scanf("%f",&num);

if(num<0.0)
goto jump; /*control moves to label jump */
sum=sum+num;
}

jump:
average=sum/(i-1);
printf("Average: %.2f",average);
system("pause");
return 0;
}

5

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

CLO 2P

4.6.2 Continue with the next example below.

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

int a = 10;

LOOP:
do
{

if( a == 15)
{

a = a + 1;
goto LOOP;
}
printf("value of a: %d\n", a);
a++;

}while( a < 20 );

system("pause");
return 0;
}

6

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

CLO 2P

Lab 4.7 Now you try!

Procedures:

4.1.12 By taking Lab 4.1 until Lab 4.6 into consideration, solve the following task.
4.1.13 Write your codes by using Dev-C++ software and check for any errors.
4.1.14 Rewrite your codes in the spaces provided.

Task A (while statement)

Write a program that will print the countdown from 5 to 0 by using while
statement. Output sample:

5
4
3
2
1
0
Boom!!

Answer:
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________

7

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

CLO 2P

Task B (do-while statement)

Modify the program in Task A so that instead of printing the countdown from
5 to 0, your program will now print the countdown of any number (n) given by
the user but this time by using do-while statement. For example:

Please enter a number: 7
7
6
5
4
3
2
1
0
Boom!!

Answer:

___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________

8

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

CLO 2P

Task C (for statement)

Modify the program in Task B so that your program will only read positive
integer n given by the user, computes and prints the sum of all integers
between 1 and n but this time by using for statement.

Answer:

___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________

9

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

CLO 2P

Task D (for statement)

Enhance the program in Task C so that your program will only read positive
integer n given by the user, computes and prints the sum of all integers
between 1 and n that is divisible by 3. Include appropriate input validation
in your program. Use for statement.

Answer:

___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________

10

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

CLO 2P

Task E (break & goto statement)
Write a program to calculate the summation of maximum 10 numbers. If a
negative number is entered, use break statement to terminate the loop and
display the sum. Ask the user if they wishes to restart the program all over
again and apply goto statement to transfer control to the beginning of the
program.
Answer:

___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________
___________________________________________________________________________________

[80 Marks]

11

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

CLO 2P

Questions (Answer all questions)

1. Which of the following is NOT a looping statements?
a) for
b) do while
c) while
d) repeat until

2. The following flowchart represents which control structure in C?

(a) If/else selection structure
(b) Switch/case selection structure
(c) While repetition structure
(d) Do/While repetition structure

3. What does the following code block while (x<100) meant?
a) While x is less than one hundred
b) While x is greater than one hundred
c) While x is equal to one hundred
d) While it wishes

4. Imprudent use of goto statement leads to?
a) Unstructured spaghetti code
b) Infinite loop
c) Break statement
d) None of the above

5. What is the final value of x when the code for (int x=0; x<10; x++) is executed? 12
a) 10
b) 9
c) 0
d) 1

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

CLO 2P

6. “Repeat the section of your program based on a specified number of times.”
The above statement is referring to
a) for
b) if
c) if-else
d) switch-case

7. What do you think will be the output of the code below?

#include<stdio.h>
void main()
{
int i;
for(i=0;i<10;i++)
printf("%d ",i);
}

a) 0 1 2 3 4 5 6 7 8 9
b) 0 1 2 3 4 5 6 7 8
c) 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8

8. Which of the following statements are TRUE about DO-WHILE loop statements?
i. It is also known as exit controlled loop
ii. It is an entry controlled loop.
iii. The body of loop will be executed at least once even if the test condition is
false.
iv. If test condition is initially false the body of loop is not executed at all.

a) i and iii
b) i and iv
c) ii and iii
d) ii and iv

13

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

CLO 2P

9. How many times does x value is being tested in the code below?

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

while (x<3)
i++;
printf("DEC2012 Is Fun!\n");

}

a) 2
b) 4
c) 3
d) 1

10. Choose the value of NUM so that the output would be 6:

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

int i,
for(i = NUM; i>4;i -=2)
{
printf(“%d”, i);
}
return(0);
}

a) 6
b) 4
c) 2
d) 8

Prepared by: Checked by: Approved by:

………………………………… ………………………………… …………………………………
() () ()

14

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


Click to View FlipBook Version
Previous Book
PAI XI Bab 4 Menganalisis pelaksanaan Khutbah Tabligh dan Dakwah LKPD
Next Book
KISAH SI IKAN EMAS DAN NELAYAN YANG TAMAK (3)