Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.33/5 (6 votes)
See more:
C#
using System;
using System.Collections.Generic;
namespace imagetit
{
    public class softball
    {
        static void Main(string[] args)
        {
            Random numgen = new Random();
            int _throw = numgen.Next(80, 85);

            List<double> speeds = new List<double>() { _throw, _throw, _throw, _throw };
            speeds.Add(_throw);

            double result = 0.0;
            foreach (double speed in speeds)
            {
                result += _throw;
            }
            result = speeds.Count;

            Console.WriteLine($" The average throw is {speeds}");


        }
    }

} //2nd tab
sing System;
using System.Collections.Generic;
using System.Text;

namespace imagetit
{
    class baseball
    {
        public baseball()
        {
            Random numgen = new Random();
            int _throw = numgen.Next(90, 100);

            List<double> speeds = new List<double>() { _throw, _throw, _throw, _throw };
            speeds.Add(_throw);

            double result = 0.0;
            foreach (double speed in speeds)
            {
                result += _throw;
            }
            result = speeds.Count;

            Console.WriteLine($" The average softball throw is {speeds}");
        }
    }
}


What I have tried:

currently no errors in compiler, 1 week into programming , love it!
Posted
Updated 6-Aug-19 23:03pm
v2
Comments
Patrice T 6-Aug-19 15:23pm    
"1 week into programming , love it!"
Nice, but what is the question?
CHill60 6-Aug-19 19:12pm    
What is the question?
Richard MacCutchan 7-Aug-19 2:23am    
Average is total divided by the number of items.
George Swan 7-Aug-19 14:38pm    
An important point to remember about averages is that the sum of the averages equals the average of the sum. Say you are calculating the average of 4 numbers 1,2,3,4=10/4=2.5 and the next number is 5 what is the average of the five numbers? Well 2.5 is the average of 4/5 of the sum and 5 is the average of 1/5 of the sum. So the new average is (2.5*4/5)+(5*1/5)=3.The point about this is that you don't need a List to update the average you just need a count of the current total of numbers and the previous average. Of course, the easy way is just to keep a running total and divide by the count of the numbers

Maybe the compiler doesn't complain, but there are logical errors in your code.
For instance you generate just one random value and fill the list with such a single value.
Moreover, as Richard already noted, the average is 'total divided by number of items'.
 
Share this answer
 
Where do I start?
Let's go with simple stuff:
            List<double> speeds = new List<double>() { _throw, _throw, _throw, _throw };
...
            Console.WriteLine($" The average softball throw is {speeds}");
No, it isn't. speeds is a collection - specifically a List of items - and that isn't an average at all. So what it will print is always the same:
The average softball throw is System.Collections.Generic.List`1[System.Double]
Because the system doesn't how how you want the collection of items displayed, so it uses the name of the object type.
To print an average, you need to sum all the values, and divide that by the number of values you added together.
av = (1 + 2 + 3 + 4 + 5) / 5 = 3

Now let's get a bit more complicated:
Random numgen = new Random();
int _throw = numgen.Next(90, 100);

List<double> speeds = new List<double>() { _throw, _throw, _throw, _throw };
speeds.Add(_throw);
Won't add different values to your collection - it will add the same value 5 times.
When you do an assignment:
int _throw = numgen.Next(90, 100);
it assigns the current value to _throw, not "remembers what it was created by". To assign a set of random numbers 5 times, you need to actually call the Random.Next method five times:
Random numgen = new Random();
List<double> speeds = new List<double>();
speeds.Add(numgen.Next(90, 100));
speeds.Add(numgen.Next(90, 100));
speeds.Add(numgen.Next(90, 100));
speeds.Add(numgen.Next(90, 100));
speeds.Add(numgen.Next(90, 100));
Or use a loop:
Random numgen = new Random();
List<double> speeds = new List<double>();
for (int i = 0; i < 5; I++)
   {
   speeds.Add(numgen.Next(90, 100));
   }

Then we come to this bit:
foreach (double speed in speeds)
{
    result += _throw;
}
result = speeds.Count;
Which just throws away the result of the summation by overwriting it with the number of items count ...

I know you've only been at this a short while, but you are making a major mistake: you are jumping straight into code. Don't do that: sit down and think first, plan what you want to do, and then check & refine the plan until you are sure it will work. Then start coding and testing. Jumping right into code means that you are making it up as you go along, and that leads to errors like these!

Make a copy of that code, then delete it all and start again. When you get it working (and I strongly recommend you learn to use the debugger, it will show you exactly what your code is doing while it runs) compare the new code with this and you will see the difference!

Good luck!
 
Share this answer
 
Quote:
Currently 2 weeks in programming but I love it

Nice, but you didn't ask anything.
Just an advice: In order to help you to understand what you code do right or wrong, you should learn to use the debugger, it is a great learning tool.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900