Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Last year, a local college implemented rooftop gardens as a way to promote energ

ID: 3695818 • Letter: L

Question

Last year, a local college implemented rooftop gardens as a way to promote energy efficiency and save money.

Write a program (flowchart AND pseudocode) that:

• Allows the user to enter the energy bills from January to December for the year prior to going green.

• Allows the user to enter the energy bills from January to December of the past year after going green.

• The program should calculate the energy difference from the two years, month by month, and display the two years’ worth of data, along with the savings.

• Additionally, the savings array should be recorded (written) to a file called savings.txt.

Explanation / Answer

The American Red Cross wants you to write a program that will calculate the average pints of blood donated during a blood drive. The program should take in the number of pints donated during the drive, based on a seven hour drive period. The average pints donated during that period should be calculated and written to a file. Write a loop around the program to run multiple times. The data should be appended to the file to keep track of multiple days. If the user wants to print data from the file, read it in and then display it. Store the pints per hour and the average pints donated in a file called blood.txt.

External Design

When the user specifies to print the data, the following should be displayed:

Pints Each Hour

##
##
##
##
##
##
##

Average Pints

##.##

Step 1: Note that the getPints, getTotal, and getAverage functions do not change. Also note that the references to displayInfo, getHigh, and getLow functions are removed to meet the new requirements. In the pseudocode below, add the following:

In the Main Module

In the writeToFile Module

In the readFromFile Module

Module main()

     //Declare local variables

     Declare String again = “yes”

     Declare Real pints[7]

     Declare Real totalPints

     Declare Real averagePints

     a. _________________________________________

         

     While again == “yes”

           //module calls below

           Display “Enter 1 to enter in new data and store to file”

           Display “Enter 2 to display data from the file”

           Input b. ______________________________

           c. If _____________ == _________________ Then

                     pints = getPints(pints)

                     totalPints = getTotal(pints, totalPints)

                     averagePints = getAverage(totalPints, averagePints)

                d. Call _____________(__________,________)

           Else

                e. Call _____________(__________,________)

           End If

           Display “Do you want to run again: yes or no”

           Input again

     End While

End Module

Module getPints(Real pints[])

Declare Integer counter = 0

           For counter = 0 to 6

                Display “Enter pints collected:”

                Input pints[counter]

           End For

End Module

Function getTotal(Real pints[], Real totalPints)

Declare Integer counter = 0

totalPints = 0

          For counter = 0 to 6

                totalPints = totalPints + pints[counter]

           End For

Return totalPints

    

     Function getAverage(Real totalPints, Real averagePints)

averagePints = totalPints / 7

Return averagePints

    

     Module writeToFile(Real pints[], Real averagePints)

           f. Declare _________ ____________ ____________

           g. Open _________________ ____________________

           h. Write _______________ “__________________”

               Declare Integer counter = 0

           i. While counter < 7

                Write __________ _________[_________]

                counter = counter + 1

               End While

j. Write ____________ “_____________”

k. Write ____________ ________________

l. Close __________________

     End Module

     Module readFromFile(Real pints[], Real averagePints)

               Declare String str1, str2

m. Declare ___________ ___________

           n. Open __________ “__________”

           o. Read _____________ ____________

           p. Display ____________

               Declare Integer counter = 0

           q. While counter < 7

Read ______________ _____________[_________]

                 counter = counter + 1

               End While

               counter = 0

           r. While counter < 7

Display _______________[_________]

                 counter = counter + 1

               End While

           s. Read ___________ ____________

           t. Display ______________

           u. Read _____________ _______________

           v. Display _____________

           w. Close _______________

     End Module

The American Red Cross wants you to write a program that will calculate the average pints of blood donated during a blood drive. The program should take in the number of pints donated during the drive, based on a seven hour drive period. The average pints donated during that period should be calculated and written to a file. Write a loop around the program to run multiple times. The data should be appended to the file to keep track of multiple days. If the user wants to print data from the file, read it in and then display it. Store the pints per hour and the average pints donated in a file called blood.txt.

External Design

When the user specifies to print the data, the following should be displayed:

Pints Each Hour

##
##
##
##
##
##
##

Average Pints

##.##

Step 1: Note that the getPints, getTotal, and getAverage functions do not change. Also note that the references to displayInfo, getHigh, and getLow functions are removed to meet the new requirements. In the pseudocode below, add the following:

In the Main Module

In the writeToFile Module

In the readFromFile Module

Module main()

     //Declare local variables

     Declare String again = “yes”

     Declare Real pints[7]

     Declare Real totalPints

     Declare Real averagePints

         

     While again == “yes”

           //module calls below

           Display “Enter 1 to enter in new data and store to file”

           Display “Enter 2 to display data from the file”

           Input b.

           c. If _____________ == _________________ Then

                     pints = getPints(pints)

                     totalPints = getTotal(pints, totalPints)

                     averagePints = getAverage(totalPints, averagePints)

                d. Call

           Else

                e. Call

           End If

           Display “Do you want to run again: yes or no”

           Input again

     End While

End Module

Module getPints(Real pints[])

Declare Integer counter = 0

           For counter = 0 to 6

                Display “Enter pints collected:”

                Input pints[counter]

           End For

End Module

Function getTotal(Real pints[], Real totalPints)

Declare Integer counter = 0

totalPints = 0

          For counter = 0 to 6

                totalPints = totalPints + pints[counter]

           End For

Return totalPints

    

     Function getAverage(Real totalPints, Real averagePints)

averagePints = totalPints / 7

Return averagePints

    

     Module writeToFile(Real pints[], Real averagePints)

           f. Declare

           g. Open

           h. Write

               Declare Integer counter = 0

           i. While counter < 7

                Write __________ _________[_________]

                counter = counter + 1

               End While

j. Write ___

k. Write ____________ ________________

l. Close

     End Module

     Module readFromFile(Real pints[], Real averagePints)

               Declare String str1, str2

m. Declare ___________ ___________

           n. Open __________ “__________”

           o. Read _____________ ____________

           p. Display ____________

               Declare Integer counter = 0

           q. While counter < 7

Read ______________ _____________[_________]

                 counter = counter + 1

               End While

               counter = 0

           r. While counter < 7

Display _______________[_________]

                 counter = counter + 1

               End While

     End Module

The last item in this function is toclosetheoutFile.This is done asfollows:outFile.close()Step 12:Under the documentation and the function header for thereadFromFilefunction, create aninFileand call the open function.Pass this function the name ofthe text file and open it inreadmode.This should look as follows:inFile = open('blood.txt', 'r')Step 13:Next, read in the string ‘Pints Each Hour’ and print this to the screen.This isdone as follows:str1 = inFile.read()print str1Step 14:Read in the pints array as an entire list and print this to the screen.This is doneas follows:pints = inFile.read()print pintsprint#adds a blank lineStep 15:Read in the string ‘Average Pints’ and print this to the screen.Step 16:Read in averagePints and print this to the screen.Step 17:Close the inFile.Step 18:Run your program and for the first execution, select option 1.Run the programmore than once and enter at least 2 sets of data.