answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
sertanlavr [38]
1 year ago
9

Write a program that computes and prints the average of the numbers in a text file. You should make use of two higher-order func

tions to simplify the design.
An example of the program input and output is shown below:




Enter the input file name: numbers.txt




The average is 69.83333333333333



______________________________________




Filename: numbers.txt




45 66 88



100 22 98
Computers and Technology
1 answer:
KengaRu [80]1 year ago
6 0

Answer:

Following is the program code as required.

Comments are given inside the code.

Explanation:

(=> Defining the main function)

def main():

(=> Getting input from user)

file = input("What is name of file?")

(=>Storing text from file in variable data )

data = open(file, "r")

(=> read the file and then split it)

info = data.read().split()

(=> Generating array named numbers)

numbers = []

for line in info:

(=>numbers will be appended from in format to list format)

numbers.append(int(line))

data.close()

(=> Average will be found by dividing sum of numbers to length of array)

avg = float(sum(numbers))/len(numbers)

(=> Printing calculation of average)

print("Calculated average is",avg)

main()

<h2>i hope it will help you!</h2>
You might be interested in
2 Name the package that contains scanner class?​
Marat540 [252]
the answer is Java.util.scanner
5 0
2 years ago
Dr. Apple wants to study a drug to manage diabetes in adolescents. The researcher plans to use an electronic informed consent (e
GarryVolchara [31]

Answer: Yes, the eIC could be used for assent.

Explanation:

Electronic informed consent (eIC) may be used for seeking, confirming and documenting informed consent.

5 0
2 years ago
Read 2 more answers
A large gambling company needs to be able to accept high volumes of customer wagers within short timeframes for high-profile spo
ElenaW [278]

Answer:

a mobile app that only accepts wagers based on the user's location.

Explanation:

One effective Cloud Solution would be a mobile app that only accepts wagers based on the user's location. This app would allow the clients to safely and effectively place any wagers which are sent to the gambling company's cloud servers which would be third-party Cloud services. These services will make sure that the gambling company can handle any number of wages at any given time. The mobile app would also only accept wagers from individuals in specific locations in which the law does not prohibit gambling. Therefore, preventing the gambling company from being liable for any losses.

5 0
1 year ago
Write a loop that subtracts 1 from each element in lowerScores. If the element was already 0 or negative, assign 0 to the elemen
Xelga [282]

Answer:

The JAVA program to implement for loop for given scenario is shown below.

public class MyProgram {

   public static void main(String args[]) {    

     int len = 5;

     int[] lowerScores = new int[len];      

     lowerScores[0] = 5;

     lowerScores[1] = 0;

     lowerScores[2] = 2;

     lowerScores[3] = -3;      

     System.out.println("The original elements of array are ");

     for(int i=0; i<len; i++)

     {

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

     }      

     System.out.println();

     System.out.println("The modified elements of array are ");

     for(int i=0; i<len; i++)

     {

         // if element is 0 or negative, 0 is assigned to this index

         if(lowerScores[i] <= 0)

           lowerScores[i] = 0;

       // 1 is subtracted from every non zero and positive element

         else

           lowerScores[i] = lowerScores[i] - 1;

           

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

     }

   }

}

OUTPUT

The original elements of array are  

5 0 2 -3 0  

The modified elements of array are  

4 0 1 0 0

Explanation:

The integer array is declared as shown.

int[] lowerScores = new int[len];

The length of the array is given by integer variable len. As per the question, we have taken four elements and size of 5 for the given array.

int len = 5;

The elements of the array are initialized inside the program with the elements given in the question.

lowerScores[0] = 5;

      lowerScores[1] = 0;

The values of the array can be changed to test the program. Also, the size of the array can be changed for testing.

As per the given scenario, if the element is greater than zero, 1 is subtracted from this element. For elements less than or equal to zero, that particular element is assigned the value of 0.

The given program implements if else statement inside for loop for this purpose.

for(int i=0; i<len; i++)

     {

         // if element is 0 or negative, 0 is assigned to this index

         if(lowerScores[i] <= 0)

           lowerScores[i] = 0;

       // 1 is subtracted from every non zero and positive element

         else

           lowerScores[i] = lowerScores[i] - 1;

     }  

5 0
1 year ago
Read 2 more answers
. Create a text file that contains your expenses for last month in the following categories: • Rent • Gas • Food • Clothing • Ca
Whitepunk [10]

Answer:

  1. from matplotlib import pyplot as plt
  2. with open("text.txt") as file:
  3.    data = file.readlines()
  4.    category = []
  5.    amount = []
  6.    for row in data:
  7.        values = row.split(" ")
  8.        category.append(values[0])
  9.        amount.append(float(values[1]))
  10.    
  11. figure = plt.figure()
  12. ax = figure.add_axes([0,0,1,1])
  13. ax.axis('equal')
  14. ax.pie(amount, labels = category, autopct='%1.2f%%')
  15. plt.show()

Explanation:

Firstly, we need to import matplotlib library (Line 1).

Next, open a file stream and use readlines method to read the data from the text file (Line 4). Presume the data read from the text files are as follows:

Rent 450

Gas 150

Food 500

Clothing 120

Car 600

Create two lists, category and amount (Line 5-6). Use a for loop to traverse through the read data and use split method to break each row of data into two individual items. The first item is added to category list whereas the second item is added to amount list (Line 7 - 10).

Next, create a plot figure and then add the axes (Line 12 - 13). We can use the pie method to generate a pie chart (Line 15) by setting the amount list as first argument and category list as value of labels attributes. The output pie chart can be found in the attachment.

7 0
2 years ago
Other questions:
  • The tuna marketers' task in the "tunathewonderfish.com" website and related campaign was to ________.
    15·1 answer
  • Blender questions
    8·2 answers
  • You have been tracking your exercise routine, which involves running, lifting weights, yoga, and stretching. You want to see wha
    15·2 answers
  • Does the Boolean expression count &gt; 0 and total / count &gt; 0 contain a potential error? If so, what is it?
    8·1 answer
  • Lin is booting up his computer, and during the boot process, the computer powers down. After several unsuccessful attempts to bo
    12·1 answer
  • I need to write a really simple python program to solve this issue. I have most of the program written but, when I run the secon
    8·1 answer
  • How many times does the following loop execute?int upperCaseLetters = 0;String str = "abcdEfghI";boolean found = false;for (int
    5·1 answer
  • For a class project, Jerome builds a simple circuit with a battery and three light bulbs. On his way to school, Jerome drops his
    9·1 answer
  • 1. The precious metals needed to make computer chips, graphic cards, and transistors are found in only a small population of cou
    8·1 answer
  • Select the correct navigational path to freeze the top row of your worksheet. Select the panes you wish to freeze. Click the tab
    15·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!