10th number in array

ItsMitch

Member
Joined
Aug 6, 2020
Messages
15
Programming Experience
Beginner
Hi, how do i go about getting the 10th even number in a array the method assumes 2 is the first even number.the method doesn't print that number it only returns it
 
This sounds a lot like the sort of thing that you would be assigned as a homework exercise and it seems a lot like you're trying to get us to do your homework for you. Regardless, as with any programming problem, start by forgetting that it's a programming problem. How would you do it manually, i.e. with pen and paper. Work that out and formalise the steps into an algorithm that you could provide to anyone else to follow to get the same result. That takes no programming experience at all; just everyday logic and a bit of effort. Once you have a working algorithm, you can write code specifically to implement that algorithm. If the algorithm works and the code doesn't then the code doesn't implement the algorithm. Only when you are in the implementation stage and cannot work out the code to implement a specific step of the algorithm do you need to ask for help, at which point you can show what you have done so far.
 
That would imply that you have already done the algorithm part. Have you? If so, what's the algorithm? If not then you're not struggling with the code part because the code part is implementing the algorithm that you have already worked out. If you, like so many others, are trying to write the code without knowing what the code is supposed to do, i.e. not just the desired result but the steps to get there, then that is the reason that you're struggling. Why would you expect to be able to write code without knowing what it has to do? So, do you know what the code has to do? If so, tell us what that is. If not, that's what you need to be working out before considering writing any code.
 
To find the even numbers i said:
for ( i = 0; i < arr.length; i++)
{
If (arr[ i ] %2 == 0)
}
But i dont know how to write so that i get the 10th number from the even number i received
 
In your algorithm you should count even numbers until you get to 10. Use a variable to keep track of count.

I would do this with Linq:
pseudo code:
result = array Where(even) Skip(9) Take(1)
 
To find the even numbers i said:
for ( i = 0; i < arr.length; i++)
{
If (arr[ i ] %2 == 0)
}
But i dont know how to write so that i get the 10th number from the even number i received
So basically you're just completely ignoring what I said and trying to write code with no regard for an algorithm. If you're going to write code with no idea what the code is supposed to do, even after I specifically addressed that, then why would you be surprised that it doesn't work?
 
Sorry i just wanted to show u what i did
1: take they array and % each number by 2 to se if it is even or not
2: get the 10th number from that list
 
Here's how I normally tell people how to develop an algorithm. Pretend that you need the task to be done by a 5 year old that you are talking to on the phone. What set of instructions will you give the 5 year old to get the task accomplished?
 
Oky how about this
1) imagine a line of people and if you can make two teams of the same amount of people in each team its even
2) in a line of 10 people you have to pick the person that is number 10 in line
 
That's not an algorithm. An algorithm is DETAILED, STEP-BY-STEP instructions for HOW to do something, Again, you've ignored what I said:
How would you do it manually, i.e. with pen and paper. Work that out and formalise the steps into an algorithm that you could provide to anyone else to follow to get the same result.
How is what you posted formal steps that anyone could follow to get the desired result?
 
LOL! @ItsMitch is doing functional programming instead of procedural programming. :)

Unfortunately, C# is still mostly procedural (even though LINQ is pushing things into the functional realm). So it looks like @ItsMitch will have to rephrase his functional approach into a procedural approach.
 
That's not an algorithm. An algorithm is DETAILED, STEP-BY-STEP instructions for HOW to do something, Again, you've ignored what I said:

How is what you posted formal steps that anyone could follow to get the desired result?
Are you talking about what i just posted after skydiver responded?
 
LOL! @ItsMitch is doing functional programming instead of procedural programming. :)

Unfortunately, C# is still mostly procedural (even though LINQ is pushing things into the functional realm). So it looks like @ItsMitch will have to rephrase his functional approach into a procedural approach.
I have to do it for a assignment and im sorry if i dont speak with the right terms im still new to c#
 

Latest posts

Back
Top Bottom