0% found this document useful (0 votes)
9 views

Solution

The document describes a program that calculates the average of a list of numbers. It explains the problem, input, functionality, output, provides examples, pseudocode, and test cases. The code is included that takes a list of numbers as input, calculates the sum and count, and outputs the average.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Solution

The document describes a program that calculates the average of a list of numbers. It explains the problem, input, functionality, output, provides examples, pseudocode, and test cases. The code is included that takes a list of numbers as input, calculates the sum and count, and outputs the average.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

PART 1:

Problem statement:
I have choosed to solve the problem of calculating the average of a list of numbers. The
program will take a list of numerical inputs, calculate their average, and provide the result to the
user.

Input Data:

A list of numerical values (integers or floats).

Program Functionality:
The program will take the list of numerical inputs, sum all the values in the list, and then divide
the sum by the number of elements in the list to calculate the average. The average will be
displayed to the user as the output.

Output:

The program will provide the average of the input list of numbers as the final result to the user.

PART 2

1
Example 1:

Input Data: [5, 10, 15, 20, 25]

Steps:

Initialize a variable sum to 0.


Loop through the list of numbers.
For each number in the list, add it to the sum variable.
After the loop, calculate the average by dividing the sum by the number of elements in the list.
Display the average to the user.

Result: Average of [5, 10, 15, 20, 25] is 15.0.

Example 2:

Input Data: [2.5, 3.5, 4.5]

Steps:

Initialize a variable sum to 0.


Loop through the list of numbers.
For each number in the list, add it to the sum variable.
After the loop, calculated the average by dividing the sum by the number of elements in the
list.
Display the average to the user.

Result: Average of [2.5, 3.5, 4.5] is 3.5.

PART 3:

2
Pseudocode::

1. Initialize the variable `sum` to 0


2. Initialize the variable `count` to 0
3. Input a list of numbers from the user
4. Loop through each number in the list
4.1. Add the number to the `sum`
4.2. Increment `count` by 1
5. calculate the average by dividing `sum` by `count`
6. Display average to the user

3
PART 4:

For testing, I provided the following test cases:

Test Case 1:

Input: [5, 10, 15, 20, 25]

Expected Output: Average of [5, 10, 15, 20, 25] is 15.0.

Results: The program output matched the expected output.

Test Case 2:

Input: [2.5, 3.5, 4.5]

Expected Output: Average of [2.5, 3.5, 4.5] is 3.5.

Results: The program output matched the expected output.

Test Case 3:

Input: [1]

Expected Output: Average of [1] is 1.0.

Results: The program output matched the expected output.

PART 5:

4
commenting:

CODE:

// Purpose: Calculate the average of a list of numbers

import java.util.Scanner;

public class Main{


public static void main (String args[]){

Scanner scanner= new Scanner(System.in);

//initialize variables to store the sum and count of numbers.


double sum = 0;
int count = 0;

System.out.print("Enter the number of elements: ");


Int n = scanner.nextInt();

// Input a list of numbers from the user.


System.out.println("Enter " + n + " numbers:");
for (int i = 0; i < n; i++) {
double number = scanner.nextDouble();
sum += number; // Add the number to the sum
count++; // Increment the count
}

// Calculate the average by dividing the sum by the count.


double average = sum / count;

// Display the average to the user.


System.out.println("The average is: " + average);

scanner.close(); // Close the scanner when done.


}
}

5
PART 6:

Link: https://replit.com/@0112it201026/averagecalculator?v=1

You might also like