Q7: Write a C# program to input an double array, and then computes some simple s
ID: 3861651 • Letter: Q
Question
Q7: Write a C# program to input an double array, and then computes some simple statistics on that array. The program should have two user-defined methods to help you with the task.public static void InputArray(double[] array, ref int n)public static double ComputeStats(double[] array, int n, ref double min, ref double max)
InputArray should be similar to Fill from Lab 4. ComputeStats is a bit more interesting. It should return the average of the numbers in the array using a return statement and the minimum and maximum values through the call by reference parameters min, and max respectively. You must compute the average, minimum and maximum using a for loop (you are not permitted to use any built-in methods from the Array class such as Sort). The average, minimum and maximum are to be outputted from the Main() method. You can assume that the maximum number of values in the array will be 20. Add a loop to the Main() method to allow the user to process more than one arrayif they so wish.
Explanation / Answer
using System.IO;
using System;
class Program
{
static void Main()
{
int n = 20;
double max=0,min=0;
double[] array = new double[n];
InputArray(array,ref n);
double average = ComputeStats(array, n,ref max, ref min);
Console.WriteLine("Average is "+average);
Console.WriteLine("Max value is "+max);
Console.WriteLine("Min value is "+min);
}
public static void InputArray(double[] array, ref int n) {
for(int i=0; i<n; i++){
Console.WriteLine("Enter a number: ");
array[i]=Convert.ToInt32(Console.ReadLine());
}
}
public static double ComputeStats(double[] array, int n, ref double min, ref double max) {
double sum = 0;
max = array[0];
min = array[0];
for(int i=0; i<n; i++){
sum = sum + array[i];
if(max < array[i]){
max = array[i];
}
if(min > array[i]){
min = array[i];
}
}
return sum/n;
}
}
Output:
sh-4.3$ mcs *.cs -out:main.exe
sh-4.3$ mono main.exe
Enter a number:
20
Enter a number:
19
Enter a number:
23
Enter a number:
45
Enter a number:
6
Enter a number:
5
Enter a number:
7
Enter a number:
8
Enter a number:
9
Enter a number:
1
Enter a number:
111
Enter a number:
123
Enter a number:
134
Enter a number:
156
Enter a number:
99
Enter a number:
88
Enter a number:
77
Enter a number:
66
Enter a number:
55
Enter a number:
22
Average is 53.7
Max value is 1
Min value is 156
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.