Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 928567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:01:58+00:00 2026-05-15T20:01:58+00:00

I’m trying to create a program where the size of array index and its

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:01:58+00:00Added an answer on May 15, 2026 at 8:01 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.