Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 428
  • Last Modified:

Search Algorithm C#

Experts,

Platform : C# . NET VS 2008 3.5

I`m trying to implement a search algorithm.

I have list of doubles and a list of objects of a certain type. I`m trying to find if the values in the list of doubles match the objects in and around a precision value, if they do, I want the Index of the object.

Now if the value is not present I want to return a null value. With the code I have now, I always return a value - that`s where the issue is.



//TimeVals is the List of Doubles. Sorted/
//AObjList is the List of Objects of a certain class
//FmZero = 0.0001 This is the precision value
   
 for (i = 0; i < TimeVals.Count - 1; i++)
                        {
                            Ind = GetInd(TimeVals[i], AObjList, ref Ind);
Console.WriteLine(Ind);
                        }

   private int? GetInd(double Time, List<Obj> LObj, ref int? Ind)
        {
            int Head = 0;
            int Tail = LObj.Count - 1;

            if (Time < LObj[(int)Ind].Time)
            {
                while ((Ind != Head) && Time < (LObj[(int)Ind].Time - GP.FmZero))
                {
                    Ind = Ind - 1;
                }
            }
            else
            {
                while ((Ind != Tail) && Time >= (LTrj[(int)Ind].Time - GP.FmZero))
                {
                    Ind = Ind + 1;
                }
            }

           return Ind;
        }

Open in new window

0
San24
Asked:
San24
  • 5
  • 3
  • 2
  • +1
3 Solutions
 
käµfm³d 👽Commented:
You've denoted Ind to be a reference parameter by your use of the ref keyword. ref expects that its corresponding parameter be initialized prior to passing it to the function. How are you initializing the Ind variable prior to line 7?
0
 
San24Author Commented:
@Kaufmed - I start with Ind=0;

I get what you are trying to say, there is no point in returning a null if I`m using the ref keyword. It would need to be initialized.

Cause initially I was thinking I could return a null if the value was not found, and then do my calculations -


for (i = 0; i < TimeVals.Count - 1; i++)
                        {
                            Ind = GetInd(TimeVals[i], AObjList, ref Ind);
if(Ind!=null){
//Calculations here
Console.WriteLine(Ind);
}
                        }

Open in new window

0
 
Jacques Bourgeois (James Burger)PresidentCommented:
kaufmed has the problem pinpointed.

It is never a good ideas anyway to pass an int by reference, since it involves boxing, a memory manipulation that is not good for performance.

And since you are returning Ind at the end of the method, you do not need the ref anyway. Simply remove the ref.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
San24Author Commented:
@JamesBurger - Makes sense.  How do I return a null if the value is not found? Right now Ind always has a value.
0
 
käµfm³d 👽Commented:
You declared the return value to be of type int?, or Nullable<int>. You can simply return null.

i.e.

return null;

Open in new window

0
 
San24Author Commented:
Yes, but where would I use that in the code. The way GetInd function is written it always returns a value.
For example where would I set the bool Found? I`m guessing I`ll have to rewrite the function is a different way.
private int? GetTrj(double Time, List<Trajectory> LTrj, int? Ind)
        {
            int Head = 0;
            int Tail = LTrj.Count - 1;
            bool Found = false;
            
            if (Time < LTrj[(int)Ind].Time)
            {
                while ((Ind != Head) && Time < (LTrj[(int)Ind].Time - GP.FmZero))
                {
                    Ind = Ind - 1;
                }
            }
            else
            {
                while ((Ind != Tail) && Time >= (LTrj[(int)Ind].Time - GP.FmZero))
                {
                    Ind = Ind + 1;
                }
            }

            return (Found == false ? null : Ind);
        }

Open in new window

0
 
Jacques Bourgeois (James Burger)PresidentCommented:
Nullable are useful, specially with databases. If they suit you, go for it.

But a regular int is easier to work with and takes up less ressources, so when it is possible, there is an alternative: return a set value that could not be returned if something was found, and treat this value as a null.

With integers, I often use int.MinValue as "my" null. I usulally do not expect -2147483648 as a value for one of my integers, so it is easy for me to interpret that value as being null.
0
 
käµfm³d 👽Commented:
Structurally, line 22 is fine. Your problem, however, is that you never update the value of Found anywhere in that function. It will always be false. You need to change your logic to update the value of Found.
0
 
San24Author Commented:
@Kaufmed - Thats what I need help with, I want to know where I can set the Found value. A sample code would be greatly appreciated.
0
 
San24Author Commented:
I have it working now. What do you guys think about this.
int i = 0;
                        int? Ind = 0;
                        int? SInd = 0;                    //Begin search at this index
                        double APos = 0;
                        int EInd = 0;                     //Exist Index. Increment only if the the given Time value was found.

                        for (i = 0; i < TimeCnt - 1; i++)
                        {
                            Ind = GetTrj(TimeVals[i], ATrj, SInd);

                            if (Ind != null)
                            {
                                APos = ATrj[(int)Ind].Pos;
                                TableGV[a, i].Value = APos;
                                SInd = Ind;
                                EInd++;
                            }
                            else
                            {
                                Ind = SInd;
                                TableGV[a, i].Value = null;
                            }
                        }



        private int? GetTrj(double Time, List<Trajectory> LTrj, int? SInd)
        {
            int Head = 0;
            int Tail = LTrj.Count - 1;
            bool Found = false;
            int Ind = 0;

            if (Time < LTrj[(int)SInd].Time)
            {
                for (Ind = (int)SInd; Ind > Head; Ind--)
                {
                    if ((Time <= (LTrj[Ind].Time + GP.FmZero)) && (Time >= (LTrj[Ind].Time - GP.FmZero)))
                    {
                        Found = true;
                        break;
                    }
                }
            }
            else
            {
                for (Ind = (int)SInd; Ind < Tail; Ind++)
                {
                    if ((Time <= (LTrj[Ind].Time + GP.FmZero)) && (Time >= (LTrj[Ind].Time - GP.FmZero)))
                    {
                        Found = true;
                        break;
                    }
                }
            }

            return (Found == false ? null : (int?)Ind);
        }

Open in new window

0
 
mlmccCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now