Reputation: 8705
Here's my try at some basic C# programming. The program is meant to ask the user for a size of an array, then to fill the array, print out the array and finally find the average of the numbers they used to fill the array. The program currently doesn't compile. This is my first time doing this without any sort of reference book so can someone help explain to me what I'm missing? Thanks. EDIT: All of the program works except the part about finding the average of the numbers in the array. Also, if there are any silly mistakes that aren't prudent for production-level coding please let me know.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _9_21_Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("enter the amount of numbers you would like to find the average of: ");
int arraylength = Int32.Parse(Console.ReadLine());
int[] AverageArray = new int[arraylength];
//filling the array with user input
for (int i = 0; i < AverageArray.Length; i++)
{
Console.Write("enter the numbers you wish to find the average for: ");
AverageArray[i] = Int32.Parse(Console.ReadLine());
}
//printing out the array
Console.WriteLine("here is your array: ");
for(int i=0; i < AverageArray.Length; i++)
{
Console.WriteLine(AverageArray[i]);
}
Console.WriteLine(FindAverage);
}
}
//Method to find the average is another class for learning porpoises
class Calcs
{
public static double FindAverage(int[] averageNumbers);
int arraySum=0;
for(int i =0; i < averageNumbers.Length; int i++)
arraysum+=arraysum;
return double average = arraysum/averageNumbers.Length;
}
}
Upvotes: 3
Views: 5054
Reputation: 166566
Change
Console.WriteLine(FindAverage);
to
Console.WriteLine(Calcs.FindAverage(AverageArray));
and
class Calcs
{
public static double FindAverage(int[] averageNumbers);
int arraySum=0;
for(int i =0; i < averageNumbers.Length; int i++)
arraysum+=arraysum;
return double average = arraysum/averageNumbers.Length;
}
to
class Calcs
{
public static double FindAverage(int[] averageNumbers)
{
int arraySum=0;
for(int i =0; i < averageNumbers.Length; i++)
arraySum+=averageNumbers[i];
return arraySum/averageNumbers.Length;
}
}
If you wish to discuss the concepts, we will be more than willing to help.
Upvotes: 1
Reputation: 564851
There are a couple of errors here:
Your for loop shouldn't specify "int" twice, and should add in the appropriate number:
// for(int i =0; i < averageNumbers.Length; int i++)
// Remove the extra "int", and
for(int i =0; i < averageNumbers.Length; i++)
{
// Add in averageNumbers[i], not arraysum
arraysum += averageNumbers[i];
}
You also need to remove the semi-colon in Calcs.FindAverage
, and make it a method wrapped in braces:
class Calcs
{
public static double FindAverage(int[] averageNumbers)
{ // Add brace...
return //...
} // Close brace
}
Your return statement doesn't need the variable declaration:
// return double average = arraysum/averageNumbers.Length;
// Just return the value
return arraysum/averageNumbers.Length;
Also, when you call FindAverage
, you need to actually call the method, ie:
Console.WriteLine(Calc.FindAverage(AverageArray));
Note that, normally, using .NET 4, you could just use existing functionality. Instead of writing your own method, you can write:
Console.WriteLine(AverageArray.Average());
This is using Enumerable.Average, which is an extension method that works on any numeric IEnumerable<T>
, such as IEnumerable<int>
(your array).
Note that, when you compile this inside of Visual Studio, the errors window will list out the errors in your code, with descriptions. You can double click on each error, and it will take you directly to the line causing the error. That should make it fairly straightforward to figure out how to fix your code, one error at a time.
Upvotes: 4
Reputation: 16651
static class Calc {
public static double FindAverage(int[] numbers) {
int sum = 0;
foreach (int number in numbers) {
sum += number;
}
return sum / numbers.Length;
}
}
You can also do this with Linq easily, but if you're learning it's better to stick with the language basics.
Upvotes: 0
Reputation: 32525
If you are using the .NET 3.5+ framework then you should use the LINQ extension method Enumerable.Average(IEnumerable<Int32>)
instead of reinventing the functionality :)
Upvotes: 1