tags:

views:

56

answers:

4

I'm trying to create a program where the size of array index and its elements are from user input. And then the program will prompt the user to search for a specific element and will display where it is.

I've already come up with a code:

using System;

namespace ConsoleApplication1
{

class Program
{

  public static void Main(String [] args)
  {
    int a;
    Console.WriteLine("Enter size of index:");
    a= int.Parse(Console.ReadLine());
    int [] index = new int [a];
    for (int i=0; i<index.Length;i++)
    {
      Console.WriteLine("Enter number:");
      index[i]=int.Parse(Console.ReadLine());
    }

  }
}
}

The problem with this is that I can't display the numbers entered and I don't have any idea how to search for an array element. I'm thinking of using if statement.

Another thing, after entering the elements the program should display the numbers like Number 0 : 1

Is this correct: Console.WriteLine("Number"+index[a]+":"+index[i]);?

And where should I put the statement? after the for loop or within it?

+4  A: 

You're on the right track. Look carefully at how you stuffed values into the array, and you might find a clue about "how to search for an array element" with specific value. This is a core introductory algorithm, so no shortcuts! You need to find the answer on your own :-).

sfuqua
+1 for not giving it away!
TJB
A: 

Just try to break down what you've done and what you still need to do

Create a program where:

1) size of its array elements comes from user input (check)
2) array elements come from user input (check)
3) Prompt the user to search for a specific element (TODO)
4) Display where it is (TODO)

From the code you've written so far I would think you have most of what you need to do #3 & #4

An if statement may come into play when finding the location of the element the user specifies.

TJB
+2  A: 

You could use Array.IndexOf,

Alex M.
+2  A: 

What is the last line Console.WriteLine(index[i]);? It seems like you are using the loop variable outside the loop.

To display entered numbers (ie. if I understand well, the numbers in the array), you have just to walk through the array like this:

for (int i = 0; i < index.length; i++)
{
    Console.WriteLine(index[i]);
}

Since you want display numbers only after every number is entered, you may put this code only after finishing the loop where the user is entering the numbers:

// The user is entering the numbers (code copied from your question).
for (int i = 0; i < index.Length; i++)
{
    Console.WriteLine("Enter number: ");
    index[i] = int.Parse(Console.ReadLine());
}

// Now display the numbers entered.
for (int i = 0; i < index.length; i++)
{
    Console.WriteLine(index[i]);
}

// Finally, search for the element and display where it is.
int elementToSearchFor;
if (int.TryParse(Console.ReadLine(), out elementToSearchFor))
{
    // TODO: homework to do.
}

To search for a number, you can either walk through the array again and compare each element until finding a good one, or use Linq TakeWhile() method. (I suppose that your intent is not to use Linq, so I don't provide any further detail in this direction.)

MainMa
for (int i=0; i<index.Length;i++) { Console.WriteLine("Enter number:"); index[i]=int.Parse(Console.ReadLine()); Console.WriteLine(index[i]); * }That line surely displays the entered number but then I was prompted to enter a number again. How can I display the entered numbers after I completed entering all the numbers?
@unsub101: "How can I display the entered numbers after I completed entering all the numbers?" - see the sample code in my answer. It does exactly what you need.
MainMa
That's odd, because when I tried running the program, the number i entered was displayed but then it prompts me again to enter another one.
Where are you putting the loop? Probably you have embedded loops, when two loops are expected to go one after another: the first to capture values, the second to display them.
MainMa
Is that for loop inside the first loop? umm, like this? for (int i=0; i<index.Length;i++) { Console.WriteLine("Enter number:"); index[i]=int.Parse(Console.ReadLine()); for (int j = 0; j < index.length; j++){ Console.WriteLine(index[j]);} }
Yes. I edited my answer to be more easy-to-understand. By the way, I recommend using `TryParse` instead of `Parse` when dealing with user input. I imagine you might not be familiar with `TryParse` and `out` parameters, so I included an example.
MainMa
Thank you very much