Docsity

Writing and Optimizing Code: Challenges and Best Practices, Exams of Technology

Toggle sidebar
bg1
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
BH01463
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
D1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f

Partial preview of the text

Download Writing and Optimizing Code: Challenges and Best Practices and more Exams Technology in PDF only on Docsity!

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

 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date:

Table of Contents

  • Introduction
  • Body
    • I. Algorithms
        1. Definition:
        1. Example:
    • II. How to develop an application:...........................................................................................................................
    • III. Solve a problem with algorithm
      • 1.The problem
      • 2 .Analayze the problem
      • 3 .Flowchart
      • 4.Source Code......................................................................................................................................................
  • Determine the steps taken from writing code to execution
  • Analyse the process of writing code, including the potential challenges faced.
  • Hình Table of Figures
  • Hình
  • Hình
  • Hình
  • Hình
  • Hình

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.

  • Process: Divide the sorted list into two halves and compare the target value with the middle element. If they match, the search is successful. Otherwise, narrow down the search by recursively dividing the list in half until the target value is found or the search range becomes empty. The process of programming an application involves several steps, including:
  1. Problem Analysis: Understand the problem or task that the application needs to solve. Identify the inputs, outputs, constraints, and requirements.
  2. Algorithm Design: Develop an algorithm that outlines the necessary steps and logic to solve the problem. Consider the efficiency, complexity, and potential edge cases.
  3. Pseudocode or Flowchart: Represent the algorithm using pseudocode or a flowchart, which provides a high-level description of the solution without specific programming syntax.
  4. Programming Language Selection: Choose a programming language that best suits the requirements of the application, considering factors such as performance, compatibility, and available libraries.
  5. Coding: Implement the algorithm in the chosen programming language, translating the pseudocode or flowchart into actual code. Follow best practices, use proper coding conventions, and ensure code readability and maintainability.
  6. Testing and Debugging: Test the application with different inputs and scenarios to ensure it produces the expected results. Identify and fix any errors or bugs that occur during testing.
  7. Optimization: Analyze the code for potential optimizations to improve performance, reduce resource usage, or enhance the user experience.
  1. After iterating through all the elements, the value of "max" will be the maximum number in the array.
  2. Return the value of "max" as the output. Here's an implementation of the algorithm in Python: Hình 2 (https://chat.openai.com) In this example, the algorithm iterates through each element of the array, compares it with the current maximum number, and updates the maximum if a larger number is found. Finally, it returns the maximum number as the output.

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:

  1. Define the Application:
    • Clearly identify the purpose, goals, and target audience of the application.
    • Define the key features and functionalities it should have.
    • Conduct market research and competitor analysis, if applicable.
  2. Plan and Design:
    • Create a detailed plan outlining the development process, timelines, and resource requirements.
    • Design the user interface (UI) and user experience (UX) to ensure a seamless and intuitive interaction.
    • Develop wireframes or prototypes to visualize the application's flow and layout.
    • Design the database schema, if the application requires one.
  3. Choose the Technology Stack:
    • Select the appropriate programming languages, frameworks, libraries, and tools for your application.
    • Consider factors such as scalability, performance, security, and compatibility with the target platforms.
  4. Front-End Development:
    • Develop the user interface using HTML, CSS, and JavaScript.
    • Implement the design elements, layouts, and interactions.
    • Ensure responsiveness across different devices and browsers.
  1. Maintenance and Updates:
    • Monitor the application's performance, stability, and security.
    • Address bug reports and user feedback promptly.
    • Regularly update the application with new features, enhancements, and security patches.
    • Perform periodic maintenance tasks such as backups and database optimization. Throughout the development process, it's crucial to follow best practices, maintain proper documentation, and collaborate effectively with team members. Regular communication, version control, and project management tools can aid in the smooth execution of the project.

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])

//========= SWAP BILL =========================

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=========================