Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


c# code program won't run

c# code program won't run

c# code program won't run


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[i] - FirstArray[j]))

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

Console.WriteLine("Minimum = {0}", Minimum);
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;


RE: c# code program won't run

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

"Trying is the first step to failure..." - Homer

RE: c# code program won't run

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.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close