0

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.

3
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by diGGity
Featured Replies
  • Maybe something like this: [CODE] 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; } [/CODE] Read More

1

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;
        }
1

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
      }
   }
}

Edited by thines01: n/a

0

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

0

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 :)

0

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?

0

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

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.