In: Computer Science
There are at least five (probably more) places in the attached C# program code that could be optimized. Find at least five things in the Program.cs file that can be changed to make the program as efficient as possible.
using System;
namespace COMS_340_L4A
{
class Program
{
static void Main(string[] args)
{
Console.Write("How many doughnuts do you need for the first
convention? ");
int.TryParse(Console.ReadLine(), out int total1);
Console.Write("How many doughnuts do you need for the second
convention? ");
int.TryParse(Console.ReadLine(), out int total2);
int totalDonuts = Calculator.CalculateSum(new int[] { total1, total2 });
Console.WriteLine($"{ totalDonuts } doughnuts is {
Calculator.ConvertToDozen(totalDonuts)} dozen.");
Console.WriteLine("Press Enter to quit...");
Console.ReadLine();
}
}
static class Calculator
{
static int DOZEN = 12;
public static int CalculateSum(int[] args)
{
int return_value = 0;
foreach (int i in args)
{
return_value += i;
}
return return_value;
}
public static int ConvertToDozen(int total)
{
int dozen = DOZEN;
return total / dozen;
}
}
}
Made a few changes but still works the same, there is a slight improvement in the efficiency of the code
using System;
namespace COMS_340_L4A
{
class Program
{
static void Main(string[] args)
{
Console.Write("How many doughnuts do you need for the first convention? ");
int.TryParse(Console.ReadLine(), out int total1);
Console.Write("How many doughnuts do you need for the second convention? ");
int.TryParse(Console.ReadLine(), out int total2);
int totalDonuts = Calculator.CalculateSum(new int[] { total1, total2 });
Console.WriteLine($"{ totalDonuts } doughnuts is { Calculator.ConvertToDozen(totalDonuts)} dozen.");
//Console.WriteLine("Press Enter to quit...");
//Console.ReadLine(); commented these as the program will be terminated after completion
}
}
static class Calculator
{ //removed static int DOZEN=12;
public static int CalculateSum(int[] args)
{
int return_value = 0;
foreach (int i in args)
{
return_value += i;
}
return return_value;
}
public static int ConvertToDozen(int total)
{
//removed int dozen=DOZEN;
return total / 12;
//instead directly given the value as 12
}
}
}