Answer to Question #330772 in C# for Macky

Question #330772

The first line will contain a message prompt to input the number of elements.



The second line will contain message prompts for the elements of the array.



The succeeding lines will contain the arranged elements of the array. Make sure to print the numbers in the order in which they appear in the input. In this case, since 6 appeared first before 12, 6 is printed first.

1
Expert's answer
2022-04-19T08:07:07-0400

public static void Main()

{

Console.WriteLine("Please enter array length");

var arrayLength = Convert.ToInt32(Console.ReadLine());


var array = InitArray(arrayLength);

PrintArray(array);

Console.ReadLine();

}


public static void PrintArray(int[] arr)

{

    for (int i = 0; i < arr.Length; i++)

    {

if (i == arr.Length - 1)

{

Console.Write(arr[i]);   

}

else

{

Console.Write("{0},",arr[i]);   

}

    }

Console.WriteLine();    

}


private static int[] InitArray(int arraySize) {

var array = new int[arraySize];

for (int i = 0; i < arraySize; i++)

    {

Console.WriteLine("Please enter {0} array element", i);

       array[i] = Convert.ToInt32(Console.ReadLine());

    }


return array;

}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS