Hello! Today, we will discuss an algorithm problem related to calculating the average for those preparing for coding tests with C#. Many companies use algorithm problems when hiring developers, so it’s important to prepare well. In this lecture, we will define the problem, design the algorithm, implement the code in C#, and conduct performance analysis.
Problem Definition
The problem is to calculate the average of a given integer array. The array consists of various positive integers, and the final result should be expressed up to two decimal places. The method for calculating the average is as follows:
Add all the elements of the given array and divide by the number of elements to find the average.
Input Format
- Length N (1 ≤ N ≤ 1000)
- Integer array A = [a1, a2, …, aN] (1 ≤ ai ≤ 10,000)
Output Format
- Average value (up to two decimal places)
Problem Examples
Example 1
Input: 5, array [10, 20, 30, 40, 50]
Output: 30.00
Example 2
Input: 3, array [5, 15, 25]
Output: 15.00
Algorithm Design
The algorithm to solve this problem is very simple. First, we sum all elements of the given array and then divide the sum by the number of elements in the array to calculate the average. To maintain two decimal places, we will use the Math.Round method.
Algorithm Steps:
- Receive an integer array.
- Calculate the total sum of the array.
- Divide the total sum by the number of elements (N) to find the average.
- Round the average to two decimal places and return it.
C# Code Implementation
Now, let’s implement the C# code based on the above algorithm.
using System;
class AverageCalculator
{
public static void Main(string[] args)
{
// Get input
Console.Write("Please enter the number of integers: ");
int N = int.Parse(Console.ReadLine());
int[] numbers = new int[N];
Console.WriteLine("Please enter the integer array:");
for (int i = 0; i < N; i++)
{
numbers[i] = int.Parse(Console.ReadLine());
}
double average = CalculateAverage(numbers);
Console.WriteLine($"Average: {average:F2}"); // Output up to two decimal places
}
private static double CalculateAverage(int[] array)
{
double sum = 0;
foreach (int number in array)
{
sum += number; // Calculate total sum of the array
}
return Math.Round(sum / array.Length, 2); // Round the average to two decimal places
}
}
Code Explanation
The above C# code has the following structure:
- It receives the number of integers and the array elements from the user.
- It calculates the average in the CalculateAverage method.
- Finally, it prints the average up to two decimal places.
Performance Analysis
The time complexity of this problem is O(N). This is because it visits each element of the array only once. The space complexity is O(1) as it uses very little additional storage space. Therefore, this algorithm continues to work effectively even as the input data size increases.
Conclusion
Through this lecture, we learned how to solve the average calculation problem using C#. Since this is a commonly occurring problem in coding tests, it is important to practice thoroughly and ensure that it works correctly with various inputs. Keep solving more algorithm problems in the future!