Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

James Calby
James Calby
15,153 Points

Given the an array of integers, you will build an function that will multiply each one times 2 and then return the array

Given the an array of integers, you will build an function that will multiply each one times 2 and then return the array back to the main method and output it.

My Code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Methods
{
    class Program
    {
        static void Main(string[] args)
        {

            // Given the an array of integers, you will build an function that will multiply each one times 2 and then return the array back to the main method and output it.
            // Array of int elements
            int[] numNum = new int[5] { 1, 2, 5, 6, 9 };
            // Return Value From Custom Function:
            int[] results = num(numNum);

            // Result to print to the console:
            // Print out full sentence string
            // You may use a loop to output the arrays or you may convert them to a string and
            // output that.
            // Console.WriteLine(result);


            for (int i = 0; i < numNum.Length; i++)
            {
                int[] num = numNum[i];
                Console.WriteLine(num [0]);
            }




        }

        // Create a function that receives this array of ints.
        // Create a loop to cycle through the array and multiply each element by 2.
        public static int[] num(int[] numberArray)
        {
            int[] doubled = new int[numberArray.Length];

            for (int i = 0; i > numberArray.Length; i++)
            {
                doubled[i] = numberArray[i] * 2;
            }
            return doubled;

        }
    }
} 

Can figure out how to output it to the console. Thank you in Advance.

1 Answer

Allan Clark
Allan Clark
10,810 Points

try

Console.Write(numNum[i]);

Write line is going to go down a line every time and from the prompt it sounds like it wants all the numbers on one line. Also, the num array is just redundant