I am working on a simple program to generate an array of values and perform some basic computations on them. I am having problems calling my "Mean" and "Median" methods from the Main method. I've attached the compiler error below. Thank you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class arraymod
{
static void Main(string[] args)
{
int[] values;
values = new int[10];
Random rand = new Random();
for (int i = 0; i < 10; i++)
{
values[i] = rand.Next(1,20);
}
Array.Sort(values);
for (int i = 0; i < 10; i++)
{
Console.WriteLine(values[i]);
}
Console.WriteLine("Min: {0}", values.Min());
Console.WriteLine("Max: {0}", values.Max());
Console.WriteLine("Sum: {0}", values.Sum());
Console.WriteLine("Mean: {0}", values.Mean()); //program fails here
Console.WriteLine("Median: {0}", values.Median());
}
public int Mean(int[] arr)
{
int valuesMean = arr.Sum()/arr.Count();
return valuesMean;
}
public int Median(int[] arr)
{
int valuesMedian = (arr.Max()+ arr.Min())/2;
return valuesMedian;
}
}
}
arraymod.cs(29,42):
error CS1061: 'System.Array' does not contain a definition for 'Mean' and no extension method 'Mean' accepting a first argument of
type 'System.Array' could be found (are you missing a using directive or an assembly reference?)
It's not a extension method. You need to make methods static and call them:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class arraymod
{
static void Main( string[] args )
{
int[] values;
values = new int[ 10 ];
Random rand = new Random();
for( int i = 0 ; i < 10 ; i++ )
{
values[ i ] = rand.Next( 1 , 20 );
}
Array.Sort( values );
for( int i = 0 ; i < 10 ; i++ )
{
Console.WriteLine( values[ i ] );
}
Console.WriteLine( "Min: {0}" , values.Min() );
Console.WriteLine( "Max: {0}" , values.Max() );
Console.WriteLine( "Sum: {0}" , values.Sum() );
Console.WriteLine( "Mean: {0}" , Mean( values ) ); //call
Console.WriteLine( "Median: {0}" , Median( values ) );//call
}
//make method "Mean" static
public static int Mean( int[] arr )
{
int valuesMean = arr.Sum() / arr.Count();
return valuesMean;
}
//make method "Median" static
public static int Median( int[] arr )
{
int valuesMedian = ( arr.Max() + arr.Min() ) / 2;
return valuesMedian;
}
}
}
Or use extension method:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
static class arraymod
{
static void Main( string[] args )
{
int[] values;
values = new int[ 10 ];
Random rand = new Random();
for( int i = 0 ; i < 10 ; i++ )
{
values[ i ] = rand.Next( 1 , 20 );
}
Array.Sort( values );
for( int i = 0 ; i < 10 ; i++ )
{
Console.WriteLine( values[ i ] );
}
Console.WriteLine( "Min: {0}" , values.Min() );
Console.WriteLine( "Max: {0}" , values.Max() );
Console.WriteLine( "Sum: {0}" , values.Sum() );
Console.WriteLine( "Mean: {0}" , values.Mean() ); //call
Console.WriteLine( "Median: {0}" , values.Median() );//call
}
//use extension method
public static int Mean( this int[] arr )
{
int valuesMean = arr.Sum() / arr.Count();
return valuesMean;
}
//use extension method
public static int Median( this int[] arr )
{
int valuesMedian = ( arr.Max() + arr.Min() ) / 2;
return valuesMedian;
}
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments