Hi guys!

I really need help for my school task in c#

TASK:

Write a method find (), which locates in the field given element and returns the index of the first occurrence of this element.

/ / <a> representing a box and the <el> element we are looking for

static int find (int el, int [] a)
{
/ / INSERT HERE YOUR CODE

/ * Your function should not return -2, but rather
index found the item (if it is not found,
should return -1) * /

return -2;
}

Any suggestions or help will be appreciated.

Recommended Answers

All 6 Replies

Maybe something like this:

static int Find(int el, int[] a)
        {
            int index = -1;
            for (int i = 0; i < a.Length; i++)
            {
                if (a[i] == el)
                {
                    return a[i];
                }
            }
            return index;
        }

You need to find an integer inside of an integer array?
Is there anything you're NOT allowed to use?

using System;
using System.Linq;

namespace DW_417111_CS_CON
{
   class Program
   {
      static int find(int el, int[] a)
      {
         return a.ToList().IndexOf(el);
      }

      static void Main(string[] args)
      {
         int[] arr_int = new int[] { 5, 4, 6, 2, 9 };
         Console.WriteLine(find(7, arr_int)); // prints -1

         Console.WriteLine(find(6, arr_int)); // prints 2
      }
   }
}

Thanks alot Mitja! I hope that this will work :) You are from slovenia like me? :)

Those instructions which I wrote at the beginning, all the instructions I've get.The use of complex functions of programming libraries is not allowed :)

Thanks alot Mitja! I hope that this will work :) You are from slovenia like me? :)

yes :), Lj, but only live there.
So have you solved the issue?

Nice :) I'll know when the Assistant examined the task and give me the grade :S

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.