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
 
Mitch, an algorithm is like a recipe. You should have a list of step by step instructions of how to bake a cake. This is called procedural programming. Unfortunately, what you have been doing is that you've been describing things in terms of end results and/or characteristics of the final result. As great as that is when presenting the cake in a cooking show just saying "a chocolate cake with vanilla filling, mocha butter frosting, and chocolate crumbles on top" is not going help anybody watching the show unless the show actually presents how to get to that end product.
 
Mitch, an algorithm is like a recipe. You should have a list of step by step instructions of how to bake a cake. This is called procedural programming. Unfortunately, what you have been doing is that you've been describing things in terms of end results and/or characteristics of the final result. As great as that is when presenting the cake in a cooking show just saying "a chocolate cake with vanilla filling, mocha butter frosting, and chocolate crumbles on top" is not going help anybody watching the show unless the show actually presents how to get to that end product.
I thought about how to make the algorithm and this is the best I could come up with
step 1:
get the list of numbers
step 2:
take each number in the list and divide it by 2
step 3:
get the answer from previous step and check if the answer is equal to 0
step 4:
get all the answers that are equal to 0
step 5:
take away the first 9 numbers from that list
step 6;
print the only number remaining (10th number)
 
Great! Now, it's time to convert each step you listed above into 1-10 lines of code each.
 
Declare a counter variable as int count=0 outside of the loop
now traverse the loop from the i=0 to the array.length
check if array of i-th index %2==0 and counter==10
then print (array of i-th index)
else
counter=counter+1;
 
I would suggest trying out that algorithm on paper first before implementing it.
 
I thought about how to make the algorithm and this is the best I could come up with
step 1:
get the list of numbers
step 2:
take each number in the list and divide it by 2
step 3:
get the answer from previous step and check if the answer is equal to 0
step 4:
get all the answers that are equal to 0
step 5:
take away the first 9 numbers from that list
step 6;
print the only number remaining (10th number)
Did you test that algorithm with pen and paper to see whether it produced the correct result for various inputs? Did you consider whether any steps were too vague or could actually be broken down into several smaller steps? For instance, what does this really mean:
get all the answers that are equal to 0
That is both vague and composite.
 
Did you test that algorithm with pen and paper to see whether it produced the correct result for various inputs? Did you consider whether any steps were too vague or could actually be broken down into several smaller steps? For instance, what does this really mean:

That is both vague and composite.
Oky i will try and break it down more
 
I put everything into code and I am getting the right answers the only problem is the output should be in one line for example: 1,2,3,4,5 but when I use Console.Write to do that I cant test multiple times because it doesn't go to the next line. The problems are at printing from 1 - 10 and printing m to n.

C#:
/// <summary>
/// Provides a variety of numeric methods
/// </summary>
public class Matherator
{
    #region Constructor

    /// <summary>
    /// Constructor
    /// </summary>
    public Matherator()
    {
    }

    #endregion

    #region Methods

    /// <summary>
    /// Prints the numbers from 1 to 10
    /// </summary>
    public void PrintOneToTen()
    {
        for (int input = 1; input <= 10; input++)
        {
            Console.Write(" " + input);
        }
    }

    /// <summary>
    /// Prints the numbers from m to n
    /// </summary>
    /// <param name="m">m</param>
    /// <param name="n">n</param>
    public void PrintMToN(int m, int n)
    {
        for (int i = m; i <= n; i++)
        {
            Console.Write(" " +  i );
        }
    }

    /// <summary>
    /// Returns the tenth even number, with 2 as the first even number
    /// </summary>
    /// <returns>tenth even number</returns>
    public int GetTenthEvenNumber()
    {
        List<int> list = new List<int>();

        for (int i = 2; i <= 2 * 10; i += 2)
        {
            list.Add(i);
        }

        return list[list.Count - 1];
    }

    /// <summary>
    /// Returns the nth even number, with 2 as the first even number
    /// </summary>
    /// <param name="n">n</param>
    /// <returns>nth even number</returns>
    public int GetNthEvenNumber(int n)
    {
        // delete code below; only included so we could compile
        List<int> nthEven = new List<int>();

        for (int i = 2; i <= 2 * n; i += 2)
        {
            nthEven.Add(i);
        }

        return nthEven[nthEven.Count - 1];
    }

    #endregion
}
 
Last edited by a moderator:
Put a Console.WriteLine() after your loop to go to the next line.
 
Something doesn't feel right. The algorithm that you came up with does not match up with the code that you actually wrote.
 
Back
Top Bottom