Question

In: Computer Science

There are at least five (probably more) places in the attached C# program code that could...

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.

  1. List five problems you found with the code (Put your list either in comments within the code or in a separate Word or text document), and then optimize the code in the attached code below to make it as efficient as possible. The program output must not be affected by your changes. To the user it should still do everything it did before your changes provided the user enters valid data.

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;
}
}
}

Solutions

Expert Solution

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

Related Solutions

C++ PROGRAM Using the attached C++ code (Visual Studio project), 1) implement a CoffeeMakerFactory class that...
C++ PROGRAM Using the attached C++ code (Visual Studio project), 1) implement a CoffeeMakerFactory class that prompts the user to select a type of coffee she likes and 2) returns the object of what she selected to the console. #include "stdafx.h" #include <iostream> using namespace std; // Product from which the concrete products will inherit from class Coffee { protected:    char _type[15]; public:    Coffee()    {    }    char *getType()    {        return _type;   ...
what is the best code to construct a C++ program that finds a five digit number;...
what is the best code to construct a C++ program that finds a five digit number; This number should reverses the order of its digits when multiplied by four. Also, how many five digits numbers are there in which the sum of the digits is even.
C++ Plan and code a program utilizing one or more repetition structures to solve the following...
C++ Plan and code a program utilizing one or more repetition structures to solve the following problem: Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. A number is a multiple of 9 if the sum of its digits is evenly divisible by 9. Determine if a number is a multiple of 9 by adding together the individual digits of the number and determining if the...
Having Trouble with this C++ assignment THe places that are marked // TODO is where code...
Having Trouble with this C++ assignment THe places that are marked // TODO is where code should be filled in at Header (myQueue.h) #ifndef _MYQUEUE_H_ #define _MYQUEUE_H_ using namespace std; template class myQueue { public:    myQueue(int maxSz);    ~myQueue();    void enqueue(T item);    T dequeue(); int currentSize(); bool isEmpty(); bool isFull(); private:    T *contents; /*Dynamic initiate (C++ keyword new) the holder array*/    int front,rear; /*Index in the array of the front and rear element*/    int...
Lab 11 C++ Download the attached program and run it - as is. It will prompt...
Lab 11 C++ Download the attached program and run it - as is. It will prompt for a Fibonacci number then calculate the result using a recursive algorithm. Enter a number less then 40 unless you want to wait for a very long time. Find a number that takes between 10 and 20 seconds. (10,000 - 20,000 milliseconds). Modify the program to use the static array of values to "remember" previous results. Then when the function is called again with...
Write a program that will prompt for a word with at least five letters in it...
Write a program that will prompt for a word with at least five letters in it (if not, keep asking for input). Then evaluate the word to produce a count of all the vowels in the word. Sample output appears below. Enter a word at least 5 characters long:<SPC>cat<ENTER> Enter a word at least 5 characters long:<SPC>dog<ENTER> Enter a word at least 5 characters long:<SPC>concatenate<ENTER> Letter Counts ========= a: 2 e: 2 i: 0 o: 1 u: 0
Write a complete C++ program that at least consists of the main() function and at least...
Write a complete C++ program that at least consists of the main() function and at least two recursive functions. The first function has no return value and can be named printPrime(). It prints first n prime numbers with proper prompt. Note that number 1 is not regarded as a prime number. We assume the first prime number is 2. The printout should start from 2. The prototype of the recursive function should be void printPrime(int n); The algorithm of printPrime()...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
C++ program. Please explain how the code resulted in the output. The code and output is...
C++ program. Please explain how the code resulted in the output. The code and output is listed below. Code: #include <iostream> #include <string> using namespace std; int f(int& a, int b) {    int tmp = a;    a = b;    if (tmp == 0) { cout << tmp << ' ' << a << ' ' << b << endl; }    b = tmp;    return b;    return a; } int main() {    int a...
Need C++ code for following with a document describing the code as well: Write a program...
Need C++ code for following with a document describing the code as well: Write a program to implement the game of Tic Tac Toe Four. The game is played on a 4 × 4 chessboard, within 2 players. The player who is playing "X" always goes first. Players alternate placing Xs and Os on the board until either one player has four in a row, horizontally, vertically, or diagonally; or all 16 squares are filled(which is a draw). The program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT