top of page
Writer's pictureD A Rosi Arsida Wardani

Programming and Algorithm

Example of Algorithm in Real Life


 

Washing clothes


Analysis :

Problem : Algorithm of washing clothes with automatic washing machine

Requirement :

Input : Clothes, detergent, softener

Process : Put the clothes into the washing machine then washed

Output : Clothes washed and dried


Algorithm :

  1. Plug the washing machine cable into the stop-contact

  2. Open the washing machine's top

  3. Put clothes into the machine

  4. Add detergent and softener

  5. Close the machine's top

  6. Turn it on by press the power button

  7. Set the water discharge

  8. Select wash mode

  9. Press start button

  10. Wait until the washing is finished

  11. When it's finished, turn off the machine

  12. Unplug the cable

  13. Open the top of the machine

  14. Take out the clothes from the washing machine

  15. Hang clothes under the sun


Flowchart



 


The Remaining Money for Groceries


Analysis :

Problem : Algorithm to find the total amount of change for groceries

Requirement :

Input : The number of types of goods purchased, the number of purchases per item, unit price, total of money payment

Process : Calculating the number of change based on the entered data

Output : Total amount of change


Algorithm :

  1. Enter the number of types

  2. Repeat the steps below for each item :

  3. Enter the amount of money payment

  4. Find the purchase price (buy_price <-- sum * each)

  5. Find the total purchase price (total += buy_price)

  6. Find change (change = pay - total)

C++ Program :


#include <iostream>
using namespace std;

struct change{
	string name;
	int sum;
	int each;
}buy[100];

int main (){
	int n, i, pay, change;
	int buy_price[100];
	int total = 0;
	cout<<"Enter the amount of types : ";
	cin>>n;
	cout<<endl;
	for (i=1;i<=n;i++){
		cout<<"Item-"<<i<<" : ";
		cin>>buy[i].name;
		cout<<"Sum of "<<buy[i].name<<" = ";
		cin>>buy[i].sum;
		cout<<"Price per each "<<buy[i].name<<" = ";
		cin>>buy[i].each;
		cout<<endl;
	}
	cout<<"Enter the amount of money payment : ";
	cin>>pay;
	cout<<endl;
	for (i=1;i<=n;i++){
		buy_price[i]=buy[i].sum*buy[i].each;
		cout<<"Price of "<<buy[i].name<<" = "<<buy[i].sum<<" *  		   "<<buy[i].each<<" = "<<buy_price[i];
		cout<<endl;
	}
	cout<<endl;
	for (i=1;i<=n;i++){
		total+=buy_price[i];
	}
	cout<<"Price total = "<<total;
	cout<<endl;
	change=pay-total;
	cout<<"Sum of change = "<<change;
	return 0;
}

Output :



Recent Posts

See All

One-dimensional Arrays

One-dimensional arrays are data structures that contain data types of the same type. In the form of a group of related memories...

Loop

Introduction Examples of algorithms in everyday life : Example 1 : To finish eating a plate of rice (initial conditions) Mouthfuls of a...

Comments


Post: Blog2_Post
bottom of page