Reputation: 129
I need to create 2 methods. Method 1 (CreateArray) shall allow the user to enter the length of an array they would like to create and then enter the value for each element. Method 2 (PrintArray) prints the array created in Method 1.
Both methods work fine on a standalone basis. However, I don't know how to pass CreateArray method to PrintArray method.
class Calculation
{
public int[] CreateArray (int a)
{
int count = 1;
int[] CreateArray = new int[a];
for (int i = 0; i < CreateArray.Length; i++)
{
Console.Write($"{count}.) Enter a value - ");
int userInput = int.Parse(Console.ReadLine());
CreateArray[i] = userInput;
count++;
}
return CreateArray;
}
public void PrintArray(int[] numbers)
{
int elementNum = 1;
for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine($"{elementNum}.) {numbers[i]}");
elementNum++;
}
}
}
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the Length of an array you would like to create: ");
int arrayLength = int.Parse(Console.ReadLine());
Calculation calc = new Calculation();
calc.CreateArray(arrayLength);
}
}
Upvotes: 2
Views: 1788
Reputation: 604
You need to store the results of "calc.CreateArray(arrayLength);" in a local variable and pass it to PrintArray():
int[] newArray = calc.CreateArray(arrayLength);
calc.PrintArray(newArray);
Upvotes: 1