Click here to Skip to main content
15,946,988 members
Please Sign up or sign in to vote.
1.00/5 (6 votes)
See more:
Implement a generic method, Search, that searches the specified element within an array using linear search algorithm. Method Search should compare the value with each element in its array parameter until the value is found or until the end of the array is reached. If the value is found, return its location in the array; otherwise return -1

What I have tried:

I am trying but not get the answer
Posted
Updated 6-Jun-23 6:39am
Comments
CPallini 31-May-23 7:34am    
What exactly did you try?
PIEBALDconsult 31-May-23 16:36pm    
Uhhh... that's already built into the .net framework, sooo... maaaybe just read the documentation perhaps?

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
C#
using System.Collections.Generic;
.
.
.
public static int LinearSearch<T>(T[] array, T item) // O(n)
{
    for (int i = 0; i < array.Length; i++)
        /* array[i] == item */
        if (Comparer<T>.Default.Compare(array[i], item) == 0)
            return i;

    return -1; // Not found
}

For other different Algorithms with Generics in C# see here
 
Share this answer
 
v2
Comments
Dave Kreskowiak 31-May-23 8:00am    
What is it with people who do other people's homework for them? You're not helping them. The point of the assignments is to think about the problems. You cannot do that for them!
jekin77 31-May-23 8:10am    
I am not a teacher to teach. It's better to learn from the mistakes of others than your own. And when someone asks for help, what prevents this person from helping. IMHO
jekin77 31-May-23 8:34am    
I repeat - I AM NOT A TEACHER. I just helped. And if your ass explodes from the fact that someone has a different opinion than you, then these are your personal problems. And don't pass them on to others. PS - and I myself learned a lot of things by reading someone else's code and delving into solutions to various problems.
Dave Kreskowiak 31-May-23 8:48am    
"I just helped."
That's the point! You THINK you helped, but you didn't. Why? Because
"I AM NOT A TEACHER"
Think about it. If asked to explain the code, do you think the OP is going to have any clue what that code does or why it works? NO, they're not! That's a fail!
OriginalGriff 31-May-23 10:32am    
Why do you think you helped, if you aren't going to teach?
There is an old saying: "Give a man a fish, you feed him for a day. Teach a man to fish, you feed him for a lifetime" - that's what doing trivial homework exercises for lazy students does: gives him a fish. Come tomorrow he needs another fish, plus one for his wife and where do they come from?
Doing homework for students creates dependency, not solutions. It's just "today's fish" instead of "tomorrow's career".
I would do this:
C#
public static int
Search<T>
(
  System.Collections.Generic.IList<T> List
,
  T                                   Sought
)
{
  return ( List.IndexOf ( Sought ) ) ;
}

  int[] arr = new int[] { 0 , 1 , 2 , 3 } ;
  int i = Search ( arr , 42 ) ;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900