1,105,456 Community Members

Write a method find ()

Member Avatar
diGGity
Newbie Poster
10 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
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;
        }
Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
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
      }
   }
}
Member Avatar
diGGity
Newbie Poster
10 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
diGGity
Newbie Poster
10 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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 :)

Member Avatar
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 557 [?]
Q&As Helped to Solve: 489 [?]
Skill Endorsements: 21 [?]
 
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?

Member Avatar
diGGity
Newbie Poster
10 posts since Mar 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article