ch10LABcs450

pdf

School

University of Nevada, Reno *

*We aren’t endorsed by this school

Course

450

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

2

Uploaded by ameliapunr

Report
Amelia Pasco CS 450.1001 11/7/23 Chapter 10 Lab Assignment Answer Form Part 1 Questions: 1. What results are displayed if you type 3 in response to the first prompt and any number < 3 in the second prompt? The program will work correctly and return the correct value from the array. 2. What results are displayed if you type 10 in response to the first prompt and 3 in the second prompt? The value returned is ‘7’. 3. What results are displayed if you type 10 in response to the first prompt and 7 in the second prompt? The value returned is ‘3’. 4. What results are displayed if you type 12 in response to the first prompt and 3 in the second prompt? The error “IndexError: list assignment index out of range” is displayed. This happened because the vals array size was set to 10, and 12 is not in that range. 5. What results are displayed if you type 10 in response to the first prompt and 12 in the second prompt? You will again get the “IndexError: list assignment index out of range” error message as 12 is not in the index range for the vals array. Part 2. Questions: 1. How many temperatures were read after step 3? 10 temperatures 2. What were the results after step 5 and why did this happen? The error message “IndexError: list assignment index out of range” was displayed because the temps array index was set to 10 and there were more than 10 temperature values in the text file. 3. Record the code from the fixed program in step 6 below: temps = [None]*10 numTemps = 0 infile = open("temps.txt", "r") for line in infile.readlines(): for i in line.split():
if numTemps < len(temps): temps[numTemps] = float(i) numTemps += 1 else: print("Array size exceeded. Resizing the array.") new_temps = [None] * (len(temps) * 2) # Double the array size new_temps[:len(temps)] = temps # Copy old values to the new array temps = new_temps # Update the reference to the new array temps[numTemps] = float(i) numTemps += 1 print(str(numTemps) + ' temperatures were read.')
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help