We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,363 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Write a method find ()

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
4 Hours
Discussion Span
1 Year Ago
Last Updated
7
Views
diGGity
Newbie Poster
10 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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;
        }
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13

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
      }
   }
}
thines01
Postaholic
Team Colleague
2,433 posts since Oct 2009
Reputation Points: 447
Solved Threads: 408
Skill Endorsements: 7

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

diGGity
Newbie Poster
10 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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 :)

diGGity
Newbie Poster
10 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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?

Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13

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

diGGity
Newbie Poster
10 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0755 seconds using 2.7MB