class Program
{
int[] arrayOne = new int[3];
int[] arrayTwo = new int[5];
int[] arrayThree = new int[7];
int sumOne = 0;
int sumTwo = 0;
int sumThree = 0;
static void Main()
{
//call method to generate numbers
generateRandomNumbers();
//call method to get sums of arrays
sumOne = getSum(arrayOne);
sumTwo = getSum(arrayTwo);
sumThree = getSum(arrayThree);
//call method to show numbers in each array and their sum
showNumbers(arrayOne);
showNumbers(arrayTwo);
showNumbers(arrayThree);
ReadKey();
}
private static void generateRandomNumbers()
{
Random random = new Random();
//load in values for arrayOne
for (int i = 0; i < arrayOne.Length; i++)
arrayOne[i] = random.Next(1, 99);
//arrayTwo
for (int j = 0; j < arrayTwo.Length; j++)
arrayTwo[j] = random.Next(1, 99);
//arrayThree
for (int k = 0; k < arrayThree.Length; k++)
arrayThree[k] = random.Next(1, 99);
}
private static int getSum(int[] array)
{
int total = 0;
for (int x = 0; x < array.Length; x++)
total += array[x];
return total;
}
private static void showNumbers(int[] array)
{
for (int m = 0; m < array.Length; m++)
Write("{0, 5}", array[m]);
switch (array.Length)
{
case 3:
Write(" = {0}", sumOne);
break;
case 5:
Write(" = {0}", sumTwo);
break;
case 7:
Write(" = {0}", sumThree);
break;
}
WriteLine();
}
}
Basically, the error occurs everywhere that I use arrayOne, arrayTwo, arrayThree, and sumOne, sumTwo, sumThree. Removing 'static'from the Main and generateRandomNumbers methods removes all of the errors, but then of course it can't compile without the static Main method.
In my C# course, we are learning methods and arrays at the moment and the goal of this program is to put random numbers into three arrays and then display each number and the sum of the array.
I have searched around and the answers mostly go along the lines of "create an instance" but I'm not sure how to go about doing that. Any help would be much appreciated!