0% found this document useful (0 votes)
20 views52 pages

Lemoa

ez c coding 2024

Uploaded by

elmaooo9979
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views52 pages

Lemoa

ez c coding 2024

Uploaded by

elmaooo9979
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

I

CONTENT

DECLARATION………….…………………………………………………………………. I

Acknowledgement………………………………………………………………………….. II

Task B1: Selection Control Structure……………………………………………………… 1

Question……………………………………………………………………………….1

Pseudocode…………………………………………………………………………… 2

Flowchart……………………………………………………………………………... 3

Source Code…………………………………………………………………………...4

Input and Output………………………………………………………………………6

Task B2: Repetition Control Structure……………………………………………………. 7

Question……………………………………………………………………………….7

Pseudocode…………………………………………………………………………....8

Flowchart……………………………………………………………………………..10

Source Code…………………………………………………………………………..11

Input and Output……………………………………………………………………...14

Task B3: Functions…………………….…………………………………………………... 15

Question……………………………………………………………………………...15

Pseudocode…………………………………………………………………………...16

Flowchart……………………………………………………………………………..18

Source Code……………………………………………………...…………………..26
Input and Output……………………………………………………………………...23
Task B4: Arrays….…………………….…………………………………………………... 24

Question……………………………………………………………………………...24

Pseudocode…………………………………………………………………………...26
Flowchart……………………………………………………………………………..28

Source Code……………………………………………………...…………………..30

Input and Output……………………………………………………………………...34

Task B5: Structures...………………….…………………………………………………... 36

Question……………………………………………………………………………...36
Pseudocode…………………………………………………………………………...38
Flowchart……………………………………………………………………………..41

Source Code……………………………………………………...…………………. 43

Input and Output……………………………………………………………………...47

Reflection…………………………………………………………………………………… 49
1
Pseudocode

Step 1:

Input: item code, quantity

Output: Display menu, total, item code and quantity


Formula: total=(float)quantity*price.

Step 2:

1) Start

2) Display variables for item code, description and price

3) Enter item code and quantity

4) Switch(itemcode)
4.1) case 1
4.1.1) price=25.00

4.2) case 2

4.2.1) price=5.50

4.3) case 3

4.3.1) price=200.00

4.4) case 4

4.4.1) price=150.00
4.5) case 5

4.5.1) price=80.00

4.6) case 6

4.6.1) price=30.00

4.7) Default

4.7.1) print “Invalid Item Code”

5) total=(float)quantity*price
6) print total

7) End

2
Flowchart

3
Source code

//name: Loo Ling Jin Yi


//ic:051205-10-2167
//index number:SW4110/1057
#include <stdio.h>
int main()
{
printf("XYZ Auto Tyres and Services SDN. BHD.");
printf("\n-----------------------------------------------------");
printf("\nItem Code\t\tDescription\t\tPrice");
printf("\n 1\t\t\tTyre alignment\t\t25.00");
printf("\n 2\t\t\tTyre balancing\t\t5.50");
printf("\n 3\t\t\tNew tyre\t\t200.00");
printf("\n 4\t\t\tEngine oil(Inclusive)\t150.00");
printf("\n 5\t\t\tEngine tuning\t\t80.00");
printf("\n 6\t\t\tBrake service\t\t30.00");
int itemcode,quantity; float price, total;

printf("\n\nEnter item code:");


scanf("%d",&itemcode);

switch(itemcode){
case 1:
price=25.00;
break;
case 2:
price=5.50;
break;
case 3:

4
price=200.00;
break;
case 4:
price=150.00;
break;
case 5:
price=80.00;
break;
case 6:
price=30.00;
break;
default:
printf("Invalid Item Code");
}
printf("Enter quantity:");
scanf("%d",&quantity);
total=(float)quantity*price;
printf("Total price:RM%.2f",total);

5
Input and Output

6
7
Pseudocode

Step 1:

Input: item code, quantity, continue buying

Output: Display menu, total price, item price, item code, quantity, customer number
Formula: total+(float)quantity*price.

Step 2:

1) Start

2) Display variables for item code, description and price

3) Start looping

3.1) Enter item code and quantity


3.2) Switch(itemcode)
3.2.1) case 1

3.2.1) price=25.00

3.2.2) case 2

3.2.1) price=5.50

3.2.3) case 3

3.2.1) price=200.00

3.2.4) case 4
3.2.1) price=150.00

3.2.5) case 5

3.2.1) price=80.00

3.2.6) case 6

3.2.1) price=30.00

3.2.7) Default

3.2.1) print “Invalid Item Code”


3.3) print (float)quatity*price

3.4) Enter any

8
4) End loop if any==’N’ or any==’n’

8) total+= quantity*price

9) print total

10) End

9
Flowchart

10
Source code

//name: Loo Ling Jin Yi

//ic:051205-10-2167

//index number:SW4110/1057

#include <stdio.h>

int main()

printf("XYZ Auto Tyres and Services SDN. BHD.");

printf("\n-----------------------------------------------------");

printf("\nItem Code\t\tDescription\t\tPrice");
printf("\n 1\t\t\tTyre alignment\t\t25.00");
printf("\n 2\t\t\tTyre balancing\t\t5.50");

printf("\n 3\t\t\tNew tyre\t\t200.00");

printf("\n 4\t\t\tEngine oil(Inclusive)\t150.00");

printf("\n 5\t\t\tEngine tuning\t\t80.00");

printf("\n 6\t\t\tBrake service\t\t30.00");//enter menu

int itemcode,quantity,i=0;

float price, total;

char x;//determine variables

do{

i++;

printf("\nNO.%d",i);
printf("\nEnter item code :");

scanf("%d",&itemcode);//define item code

11
switch(itemcode){

case 1:

price=25.00;

break;
case 2:

price=5.50;

break;

case 3:

price=200.00;

break;

case 4:
price=150.00;
break;

case 5:

price=80.00;

break;

case 6:

price=30.00;

break;
default:

printf("Invalid Item Code");

break;

} //define price based on item code

printf("Enter quantity :");

scanf("%d",&quantity);// enter quantity

total+=(float)quantity*price;// total price


printf("Item Price :RM%.2f",(float)quantity*price);//price for 1 unit

printf("\nAny more item(Y/N) :");


scanf(" %c",&x);// determine to exit or continue

12
}while(x=='y'||x=='Y');

printf("\n-------------------------------------------------------------------------");
printf("\nTotal price:RM%.2f",total);

printf("\n-------------------------------------------------------------------------");//total price
for all repitition
return 0;

13
Input and Output

14
15
Pseudocode

Step 1:

Input: item code, quantity, continue buying

Output: Display menu, total price, item price, item code, quantity, customer number
Formula: price=calculateItemPrice(itemcode, quantity)

total+=price

Step 2:

Main:

1) Start

2) Display variables for item code, description and price


3) Declare all integer, character, and float variables
4) make initial i=0

5) Start looping

5.1) i++

5.2) Enter itemcode and quantity

5.3) price=calculateItemPrice(itemcode, quantity)

5.4) print price

5.5) total+=price
5.6) Enter any

6) End loop if any==’N’ or any==’n’

7) Print total

8) End

Function:

1) Start
2) float customerinfo(int itemcode, int quantity)
3) Switch(itemcode)

3.1) case 1
3.1.1) price=25.00

16
3.2) case 2

3.2.1) price=5.50

3.3) case 3

3.3.1) price=200.00
3.4) case 4

3.4.1) price=150.00

3.5) case 5

3.5.1) price=80.00

3.6) case 6

5.2.6.1) price=30.00

3.7) Default
3.7.1) print “Invalid Item Code
4) return price*quantity

17
Flowchart

Main:

18
Function:

19
Source Code

//name: Loo Ling Jin Yi

//ic:051205-10-2167

//index number:SW4110/1057

#include <stdio.h>

float calculateItemPrice(int, int);//function prototype

int main()//main function

printf("XYZ Auto Tyres and Services SDN. BHD.");

printf("\n-----------------------------------------------------");
printf("\nItem Code\t\tDescription\t\tPrice");
printf("\n 1\t\t\tTyre alignment\t\t25.00");

printf("\n 2\t\t\tTyre balancing\t\t5.50");

printf("\n 3\t\t\tNew tyre\t\t200.00");

printf("\n 4\t\t\tEngine oil(Inclusive)\t150.00");

printf("\n 5\t\t\tEngine tuning\t\t80.00");

printf("\n 6\t\t\tBrake service\t\t30.00");//enter menu

int itemcode,quantity,i=0;
float price, total, itemprice;

char x;//determine variables

do{

i++;

printf("\nNO.%d",i);

price=calculateItemPrice(itemcode, quantity);//function call


printf("\nItem Price:RM%.2f",price);//print price

total+=price;//addition of all repetition

20
printf("\nAny more item(Y/N):");

scanf(" %c",&x);

}while(x=='y'||x=='Y');//while x=y the loop continues

printf("\n-------------------------------------------------------------------------");
printf("\nTotal price:RM%.2f",total);

printf("\n-------------------------------------------------------------------------");//total price from all


repitition
}

float calculateItemPrice(int itemcode,int quantity)//function definition

float initial,itemprice,price;

printf("\n\nEnter item code:");

scanf("%d",&itemcode);//define item code

switch(itemcode){

case 1:

price=25.00;

break;

case 2:
price=5.50;

break;

case 3:

price=200.00;

break;

case 4:

price=150.00;
break;

21
case 5:

price=80.00;

break;

case 6:
price=30.00;

break;

default:

printf("Invalid Item Code");//define price based on item code

printf("Enter quantity:");

scanf("%d",&quantity);//enter quantity
itemprice=quantity*price;//define itemprice
return quantity*price;//return value

22
Input and Output

23
24
25
Pseudocode

Step 1:

Input: item code, quantity, continue buying

Output: Display menu, total price, item price, item code, quantity, customer number,
customertotalprice[i]

Formula: price=calculateItemPrice(itemcode, quantity)

customertotalprice[i]+=price

Step 2:

Main:

1) Start
2) Display variables for item code, description and price
3) float calculateItemPrice(int,int)

4) Declare all integer, character, and float variables

5) Declare customertotalprice[i]

6) For i=0; i<3

6.1) j=0

6.2) Start looping

6.2.1) j++
6.2.2) Print customer no

6.2.3) Enter item code and quantity

6.2.4) price= calculateItemPrice(itemcode, quantity)

6.2.5) print price

6.2.6) customertotalprice[i]+=price

6.3) End loop if any==’N’ or any==’n’

6.4) print customertotalprice[i]


6.5) i++

7) end for loop when i=3


8) total=customertotalprice[0]+customertotalprice[1]+customertotalprice[2]

26
9) Print total

10) END

Function:
1) Start
2) float calculateItemPrice(int itemcode, int quantity)
3) Switch(itemcode)

3.1) case 1

3.1.1) price=25.00
3.2) case 2

3.2.1) price=5.50

3.3) case 3

3.3.1) price=200.00

3.4) case 4

3.4.1) price=150.00

3.5) case 5
3.5.1) price=80.00

3.6) case 6

5.2.6.1) price=30.00

3.7) Default

3.7.1) print “Invalid Item Code

4) return price*quantity

27
Flowchart

Main:

Float calculateItemPrice(int, int)

price=calculateItemPrice(itemcode, quantity)

True

28
Function:

29
Source Code

//name: Loo Ling Jin Yi

//ic:051205-10-2167

//index number: SW4110/1057

#include <stdio.h>

float calculateItemPrice (int,int);//function prototype

int main()//main function

int itemcode,quantity,i,j=0;

float price,total,initial;
char any;// define integers
float customertotalprice[3];

printf("XYZ Auto Tyres and Services SDN. BHD.");

printf("\n-----------------------------------------------------");

printf("\nItem Code\t\tDescription\t\tPrice");

printf("\n 1\t\t\tTyre alignment\t\t25.00");

printf("\n 2\t\t\tTyre balancing\t\t5.50");

printf("\n 3\t\t\tNew tyre\t\t200.00");


printf("\n 4\t\t\tEngine oil(Inclusive)\t150.00");

printf("\n 5\t\t\tEngine tuning\t\t80.00");

printf("\n 6\t\t\tBrake service\t\t30.00");//enter menu

for(i=0;i<3;i++)//for loop

printf("\n\nCustomer %d",i+1);//customer number

j=0;// for not effect by inner loop


do{

j++;
printf("\nNo.%d",j);

30
price= calculateItemPrice(itemcode, quantity);//function call

printf("Item price : %.2f",price);//print price

customertotalprice[i]+=price;

printf("\nAny more item(Y/N) : ");


scanf(" %c",&any);//know when to stop

}while(any=='y'||any=='Y');// do while loop

printf("---------------------------------------------------");

printf("\nTotal price : RM%.2f\n",customertotalprice[i]);

printf("---------------------------------------------------\n");//printing
of total 1 customer price

}
printf("---------------------------------------------------");

printf("\nCharge Summary\n");

printf("---------------------------------------------------");

printf("\nCustomer 1 : RM%.2f",customertotalprice[0]);

printf("\nCustomer 2 : RM%.2f",customertotalprice[1]);

printf("\nCustomer 3 : RM%.2f",customertotalprice[2]);//menu of total


customer price

total=customertotalprice[0]+customertotalprice[1]+customertotalprice[2];

printf("\n\n---------------------------------------------------");

printf("\nTotal sales : RM%.2f\n",total);


printf("---------------------------------------------------");//total of all customer
price
}

float calculateItemPrice(int itemcode, int quantity)//function definition

{
float price,initial;

31
int i;

float customertotalprice[3];

printf("\nEnter item code : ");

scanf("%d",&itemcode);//define itemcode

switch(itemcode)

case 1:

price=25.00;

break;

case 2:
price=5.50;
break;

case 3:

price=200.00;

break;

case 4:

price=150.00;

break;
case 5:

price=80.00;

break;

case 6:

price=30.00;

break;

default:
printf("invalid item code");

break;//define price by item price


}

32
printf("Enter quantity : ");

scanf("%d",&quantity);//define quantity

initial+=price*quantity;//equation for initial

return price*quantity;//return initial


}

33
Input and Output

34
35
RESTRICTED STPM
2024
ICT COURSEWORK
Assignment B5: Structures (Duration: 2 periods)
Based on Assignment B4, construct an algorithm that reads and stores three customer records
using the following structure.

struct Customer{
char custName[20];
char custID[4];
float price;
};
typedef struct Customer buyer;
buyer itemList[recordNo];

The algorithm will display the customer name, customer ID and the total price for each
customer and grand total of item price.

Write a program C based on the algorithm above. The program code in Assignment B4 can be
used.

An example of the user interface screen is shown below.

36
37
Pseudocode

Step 1:

Input: item code, quantity, continue buying, itemlist[i].custID, itemlist[i].custName,


itemlist[i]

Output: Display menu, total price, item price, item code, quantity, customer number,

itemlist[i].price, total[i]

Formula: itemlist[i].price= calculateItemPrice(itemcode, quantity)


total[i]+=itemlist[i].price, (float)total[0]+total[1]+total[2]

Step 2:

Main:

1)Start

2)Define recordNo as 3

3) float calculateItemPrice(int,int)

4) struct customer
4.1) char custName[20]

4.2) char custID[4]

4.3) float price

4.4) typedef struct customer buyer

4.4.1) buyer itemlist[recordNo]

5) Display item code, description and price

6) Declare total[recordNo],grandtotal, itemcode, quantity,j,i = 0,any;


7) Start loop if i<recordNo

7.1) j=0

7.2) Start loop

7.3) j++

7.4) print customer number

7.5) Enter itemlist[i].custName and itemlist[i].custID

7.5.1) Enter item code and quantity


7.5.2) itemlist[i].price= calculateItemPrice(itemcode, quantity)

38
7.5.3) print price

7.5.4) total[i]+=itemlist[i].price

7.5.5) End loop if any==’N’ or any==’n’

7.5.6) print total[i]


7.5.7) i++

7.5.8) end for loop when i=3

8) i=0

9) Start loop if i<recordNo

9.1) print itemlist[i].custName,itemlist[i].custID,total[i]

9.2) grand total=total[0] +total[1]+total[2]

9.3) i++
10) End loop
11) Print grand total

12) End

Function:

1) Start
2) float calculateItemPrice (int itemcode, int quantity)
3) Switch(itemcode)

3.1) case 1

3.1.1) price=25.00

3.2) case 2

3.2.1) price=5.50

3.3) case 3
3.3.1) price=200.00

3.4) case 4

3.4.1) price=150.00

3.5) case 5

3.5.1) price=80.00
3.6) case 6

39
5.2.6.1) price=30.00

3.7) Default

3.7.1) print “Invalid Item Code

4) return price*quantity

40
Flowchart:

float calculateItemPrice(int, int)

Itemlist[i].price=calculateItemPrice(itemcode, quantity)

41
Function:

42
Source code

//name: Loo Ling Jin Yi

//ic:051205-10-2167

//index number:SW4110/1057

#include <stdio.h>

#define recordNo 3

float calculateItemPrice(int,int);//function prototype

struct customer{

char custName[20];

char custID[4];
float price;

};

typedef struct customer buyer;

buyer itemlist [recordNo];

int main()

{ printf("XYZ Auto Tyres and Services SDN. BHD.");


printf("\n-----------------------------------------------------");

printf("\nItem Code\t\tDescription\t\tPrice");

printf("\n 1\t\t\tTyre alignment\t\t25.00");

printf("\n 2\t\t\tTyre balancing\t\t5.50");

printf("\n 3\t\t\tNew tyre\t\t200.00");

printf("\n 4\t\t\tEngine oil(Inclusive)\t150.00");

printf("\n 5\t\t\tEngine tuning\t\t80.00");


printf("\n 6\t\t\tBrake service\t\t30.00");//enter menu

float total[recordNo],grandtotal;

43
int itemcode, quantity,j,i;

char any;

for(i=0;i<recordNo;i++){

j=0;

printf("\n\nCustomer %d",i+1);

printf("\n-----------\n");

printf("\nCustomer Name : ");

scanf("%s",&itemlist[i].custName);

printf("Customer ID : ");
scanf(" %s",&itemlist[i].custID);

do{

j++;

printf("\n\nNo.%d",j);

itemlist[i].price= calculateItemPrice(itemcode, quantity);//function call

printf("Item price : %.2f",itemlist[i].price);//print price

total[i]+=itemlist[i].price;
printf("\nAny more item(Y/N) : ");

scanf(" %c",&any);

}while(any=='Y'||any=='y');

printf("\n-----------------------------------------------------------------
----------");

printf("\nTotal Price : RM%.2f",total[i]);

printf("\n-----------------------------------------------------------------
----------");

}
}

44
printf("\n\n---------------------------------------------------------------------------");

printf("\nCharge Summary");

printf("\n---------------------------------------------------------------------------");

printf("\nCustomer Name Customer ID Total price");


for(i=0;i<recordNo;i++){

printf("\n%s\t\t\t%s\t\t
RM%.2f",itemlist[i].custName,itemlist[i].custID,total[i]);
}

printf("\n---------------------------------------------------------------------------");

printf("\nTotal sales :RM%.2f",(float)total[0]+total[1]+total[2]);

printf("\n---------------------------------------------------------------------------");

float calculateItemPrice (int itemcode, int quantity)//function definition

{
float price,initial;

int i;

float total[i];

printf("\nEnter item code : ");

scanf("%d",&itemcode);//define itemcode

switch(itemcode)
{

case 1:

itemlist[i].price=25.00;

break;

case 2:

itemlist[i].price=5.50;

break;
case 3:

45
itemlist[i].price=200.00;

break;

case 4:

itemlist[i].price=150.00;
break;

case 5:

itemlist[i].price=80.00;

break;

case 6:

itemlist[i].price=30.00;

break;
default:
printf("invalid item code");

break;//define price by item price

printf("Enter quantity : ");

scanf("%d",&quantity);//define quantity

return itemlist[i].price*quantity;//return initial

46
Input and output

47
48
Reflexion

In this coursework I have learned a lot about c programming such as looping, structure, array
and many more. Even I have faced many challenges doing this coursework when developing
the code but I still managed to overcome the errors and problems. Besides that I also learnt a
lot about pseudocode and flowchart knowledge in this coursework. I am gratitude that I have
wonderful teachers and friends to help me along this journey and letting me develop these
programs smoothly.

49

You might also like