In: Computer Science
A very common application of arrays is computing statistics on lists of numbers. Below you will find a template of a program that uses four user-defined methods: input an array of numbers, compute the average the array, find the largest number in the array, and find the smallest number in the array. Enter the following program into C# (ignore the line numbers) and replace the lines marked with // *** with the appropriate C# code (may require more than one line of code to complete the missing parts).
// Name: xxxxxxxxxxxxx // Student Number: xxxxxxx // Lab 4, Part 2 // Program Description: This program uses four user-defined methods: one // to input an array of numbers, one to compute the average the array, one // to find the largest number in the array, and one to find the smallest // number in the array. using System; public static class Lab4_2 { public static void Main() { int[] compStats = new int[25]; int n = 0, large, small; double avg = 0; // Input values into the array InpArray(compStats, ref n); // Find the average of the elements in the array // *** Insert the code for the call to the FindAverage method // Find the largest element in the array // *** Insert the code for the call to the FindLarge method // Find the largest element in the array // *** Insert the code of the call to the FindSmall method // Print out the results Console.WriteLine("\nThe Average of the array is {0:F}", avg); // *** Insert the code to print out the largest and smallest values // Pause until user is done Console.ReadLine(); } // Method: InpArray // Description: Input values into an array. // Parameters: arrValues: the array to fill. // num: number of elements in the array. // Returns: void public static void InpArray(int[] arrValues, ref int num) { // input the number of data values to put in the array do { Console.Write("Enter the number of elements (<= 25) => "); num = Convert.ToInt32(Console.ReadLine()); } while (num < 0 || num > 25); // loop to enter the values for (int i = 0; i < num; ++i) { Console.Write("Enter the Element {0} => ", i); arrValues[i] = Convert.ToInt32(Console.ReadLine()); } } // Method: FindAverage // Description: Computes the average of the elements in the array. // Parameters: arrVals: array to be processed // num: number of elements in the array. // Returns: the average of the elements in the array public static double FindAverage(int[] arrVals, int num) { // *** Insert the details of the FindAverage Method } // Method: FindLarge // Description: Determines the largest element in the array. // Parameters: arrVals: array to be processed // num: number of elements in the array. // Returns: the largest element in the array public static int FindLarge(int[] arrVals, int num) { // *** Insert the details of the FindLarge Method } // Method: FindSmall // Description: Determines the smallest element in the array. // Parameters: arrVals: array to be processed // num: number of elements in the array. // Returns: the smallest element in the array public static int FindSmall(int[] arrVals, int num) { // *** Insert the details of the FindSmall Method } }
C# Code:
// Name: xxxxxxxxxxxxx
// Student Number: xxxxxxx
// Lab 4, Part 2
// Program Description: This program uses four user-defined
methods: one
// to input an array of numbers, one to compute the average the
array, one
// to find the largest number in the array, and one to find the
smallest
// number in the array.
using System;
public static class Lab4_2
{
public static void Main()
{
int[] compStats = new int[25];
int n = 0, large, small;
double avg = 0;
// Input values into the array
InpArray(compStats, ref n);
// Find the average of the elements in the array
avg = FindAverage(compStats, n);
// Find the largest element in the array
large = FindLarge(compStats, n);
// Find the largest element in the array
small = FindSmall(compStats, n);
// Print out the results
Console.WriteLine("\nThe Average of the array is {0:F}",
avg);
Console.WriteLine("\nThe Largest of the array is {0}",
large);
Console.WriteLine("\nThe Smallest of the array is {0}", small);
// Pause until user is done
Console.ReadLine();
}
// Method: InpArray
// Description: Input values into an array.
// Parameters: arrValues: the array to fill.
// num: number of elements in the array.
// Returns: void
public static void InpArray(int[] arrValues, ref int num)
{
// input the number of data values to put in the array
do
{
Console.Write("Enter the number of elements (<= 25) =>
");
num = Convert.ToInt32(Console.ReadLine());
} while (num < 0 || num > 25);
// loop to enter the values
for (int i = 0; i < num; ++i)
{
Console.Write("Enter the Element {0} => ", i);
arrValues[i] = Convert.ToInt32(Console.ReadLine());
}
}
// Method: FindAverage
// Description: Computes the average of the elements in the
array.
// Parameters: arrVals: array to be processed
// num: number of elements in the array.
// Returns: the average of the elements in the array
public static double FindAverage(int[] arrVals, int num)
{
double sum = 0;
for(int i = 0; i < num; i++)
{
sum += arrVals[i];
}
return sum / num;
}
// Method: FindLarge
// Description: Determines the largest element in the array.
// Parameters: arrVals: array to be processed
// num: number of elements in the array.
// Returns: the largest element in the array
public static int FindLarge(int[] arrVals, int num)
{
int large = arrVals[0];
for (int i = 1; i < num; i++)
{
if(large< arrVals[i])
{
large = arrVals[i];
}
}
return large;
}
// Method: FindSmall
// Description: Determines the smallest element in the array.
// Parameters: arrVals: array to be processed
// num: number of elements in the array.
// Returns: the smallest element in the array
public static int FindSmall(int[] arrVals, int num)
{
int small = arrVals[0];
for (int i = 1; i < num; i++)
{
if (small > arrVals[i])
{
small = arrVals[i];
}
}
return small;
}
}
Sample Output:
Enter the number of elements (<= 25) => 5
Enter the Element 0 => 15
Enter the Element 1 => 23
Enter the Element 2 => 456
Enter the Element 3 => 23
Enter the Element 4 => 10
The Average of the array is 105.40
The Largest of the array is 456
The Smallest of the array is 10