53.2k views
4 votes
Write a program named Averages that includes a method named Average that accepts any number of numeric parameters, displays them, and displays their average. For example, if 7 and 4 were passed to the method, the output would be: 7 4 -- Average is 5.5 Test your function in your Main(). Tests will be run against Average() to determine that it works correctly when passed one, two, or three numbers, or an array of numbers. Grading When you have completed your program, click the Submit button to record your score.

2 Answers

1 vote

Final answer:

The Averages program in C# can be created with a method that calculates the average of any number of numeric parameters and displays each number along with the average. The key aspects include the use of the params keyword for flexible arguments and iterating through the numbers to compute their sum.

Step-by-step explanation:

Writing the Averages Program in C#

To solve the student's question, you can create a program in C# called Averages that includes a method named Average. This method takes a params array of numbers, computes the average, and prints the individual numbers along with the calculated average. Below is an example of how the method and the Main function could be implemented:

using System;

class Averages {
static void Main(string[] args) {
Average(7, 4); // Test the function with two numbers
Average(10); // Test with one number
Average(3, 6, 9); // Test with three numbers
Average(new int[] { 2, 4, 6, 8, 10 }); // Test with an array of numbers
}

static void Average(params int[] numbers) {
if (numbers.Length == 0) {
Console.WriteLine("No numbers to average.");
return;
}

int sum = 0;
foreach (var number in numbers) {
sum += number;
Console.Write(number + " ");
}
double avg = (double)sum / numbers.Length;
Console.WriteLine("-- Average is " + avg);
}
}

This program demonstrates the use of the params keyword, which allows the Average method to accept a variable number of arguments, making it flexible and robust for computation.

User Juuso Ohtonen
by
3.4k points
5 votes

Answer:

In Java:

import java.util.*;

public class Averages{

public static double Average(int [] myarr, int n){

double sum = 0;

for(int i = 0;i<n;i++){

System.out.print(myarr[i]+" ");

sum+=myarr[i]; }

System.out.println();

return sum/n; }

public static void main(String[] args) {

Scanner inp = new Scanner(System.in);

System.out.print("Length of inputs: ");

int n = inp.nextInt();

int [] arr = new int[n];

System.out.print("Enter inputs: ");

for(int i = 0; i<n;i++){

arr[i] = inp.nextInt(); }

System.out.println("Average: "+Average(arr,n)); }}

Step-by-step explanation:

The function is defined here

public static double Average(int [] myarr, int n){

Initialize sum to 0

double sum = 0;

Iterate through the elements of the array

for(int i = 0;i<n;i++){

Print each element followed by space

System.out.print(myarr[i]+" ");

Add up elemente of the array

sum+=myarr[i]; }

Print a new line

System.out.println();

Return the average

return sum/n; }

The main begins here

public static void main(String[] args) {

Scanner inp = new Scanner(System.in);

Prompt the user for number of inputs

System.out.print("Length of inputs: ");

Get the number of inputs

int n = inp.nextInt();

Declare an array

int [] arr = new int[n];

Get element of the array

System.out.print("Enter inputs: ");

for(int i = 0; i<n;i++){

arr[i] = inp.nextInt(); }

Call the Average function

System.out.println("Average: "+Average(arr,n)); }}

User Daniel Dobalian
by
3.4k points