Question

The following program creates and processes binary files containing integers. Can someone help me write a...

The following program creates and processes binary files containing integers. Can someone help me write a mechanism for printing the integers as a string (separated by a single white space) in numeric order (smallest to greatest) AND 10 integers per line. I've marked below where the numbers are outputted.

Here's the code:

from sys import argv
from pickle import dump
from random import randint
from pickle import load

(*'s indicate indentation)

if (argv[1] == 'c'):

****output_file = open(argv[2], 'wb')

****file_data = []

****for i in range(int(argv[3])):
******file_data.append(randint(int(argv[4]), int(argv[5])))
****dump(file_data, output_file)
****output_file.close()

****print('{} random integer values have been written to the file {}.'

**********.format(argv[3], argv[2]))
****input('Press Enter to continue ... ')

elif (argv[1] == 'p'):

****num_spaces = 1

****input_file = open(argv[2], 'rb')

****while True:
******try:
******* i = load(input_file)

******* print('\nThe maximum value from the file is {}'.format(max(i)))
******* print('The minimum value from the file is {}'.format(min(i)))

******* print('The sum of the values from the file is {}'.format(sum(i)))
******* print('The average of the values from the file is'
************* ' %.2f' % (sum(i) / len(i)))
******* print("Here are the integers from integers.dat:")
******* print('And here are all the values from the file, sorted ascending'
************* ',\n with a maximum of 10 per line, and with each value in'
************* ' {} spaces:'.format(num_spaces))

******* print(i, end=(' ' * int(num_spaces)))    #This is where the integers are printed out

******except EOFError:
********input_file.close()
********break

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Do reach out in the comment section if you have any doubt. also a like would be highly appreciated.

This is your modified code. which is working as expected. please have a look at input-output to ensure.

 from sys import argv from pickle import dump from random import randint from pickle import load if (argv[1] == 'c'): output_file = open(argv[2], 'wb') file_data = [] for i in range(int(argv[3])): file_data.append(randint(int(argv[4]), int(argv[5]))) dump(file_data, output_file) output_file.close() print('{} random integer values have been written to the file {}'.format(argv[3], argv[2])) input('Press Enter to continue ... ') elif (argv[1] == 'p'): num_spaces = 1 input_file = open(argv[2], 'rb') while True: try: i = load(input_file) print('\nThe maximum value from the file is {}'.format(max(i))) print('The minimum value from the file is {}'.format(min(i))) print('The sum of the values from the file is {}'.format(sum(i))) print('The average of the values from the file is %.2f' % (sum(i) / len(i))) print("Here are the integers from integers.dat:") print('And here are all the values from the file, sorted ascending,\n with a maximum of 10 per line, and with each value in {} spaces:'.format(num_spaces)) for j in range(0, len(i), 10): print(*i[j:j+10]) # This is where the numbers are printed out. except EOFError: input_file.close() break 

Here is your input and output part.

(blackcat) expertname@expert:~/Desktop/exprimental$ python HomeworkLib.py c test.dat 20 1 100 20 random integer values have been written to the file test.dat Press Enter to continue ... (blackcat) expertname@expert:~/Desktop/exprimental$ python HomeworkLib.py p test.dat The maximum value from the file is 80 The minimum value from the file is 4 The sum of the values from the file is 808 The average of the values from the file is 40.40 Here are the integers from integers.dat: And here are all the values from the file, sorted ascending, with a maximum of 10 per line, and with each value in 1 spaces: 30 17 63 75 26 28 35 4 47 48 65 39 78 17 80 29 28 36 46 17
Add a comment
Know the answer?
Add Answer to:
The following program creates and processes binary files containing integers. Can someone help me write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT