Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi
I'm going to type in a number in the Console and then display all the numbers that are less than it in the numb array.

C#
int[] numb = { -9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 8, 9 };

            Console.Write("pick a number: ");
            int v = int.Parse(Console.ReadLine());
            int t = 0;
            for (int x = 0; x < numb.Length; x++)
            {
                if (v > numb[x])
                    t = numb[x];
            }
            Console.Write("Your num under is: " + t);
            
            Console.ReadLine();


This code only only displays one number. How do I change it to display the numbers less than the entered value?
Posted
Updated 7-Mar-13 8:12am
v2
Comments
Mike Meinz 7-Mar-13 14:09pm    
This looks like homework.

You should really do your homework yourself.

Here's a clue:
You are only writing out one number.
What would you change to have it write out all numbers that are selected?
Sergey Alexandrovich Kryukov 7-Mar-13 14:14pm    
What did you try?
It would be a shame just to write you an answer, as it's way too easy... :-)
If you don't do it by yourself eventually, how can you learn anything at all?
—SA
joshrduncan2012 7-Mar-13 14:43pm    
Smells like homework.

Btw, also smells like a foreach loop is necessary.

This is your homework, so I won't give you any code solution.

But...you are on the right lines.
All you have to do is work out what is going on, and what you want to happen.

At the moment, you have a loop which runs through each of the numbers in your array and checks if it is less than you entered number. If it is, it stores it in a variable, called "t". After the loop has finished, you print the value of "t". The problem is that if you want to print more than one value (and you do) should the print be outside the loop?

By the way, I don't know if you have met it yet (I'm going to assume you have) but this would be a lot easier to read (and write) if you used a foreach loop instead of a for - since you don't need to know where you are in teh array, why have a variable to look at where you are?
Without changing any code except the loop (i.e. not fixing your problem at all):
C#
int[] numb = { -9, -8, -7, -6, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 6, 8, 9 };

Console.Write("pick a number: ");
int v = int.Parse(Console.ReadLine());
int t = 0;
foreach (int n in numb)
{
    if (v > n)
    {
        t = n;
    }
}
Console.Write("Your num under is: " + t);

Console.ReadLine();
See how much easier it is to read?
The other change I made is to add curly brackets around the if statement. While you are getting started, it's a good idea to always use brackets, even if you don't need them - it can make your life a lot easier later when you have to make changes. Trust me on this: I spent hours trying to work out why my code didn't work only to find that if I'd put brackets in it would have been sooooo obvious...:laugh:

So, can you see what you have to do?
 
Share this answer
 
Comments
Jegan Thiyagesan 7-Mar-13 15:06pm    
Well presented answer!
t must be an array.
C#
int[] t;

Inside a loop you need to resize t array...

How to resize array in C#?[^]

;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900