how to locate numbers in a list if u input the numbers??

titotitus

Member
Joined
Feb 12, 2020
Messages
14
Programming Experience
Beginner
The exercise template contains a base that reads numbers from the user and adds them to a list. Reading is stopped once the user enters the number -1.

Expand the program to ask for a start and end indices once it has finished asking for numbers. After this the program shall prints all the numbers in the list that fall in the specified range (between the indices given by the user, inclusive). You may assume that the user gives indices that match some numbers in the list.

> 72
> 2
> 8
> 11
> -1
From where?
> 1
Where to?
> 9
2
8

public static void Main(string[] args)
{
List<int> list = new List<int>();
while (true)
{
int input = Convert.ToInt32(Console.ReadLine());
if (input == -1)
{
break;
}
list.Add(input);
}


Console.WriteLine("From where");
int number = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("To where");
int number2 = Convert.ToInt32(Console.ReadLine());


for (int i = 0; i < list.Count; i++)

Console.WriteLine(i);
 
Last edited:
Okay, so you told us what your exercise is. Are you going to tell us what problem you are encountering?

Are you going to show us your code? It looks like your instructor already provides you all the starting code and you just need to modify it to support the new functionality.
 
Okay, so you told us what your exercise is. Are you going to tell us what problem you are encountering?

Are you going to show us your code? It looks like your instructor already provides you all the starting code and you just need to modify it to support the new functionality.
public static void Main(string[] args)
{
List<int> list = new List<int>();
while (true)
{
int input = Convert.ToInt32(Console.ReadLine());
if (input == -1)
{
break;
}
list.Add(input);
}


Console.WriteLine("From where");
int number = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("To where");
int number2 = Convert.ToInt32(Console.ReadLine());


for (int i = 0; i < list.Count; i++)

Console.WriteLine(i);
 
Please learn to use the code tags. On the toolbar, click on the down arrow beside the 3 dots, and select "Code".

Did you take time to review your class notes on for loops?

If you look at your current code, you have this:
C#:
for (int i = 0; i < list.Count; i++)
    Console.WriteLine(i);

So that ranges from 0 to list.Count - 1. 0 is the lower bound, and list.Count is the upper bound.

Well, now you have number and number2. How woud you use them to set the bounds of your for loop?
 
In this code you had no trouble using the input values...
C#:
int input = Convert.ToInt32(Console.ReadLine());
if (input == -1)
{
    break;
}
list.Add(input);

If you are having this much trouble with the assignment, I highly recommend talking to your teacher and/or their teaching assistant. They should be able to help you get caught up on concepts that you should know by the time you tackle this particular exercise.
 
C#:
    public static void Main(string[] args)
    {
      List<int> list = new List<int>();
            while (true)
            {
                int input = Convert.ToInt32(Console.ReadLine());
                if (input == -1)
                {
                    break;
                }
                list.Add(input);

            }
            
                Console.WriteLine("From where");
                int number = Convert.ToInt32(Console.ReadLine());

          
                 Console.WriteLine("To where");
                int number2 = Convert.ToInt32(Console.ReadLine());

            while (number < number2)
            {
                Console.WriteLine(number + number2 + 1);
             number2++;
            }

        }
    }
}



i tried this and i am getting endless loop
 
Try it simulating that on pen and paper. Each time you loop, you are bumping up your upper end. So number never will be greater than or equal to number2.
 
Blindly accepting user input is your first mistake. Convert.ToInt32(Console.ReadLine());

Your second mistake is using while loops where they aren't needed, or also using the wrong type of loop. Why are you using while loops?

The literature of which this exercise comes from must be some of that old trash they are still teaching in schools today, and that's clearly not your fault for sure.
 
C#:
 public static void Main(string[] args)
        {
            List<int> list = new List<int>();
            while (true)
            {
                int input = Convert.ToInt32(Console.ReadLine());
                if (input == -1)
                {
                    break;
                }
                list.Add(input);

            }
            Console.WriteLine("from where");
            int number = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("To where");
            int number2 = Convert.ToInt32(Console.ReadLine());

            for (int i = 0; i < list.Count + 1; i++)
            {
                if (i >= number && i <= number2)
                {
                    Console.WriteLine(i);

                }
            }
        }


this worked!!!!
 
Almost there.

Next try changing line 20 so that you don't need the if statement anymore.

After that, re-read your assignment requirements. I think you are supposed to print out the list elements at those indexes, not the indexes.
 

Latest posts

Back
Top Bottom