























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!
























ASSIGNMENT 1 FRONT SHEET Qualification BTEC Level 5 HND Diploma in Computing Unit number and title Unit 1: Programming Submission date Date Received 1st submission Re-submission Date Date Received 2nd submission Student Name Nguyen Duc Ngoc Student ID BH Class Assessor name Dinh Van Dong Student declaration I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that making a false declaration is a form of malpractice. Student’s signature Grading grid P1 P2 M1 D
Grade: Assessor Signature: Date:
Introduction Hình 1 (https://www.investopedia.com/terms/a/algorithm.asp) Algorithms are fundamental to computer science and play a crucial role in solving problems and performing computations efficiently. An algorithm is a step-by-step procedure or set of rules for solving a problem or accomplishing a specific task. It serves as a blueprint or recipe that guides the computer in executing a sequence of well-defined instructions.
Body I. Algorithms
1. Definition: Basic algorithms are the foundational building blocks of any program or application. They are step-by-step procedures that outline the logic and operations required to solve a specific problem or perform a particular task. Here are a few examples of basic algorithms: 1.1 Linear Search Algorithm: - Input: A list of elements and a target value. - Output: The index of the target value in the list or a sentinel value if not found. - Process: Iterate through each element in the list and compare it with the target value until a match is found or the end of the list is reached. 1.2 Bubble Sort Algorithm: - Input: An array of elements. - Output: The array sorted in ascending order. - Process: Repeatedly iterate through the array, comparing adjacent elements and swapping them if they are in the wrong order, until the entire array is sorted. 1.3 Binary Search Algorithm: - Input: A sorted list of elements and a target value. - Output: The index of the target value in the list or a sentinel value if not found.
II. How to develop an application: Developing an application involves a systematic process that includes several steps. Here's a high- level overview of the typical development process:
III. Solve a problem with algorithm 1.The problem The code in 4 is a C# program that calculates water bills for a given number of customers, calculates water bills for different types of customers based on their water consumption. It prompts the user to input the number of customers, their names, customer types, water meter readings for last month and this month. It then calculates the amount of consumption and the corresponding bill for each customer. Finally, it sorts the customers based on their bill amounts and displays the sorted list. The program starts with the Main method, which is the entry point of the program. The user is prompted to input the number of customers. Arrays are initialized to store information about each customer, such as their name, customer type, number of people in the household, and water meter readings for the last month and this month. 2 .Analayze the problem A loop is used to collect the required information for each customer from the user. After collecting the information, the program proceeds to calculate the water bill for each customer using the CaculateWaterBill method. The calculated bill amounts are stored in the arrayWaterBill. The program then sorts the customers based on their water bill amounts using a nested loop and the bubble sort algorithm. Finally, the program outputs the customer details and their sorted water bill amounts. The CaculateWaterBill method calculates the water bill based on the amount of water consumed, the number of people in the household, and the customer type. It uses a switch statement to determine the appropriate calculation based on the customer type. Declaring and initializing necessary variables and arrays. Getting the number of customers from the user and creating arrays to store customer information. Prompting the user to input customer details, such as name, customer type, number of people in the household (for household customers), and water meter readings. Calculating the amount of consumption and the bill for each customer using the
3 .Flowchart Hình 3
Hình 4 4.Source Code int numberOfCustomer =0;
Console.WriteLine("Input water meter in this month"); arrayWaterMeterThisMonth[i]= int.Parse(Console.ReadLine()); } // OUTPUT Console.WriteLine("=================OUTPUT===================="); for(int i = 0;i < numberOfCustomer; i++) { Console.WriteLine("customer " + i); Console.WriteLine("Name: " + arrayCustomerName[i]); Console.WriteLine("LastMonth:" + arrayWaterMeterLastMonth[i]); Console.WriteLine("ThisMonth:" + arrayWaterMeterThisMonth[i]); double amount = (arrayWaterMeterThisMonth[i] - arrayWaterMeterLastMonth[i]); Console.WriteLine("Amount of consumption:" + amount ); double money = CaculateWaterBill(amount, arrayNumberOfPeople[i], arrayCustomerType[i]); arrayWaterBill[i] = money; Console.WriteLine("Bill:" + money); } // SORT BY WATER BILL MONEY for(int i = 0;i< numberOfCustomer;i++) { for(int j = 0;j< numberOfCustomer; j++) { if (arrayWaterBill[i] < arrayWaterBill[j])
double temp = arrayWaterBill[i]; arrayWaterBill[i]= arrayWaterBill[j]; arrayWaterBill[j]= temp; //=============SWAP number of people========================= /* int[] arrayNumberOfPeople = new int[numberOfCustomer]; int[] arrayCustomerType = new int[numberOfCustomer]; String[] arrayCustomerName = new String[numberOfCustomer]; double[] arrayWaterMeterLastMonth = new double[numberOfCustomer]; double[] arrayWaterMeterThisMonth = new double[numberOfCustomer];*/ int temp2 = arrayNumberOfPeople[i]; arrayNumberOfPeople[i]= temp2; arrayNumberOfPeople[j]= temp2; //================SWAP customer type============================ int temp3 = arrayCustomerType[i]; arrayCustomerType[i]= arrayCustomerType[j]; arrayCustomerType[j]= temp3; //=================SWAP customer name=========================== String temp4 = arrayCustomerName[i]; arrayCustomerName[i]= arrayCustomerName[j]; arrayCustomerName[j]= temp4; //=====================Swap last month=========================