Assignment 2nd Programming Anhnd Bh01788
Assignment 2nd Programming Anhnd Bh01788
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.
Grading grid
P1 P2 M1 D1
Summative Feedback: Resubmission Feedback:
Program development involves problem analysis, algorithm design, program design, implementation,
testing, deployment, and maintenance. Problem analysis requires identifying requirements, while
algorithm design involves devising algorithms to solve problems. Program design involves structuring the
application, selecting data structures, and planning user interactions.
Implementation involves writing code following the rules of the programming language, while testing
and debugging ensure the application is functional and correct. Deployment and maintenance involve
preparing the application for deployment, maintaining it by addressing bug fixes and adding new
features.
2. Content
3. Algorithm
1.3. Definition
An algorithm is a step-by-step set of instructions designed to solve a specific task or problem.
It provides a clear and unambiguous description of the problem-solving process, outlining the
exact sequence of operations to be executed, the data to be processed, and the desired
output or end goal. Algorithms can be presented in different forms, such as programming
code, flowcharts, or pseudocode.
Algorithms come in different categories based on their complexity and purpose. Basic algorithms are
commonly used in everyday programming tasks and include sorting, searching, and mathematical
operations. Advanced algorithms, such as graph traversal, dynamic programming, and machine learning
algorithms, are more complex and require a deeper understanding of computational theory.
Anon, (2021). Algorithm Là Gì? Những điều Cần Biết Về Thuật Toán Algorithm. [online] Available at:
https://xuyenvietmedia.com/blogs/algorithm-la-gi/ [Accessed 18 Oct. 2023].
The design and analysis of algorithms are crucial components of computer science and programming.
Algorithm analysis involves evaluating the efficiency and performance characteristics of algorithms, such
as their time complexity (how long they take to run) and space complexity (how much memory they
require). This analysis helps in selecting the most appropriate algorithm for a given problem, considering
factors such as input size, available resources, and desired execution time.
Overall, algorithms play a vital role in problem-solving and computational tasks. They enable
programmers and computer scientists to devise systematic and efficient solutions to a wide range of
problems, forming the foundation of software development.
Shutterstock. (n.d.). ‘Bột nhào’ – 5.529.106 Ảnh, vector, đối tượng 3D và hình chụp có sẵn. [online]
Available at: https://www.shutterstock.com/vi/search/b%E1%BB%99t-nh%C3%A0o [Accessed 23 Oct.
2023].
- Pizza sauce
Pngtree. (n.d.). Nền Maasdam Phô Mai Thụy Sĩ Lát Bữa ăn Sáng đặc Sản Hình Chụp Và Hình ảnh Để Tải
Về Miễn Phí. [online] Available at: https://vi.pngtree.com/freebackground/maasdam-swiss-cheese-slice-
breakfast-shot-delicatessen-photo_3577858.html [Accessed 23 Oct. 2023].
- Toppings (e.g., pepperoni, mushrooms, onions, etc.)
iStock. (2022). A set of different toppings for pizza in cartoon style, different... [online] Available at:
https://www.istockphoto.com/vi/vec-to/m%E1%BB%99t-b%E1%BB%99-c%C3%A1c-lo%E1%BA%A1i-
topping-kh%C3%A1c-nhau-cho-pizza-theo-phong-c%C3%A1ch-ho%E1%BA%A1t-h%C3%ACnh-c%C3%A1c-
lo%E1%BA%A1i-gm1408277497-459171468 [Accessed 23 Oct. 2023].
2. Preheat the oven to the appropriate temperature according to the pizza dough instructions.
3. Roll out the pizza dough on a floured surface to the desired thickness and shape.
5. Spread a layer of pizza sauce evenly over the dough, leaving a small border around the edges.
6. Sprinkle a generous amount of cheese over the sauce, covering the entire surface.
8. Place the pizza in the preheated oven and bake according to the dough instructions or until the crust is
golden brown and the cheese is melted and bubbly.
9. Once the pizza is cooked, carefully remove it from the oven using oven mitts or a pizza peel.
10. Let the pizza cool for a few minutes before slicing it into wedges.
4. How to develope an application
Developing an application that involves algorithms requires specific steps and considerations. Here's a
general outline of the process:
- Determine the objective of your algorithm application. What problem or task will it address? Will it be
an educational tool, a reference guide, or a practical implementation of specific algorithms?
exposit_marketing (2019). How to Choose a Programming Language for a Project | Selecting and
Choosing a Programming Language. [online] Exposit. Available at: https://www.exposit.com/blog/which-
programming-language-choose-software-product-development/ [Accessed 18 Oct. 2023].
- Select a programming language that suits your application's requirements. Consider factors such as
ease of implementation, available libraries or frameworks, and compatibility with your target platform.
- Set up the development environment by installing the necessary software and tools for coding and
testing.
- Write the code to implement the algorithms you want to showcase or utilize in your application.
Follow best practices for coding, such as modularization, encapsulation, and code reusability.
- Ensure that the algorithm logic is correctly implemented and functions as expected. Thoroughly test
the algorithms with different input scenarios to validate their accuracy and efficiency.
- Connect the user interface with the algorithm logic. Implement the necessary functionality to allow
users to interact with the algorithms through the UI.
- Design and implement any necessary data input mechanisms, such as text fields, file upload features,
or interactive visualizations.
- Consider incorporating additional features that enhance the functionality and usability of your
algorithm application. This may include features like algorithm visualization, step-by-step execution,
algorithm comparisons, or performance analysis.
- Thoroughly test your application to identify and fix any bugs or issues. Perform unit tests on individual
algorithms, as well as integration tests to ensure the overall functionality of the application.
- Consider soliciting feedback from users or fellow developers to gather insights and identify areas for
improvement.
- Evaluate the performance of your application and identify areas for optimization. This may involve
optimizing algorithms for efficiency, improving UI responsiveness, or enhancing user experience.
- Refine the application based on user feedback and incorporate any necessary updates or
improvements.
9. Document and Deploy:
- Provide clear documentation for your application, including instructions on how to use it, descriptions
of the implemented algorithms, and any relevant references or resources.
- Package your application for deployment, considering the target platform (e.g., desktop, web, mobile)
and the appropriate distribution channels. This may involve creating installation packages, publishing to
an app store, or hosting the application on a web server.
- Regularly maintain and update your application to address any bugs, security vulnerabilities, or
compatibility issues that may arise.
- Stay informed about advancements in algorithms and programming techniques to incorporate new
features or improve existing ones.
a. The problem
c. Flowchart
d. Source code
a. The problem
You are tasked with developing an algorithm to calculate the water bill for a household based on the
water consumption recorded by a water meter. The billing rate is determined by a tiered pricing
structure, where different rates apply depending on the amount of water consumed.
c. Flowchart
d. Source code
6. Determine the steps taken from writing code to execution
int[] numberOfPeople = { 4, 0, 2 };
int[] customerType = { 1, 2, 3 };
- `customerType`: The type of customer (e.g., 1 for household, 2 for public service, 3 for production unit,
4 for business service).
- `waterMeterLastMonth`: The water meter reading from the last month for each customer.
- `waterMeterThisMonth`: The water meter reading for the current month for each customer.
{
case 1: // Household
break;
break;
break;
break;
```
Inside the `switch` statement, the `customerType` variable is evaluated. Based on its value, specific code
blocks are executed for different customer types.
- Case 1: Household
You can add or modify the cases as per your requirements to handle additional customer types. Within
each case, you can include the specific calculations or logic that should be performed for that particular
customer type.
For example, if you want to add a new customer type "5: Industrial", you can extend the `switch`
statement as follows:
case 5: // Industrial
break;
```
You can customize the code within each case block to suit your specific business rules and calculations
for different customer types.
using System;
namespace WaterBill
int numberOfCustomers = 3;
int[] numberOfPeople = { 4, 0, 2 };
int[] customerType = { 1, 2, 3 };
Console.WriteLine("=================OUTPUT====================");
waterBill[i] = bill;
Console.WriteLine();
Array.Sort(waterBill, customerName);
{
Console.WriteLine("Customer " + (i + 1));
Console.WriteLine();
Console.ReadKey();
}
static double CalculateWaterBill(double amount, int numberOfPeople, int customerType)
double price = 0;
switch (customerType)
case 1: // Household
else
}
break;
case 2: // Public Service
break;
case 3: // Production Unit
break;
break;
}
return price * 1.1; // Including VAT
}
}
}
The `CalculateWaterBill` function takes three parameters: `amount`, `numberOfPeople`, and
`customerType`. Let's break down its functionality step by step:
1. First, a variable `price` is initialized to 0. This variable will hold the calculated water bill amount.
2. The function uses a `switch` statement to determine the customer type and perform the
corresponding calculations.
3. If the `customerType` is 1 (representing a Household customer), the function calculates the average
water consumption per person (`avg`) by dividing the `amount` by the `numberOfPeople`.
4. Based on the value of `avg`, the function assigns a specific price per unit of water consumed. Here are
the conditions checked:
- If `avg` is greater than 30, the price is set to `amount * (15929 * 1.1)`.
- If `avg` is greater than 20, the price is set to `amount * (8699 * 1.1)`.
- If `avg` is greater than 10, the price is set to `amount * (7052 * 1.1)`.
- If none of the above conditions are met, the price is set to `amount * (5973 * 1.1)`.
Note that the prices are multiplied by 1.1 to include a 10% VAT (Value Added Tax).
5. If the `customerType` is 2 (representing a Public Service customer), the price is calculated as `amount *
9955 * 1.1`.
8. Finally, the function returns the calculated `price` multiplied by 1.1 again to include the VAT.
4.6. . Sort customers list by total of water bill payment:
1. We start by declaring and initializing the necessary arrays to store customer information, such as
`numberOfPeople`, `customerType`, `customerName`, `waterMeterLastMonth`, `waterMeterThisMonth`,
and `waterBill`. These arrays hold the respective data for each customer.
2. Inside the `Main` method, we iterate over the customers using a `for` loop. For each customer, we
calculate the amount of water consumption by subtracting the last month's reading from the current
month's reading. Then, we calculate the water bill using the `CalculateWaterBill` method, passing in the
amount of consumption, the number of people, and the customer type. The calculated bill is stored in
the `waterBill` array.
3. After calculating the water bills for all customers, we display the original information and the
calculated bills using a loop. This provides the initial output before sorting.
4. To sort the customers' list based on the water bill amount, we introduce an additional array called
`indices`. This array is initialized with the indices `[0, 1, 2]`, representing the indices of the customers in
the original arrays.
5. We use the `Array.Sort` method to sort the `indices` array based on the `waterBill` array's values. The
custom comparison function `(x, y) => waterBill[y].CompareTo(waterBill[x])` ensures that the sorting is
done in descending order. The `waterBill` array is used to compare the bill amounts, and the indices are
rearranged accordingly.
6. Finally, we iterate over the sorted `indices` array and retrieve the customer details from the original
arrays using the sorted index. We display the sorted customer information, including the name and bill
amount.
2. The `Array.IndexOf` method is then used to search for `searchName` within the `customerName`
array. This method iterates through the array and returns the index of the first occurrence of the
specified value (`searchName`). If the value is not found, it returns -1.
3. The returned index is stored in the `index` variable. If `index` is not equal to -1, it means that the
customer name was found in the array.
4. The code then checks if the customer was found by evaluating the condition `if (index != -1)`. If the
condition is true, it means the customer exists in the array and the code proceeds to display their
information.
5. Inside the `if` block, the customer's name is displayed using `Console.WriteLine("Name: " +
customerName[index])`. The corresponding water bill is retrieved from the `waterBill` array using the
same index: `double bill = waterBill[index]`.
6. Finally, the customer's bill amount is displayed using `Console.WriteLine("Bill: " + bill)`.
If the customer name entered by the user is not found in the `customerName` array (i.e., `index` is -1),
the code enters the `else` block. It displays a message indicating that the customer was not found with
`Console.WriteLine("Customer not found!")`.
2. Potential challenges
class Program
return volume;
When solving a problem or requirement, there are several steps that need to be followed, which include
the following:
a. Understanding the problem: This step involves analyzing the problem statement, identifying the inputs
and outputs, and understanding any constraints or special conditions.
b. Designing a solution: Once you have a clear understanding of the problem, you need to break it down
into smaller tasks or subproblems. Then, determine the appropriate algorithms or data structures to use
and plan the overall structure of the code.
c. Implementing the code: With the design in place, the actual coding process begins. This involves
writing the code according to the chosen programming language while following best practices and
coding conventions. You will typically need to write functions or methods, define variables, use control
structures (such as loops and conditionals), and implement any necessary data structures.
d. Testing and debugging: After the code is implemented, it needs to be thoroughly tested to ensure it
produces the expected results. This involves creating test cases that cover different scenarios and edge
cases, running the code with the test cases, and comparing the actual output with the expected output.
If any issues or bugs are found, they need to be debugged and fixed.
e. Refactoring and optimization: Once the code is working correctly, it can be improved through
refactoring and optimization. This involves making the code more efficient, readable, and maintainable.
It may include removing redundant code, optimizing algorithms, improving code organization, and
applying coding best practices.
Developers may face various challenges while writing code. Here are some of them:
a. Understanding the problem domain: Sometimes, the problem itself may be complex or poorly defined.
In such cases, developers need to put in additional effort to understand and analyze it properly. This may
involve researching the domain or consulting with domain experts to gain more insights.
b. Choosing the right approach: Selecting the most appropriate algorithms, data structures, and design
patterns to solve the problem efficiently and effectively can be challenging. Developers need to weigh
different options and consider trade-offs before choosing the best approach.
c. Handling edge cases: Dealing with edge cases, such as unexpected inputs or corner cases, can be
challenging. Developers need to thoroughly test the code and consider all possible scenarios to ensure it
handles them correctly.
d. Debugging and troubleshooting: When issues or bugs arise, debugging and troubleshooting can be
time-consuming and require solid problem-solving skills. It involves analyzing error messages, using
debugging tools, and systematically narrowing down the problem's source.
e. Ensuring code quality: Writing clean, maintainable, and efficient code is crucial for long-term success.
Developers should maintain good coding practices, follow style guidelines, and perform code reviews to
ensure code quality.
f. Managing complexity: As codebases grow larger and more complex, managing complexity becomes a
challenge. It requires effective code organization, modularization, and applying software engineering
principles to keep the codebase manageable and understandable.
g. Documentation and communication: Clearly documenting the code and communicating its
functionalities and usage can be challenging but is essential for collaboration and future maintenance. It
involves writing clear comments, providing meaningful variable and function names, and documenting
APIs or interfaces.
By being aware of these potential challenges and following best practices, developers can navigate
through the process of writing code more effectively and produce high-quality solutions.