INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*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.

Jobs from Indeed

c# code program won't run

c# code program won't run

(OP)
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[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 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.

Nelviticus

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.

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:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!