mercredi 29 octobre 2014

Why is this C# program which uses a "function" not working?


Vote count:

-1




I've made a small C# program that takes an array with some numbers (float) inside and that tells how many numbers in the array are not in the interval ]a,b] I succeeded. Here is the program:



using System;

namespace Exercice_1
{
class Program
{
public static void Main(string[] args)
{

Console.WriteLine("a = ?");

float a = float.Parse(Console.ReadLine());

Console.WriteLine("b = ?");

float b = float.Parse(Console.ReadLine());

float[] t = new float[50];

for(int i=0; i<t.Length; i++)
{
t[i]=4*i;
}

int k = 0;

for(int i=0; i<t.Length; i++)
{
if(t[i] <= a || t[i] > b)
{
k++;
}
}

Console.Write(""+k);

Console.ReadKey(true);

}
}
}


But then I wanted to try making that same program but with using a "function".

But I did not succeed. Here is the program:



using System;

namespace Exercice_2
{

class Program
{
public static void Main(string[] args)
{

Console.WriteLine("a = ?");

float a = float.Parse(Console.ReadLine());

Console.WriteLine("b = ?");

float b = float.Parse(Console.ReadLine());

float[] t = new float[50];

for(int i=0; i<t.Length; i++)
{
t[i]=4*i;
}

Console.Write(""+count(float[] t, float a, float b));

Console.ReadKey(true);

}

static int count(float[] u, float x, float y)
{
int k = 0;

for(int i=0; i<u.Length; i++)
{
if(u[i] <= x || u[i] > y)
{
k++;
}
}

return k;
}


}
}


I've also tried putting the function outside "class Program", but it didn't work either.


It's the first time I'm trying to use a "function" so I probably made some obvious beginner mistakes...


Here are all the "errors" which SharpDevelop is pointing out: http://ift.tt/1tMONnD



asked 1 min ago







Why is this C# program which uses a "function" not working?

Aucun commentaire:

Enregistrer un commentaire