The batting average for each position

124 views 9:54 am 0 Comments March 23, 2023

CH A P T E R FIVE
pairs; the first number will be between 1 and 9 and will represent the batter’s position, and the second number will be either 0 or 1 and will represent an out (0) or a hit (1). The program will continue to input data pairs until the sentinel value -1 is read. At that point, the program should output the batting average for each of the nine positions. (Note: Batting average is the number of hits divided by the number of at bats. Therefore, hits and at bats may be good values to store in parallel arrays.) The output shown below was generated using the input file “battingaverage.txt,” which may be available from your instructor.
424 pairs of data wore road from a fiYi.
The batting average for each position was: Position 1 batting average is 0.346938775510204 Position 2 batting average is 0.240740740740741 Position 3 batting average is 0.274509803921569 Position 4 batting average is 0.268292682926829 Position 5 batting average is 0.234042553191489 Position 6 batting average is 0.222222222222222 Position 7 batting average is 0.322033898305085 Position 8 batting average is 0.304347826086957 Position 9 batting average is 0.219512195121951
Ju
5-5. Batting Average and Slugging Percentage. Write a program that uses parallel arrays to determine the batting average and slugging percentage for a baseball team by position. There are nine positions on a baseball team and your program should have parallel arrays with an upper bound of 9. Your program will read data in pairs; the first number will be between 1 and 9 and will represent the batter’s position, and the second number will be either a 0 (for an out) or a 1 (for a single) or a 2 (for a double) or a 3 (for a triple) or a 4 (for a home run). The program will continue to input data pairs until the sentinel value -1 is read. At that point, the program should output the batting average and slugging percentage for each of the nine posi-tions. (Note: Batting average is the number of hits divided by the number of at bats and slug-ging percentage is the total number of bases divided by the number of at bats. You might want to have three parallel arrays in your solution.) The output shown below was generated using the input file “battingandslugging.txt,” which may be available from your instructor. (Hint: You will probably need parallel arrays for Hits, AtBats, and Bases.)
BE
424 pa rs o • ata wore roa•roe a l o. The batting average for each position was: Position 1 batting average is 0.346938775510204 Position 2 batting average is 0.240740740740741 Position 3 batting average is 0.274509803921569 Position 4 batting average is 0.268292682926829 Position 5 batting average is 0.234042553191489 Position 6 batting average is 0.222222222222222 Position 7 batting average is 0.322033898305085 Position 8 batting average is 0.304347826086957 Position 9 batting average is 0.219512195121951 The slugging percentage for each position was: Position 1 batting average is 0.530612244897959 Position 2 batting average is 0.37037037037037 Position 3 batting average is 0.509803921568627 Position 4 batting average is 0.292682926829268 Position 5 batting average is 0.25531914893617 Position 6 batting average is 0.388888888888889 Position 7 batting average is 0.627118644067797 Position 8 batting average is 0.434782608695652 Position 9 batting average is 0.268292682926829

93 Copyright 2009 Cengage Learning, Inc. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.

ARRAYS
5-6. Two Dice Simulation. Write a program that simulates the rolling of two dice many times. Each of the two dice should have a separate “Random(6) + 1″ and the two random values should be added together to get the total of the two dice roll. After rolling the two dice, the total rolled should be updated in an array of counters. For example, if the random values were 3 and 4, then the dice total is 7, and the index 7 element should be incremented by 1. Because the maximum roll possible for two dice is 12, your array of counters should have an upper bound of 12. Your program should simulate rolling the two dice many times (400 rolls is the number used in the figure below) and then display how many times each total was rolled and the percentage for each total. Finally, the program should display the roll totals visually by creating a histogram. Note: Each time you run your program you will get different totals and therefore a different histogram. In general the middle totals (such as 6, 7, 8) should be rolled more often than the extremes (2, 3, 4 on the low end and 10, 11, 12 on the high end) because there are more combinations of two dice that result in those middle values.
cl:TalUt :Zt.1 1 i’:■2”:’,.72Z Vietnn val. wat col t ::1’4: :LIZ n’l “:.”r ‘L% tho value aa, 1/..M. Z:1n n :J:: ;:11:ut 17.7Vt R: %%1 70”::,-.11T’ … ‘MR in: !I ::: 21, , ….. 4′ ,’.i.s
ao• the ie.vlly . histoura• IOW rrnm • 1.171.7.000.1..1001.00000000001.000

• J.

tamulan001XXXMOMOCOOIX030000txXXXxborunotuato
tux.3000.04X.X.Ota,0000.0ova .X.000000
5-7. Access Granted. Write a program that reads 10 username and password pairs and stores those username and password values into parallel arrays. After the arrays have been loaded, the program should behave as a login screen, continuously prompting for a username and password until a valid combination is entered and access is granted. Each time the user enters username and password data, the program should respond appropriately with one of three output mes-sages. If the username does not match one of the values in the username array, then the message should be “Usemame not found.” If the username is found in the username array, but the pass-word does not match the parallel value in the password array, then the message should be “Username and password do not match.” If the username is found and the password matches the parallel value in the password array, the message should be ‘Access granted.” The pro-gram should use a loop and continue to prompt the user for a valid username and password pair until a valid pair is entered and access is granted.
P oaso typo a va uo or INPUTUSER: yostor•ay Username not found Please typo a value for INPUTUSER:”water. Please type a value for INPUTPASS:”ice” username and password do not match Please type a value for INPUTUSER:”water” Please type a value for INPUTPASS:.oil. Access granted
94 Copyright 2009 Cengage Learning, Inc. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part.