Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In C# - Create a program named FlexibleArrayMethod that declares at least three

ID: 3679434 • Letter: I

Question

In C# - Create a program named FlexibleArrayMethod that declares at least three integer
arrays of different sizes. In turn, pass each array to a method that displays all the
integers in each array and their sum.

amended as follows.

The arrays should be of sizes 3, 5, 7 (write your program, including array initialization, in the
way applicable to much larger arrays as well). Populate them with random numbers from 1 to 99
generated automatically. The results for each array
should be displayed on a separate line.

Explanation / Answer

using System;
namespace HelloWorld
{
    class ArraySample
    {
        static void Main()
        {
            ArraySample test=new ArraySample();
            test.FlexibleArrayMethod();
          
        }
        void FlexibleArrayMethod()
        {
            //declare arrays
int[] array1=new int[3];
int[] array2=new int[5];
int[] array3=new int[7];
//initialize arrays
intializeArray(array1);
intializeArray(array2);
intializeArray(array3);
displayArray(array1);
displayArray(array2);
displayArray(array3);
}

void intializeArray(int[] n)
{
     Random rng = new Random();
for(int i=0;i<n.Length;i++)
n[i]=rng.Next(1,99);
}
      
void displayArray(int[] n)
{
    int sum=0;
    Console.WriteLine("Elements are:");
    for(int i=0;i<n.Length;i++)
    {
        Console.WriteLine(n[i]);
        sum+=n[i];
    }
    Console.WriteLine("Sum of array is:"+ sum);
}
    }
}