Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

c# code program won't run

Status
Not open for further replies.

Al1s0n

MIS
May 28, 2016
1
0
0
AU
Hi,

I'm trying to calculate the minimum distance between 2 closest in an array and I don't know why my program won't run - can someone please help?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exercise
{
public class Program
{
static int Main(string[] args)
{
int n = 0;
int[] FirstArray = new int[n];
Random r = new Random();

int Minimum = int.MaxValue;
for (int i = 0; i < n; i++)
{

for (int j = 0; j < n - 1; j++)
{

if (Minimum > Math.Abs(FirstArray - FirstArray[j]))
{

Minimum = Math.Abs(FirstArray - FirstArray[j]);
}
}

Console.WriteLine("Minimum = {0}", Minimum);
Console.Read();
return 0;
}
}

class Elements
{
public double Distance(Elements compar)
{


var x1 = Convert.ToDouble(Console.ReadLine());
var y1 = Convert.ToDouble(Console.ReadLine());

var x2 = Convert.ToDouble(Console.ReadLine());
var y2 = Convert.ToDouble(Console.ReadLine());

var finalResult = Distance(x1, x2, y1, y2);


}

private static double Distance(double x1, double x2, double y1, double y2)
{

var temp1 = Math.Pow((x2 - x1), 2);
var temp2 = Math.Pow((y2 - y1), 2);
var result = Math.Sqrt(temp1 + temp2);

return result;
}

}
}
}
 
Your "return 0" is inside the first for-loop for starters.

"Trying is the first step to failure..." - Homer
 
janni78 is right; also your 'Distance(Elements compar)' method doesn't return a value (not that it's ever used, but it'll stop things from compiling).

Move your 'return 0' line to after the loop and add a 'return finalResult' line to the end of your 'Distance(Elements compar)' method. Also, you'll want to set n to higher than zero otherwise your program will just return zero straight away.

Nelviticus
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top