Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

Could I make any improvements to my first Average Calculator? (C#)

0 votes
asked Apr 9 by Hunter Foust (120 points)
using System;
using System.IO;

namespace calculatorTest
{
    class Program
    {
        static void Main(string[] args)
        {
            double num01;
            double num02;
            double num03;
            double num04 = 3;
            
            Console.Write("Enter the 1st number: ");
            num01 = Convert.ToDouble(Console.ReadLine());
            
            Console.Write("Enter the 2nd number: ");
            num02 = Convert.ToDouble(Console.ReadLine());
            
            Console.Write("Enter the 3rd number: ");
            num03 = Convert.ToDouble(Console.ReadLine());
            
            double result = (num01 + num02 + num03) / num04;
            Console.WriteLine("The average of the 3 numbers is: " + result);
        }
    }
}

1 Answer

0 votes
answered Apr 9 by Peter Minarik (63,340 points)

A few tips:

  • Use variable names with meaning. Num01, num02, num03 kind of makes sense in this particular scenario, but having the count/number of items you sum calling num04 makes no sense at all. Calling it count or something similar would be a better choice
  • You should initialize your variables as soon as you declare them to avoid unknown values. For instance, you could declare and initialize your numbers in a single line, e.g.:
    double num01 = Convert.ToDouble(Console.ReadLine());
No major issues though. So good job! :)
commented Apr 10 by Hunter Foust (120 points)
edited Apr 10 by Hunter Foust
Thanks for the advice!

I completely understand the first tip and didn't really think about that, but now that you've said it, it makes much more sense to name the number of items I am summing to something other than the numbers that you input for the answer; it makes for better readability and knowing what the number is for.

I'm a little confused on what you mean by the 2nd tip as to where I would put this in the code. I tried putting it in the same line as the "num__ =  Convert.ToDouble..." but if I put "double" in front of the num = then it gives me 6 errors, 2 for each line.

If you could take a sample of my code and show me where to put the new line of code that would be awesome!

Edit I was thinking about the 1st tip and figured I could just get rid of the 4th number completely, I could just go to the line that finds the average

double result = (num01 + num02 + num03) / num04;

and exchange num04 (or "count" in renewed version) for just the number 3 instead of having a dedicated variable for it

double result = (num01 + num02 + num03) / 3;
commented Apr 10 by Peter Minarik (63,340 points)
Here's how you could simplify your code (including the assignment on declaration):

using System;

namespace calculatorTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter the 1st number: ");
            double num01 = Convert.ToDouble(Console.ReadLine());
            
            Console.Write("Enter the 2nd number: ");
            double num02 = Convert.ToDouble(Console.ReadLine());
            
            Console.Write("Enter the 3rd number: ");
            double num03 = Convert.ToDouble(Console.ReadLine());
            
            double result = (num01 + num02 + num03) / 3;
            Console.WriteLine("The average of the 3 numbers is: " + result);
        }
    }
}
commented Apr 10 by Peter Minarik (63,340 points)
If you'd like to use a loop, you can calculate the average for as many numbers as you'd like:

using System;

namespace calculatorTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter how many numbers would you like to sum: ");
            int count = Convert.ToInt32(Console.ReadLine());
            double sum = 0.0;
            for (int i = 0; i < count; i++)
            {
                Console.Write($"Enter the {i + 1}th number: "); // Feel free to add code here to use st, nd, rd and th appropriately
                sum += Convert.ToDouble(Console.ReadLine());
            }
            Console.WriteLine($"The average of the {count} numbers is: {sum / count}.");
        }
    }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...