Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Parameters, Indefinite, Loops and Loops Patterns - Lecture Slides | CSSE 120, Assignments of Software Engineering

Material Type: Assignment; Class: Intro to Software Development; Subject: Computer Sci & Software Engr; University: Rose-Hulman Institute of Technology; Term: Unknown 1989;

Typology: Assignments

Pre 2010

Uploaded on 08/18/2009

koofers-user-jzx
koofers-user-jzx 🇺🇸

10 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PARAMETERS, INDEFINITE
LOOPS, AND LOOP PATTERNS
CSSE 120Rose Hulman Institute of Technology
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Parameters, Indefinite, Loops and Loops Patterns - Lecture Slides | CSSE 120 and more Assignments Software Engineering in PDF only on Docsity!

PARAMETERS, INDEFINITE

LOOPS, AND LOOP PATTERNS

CSSE 120—Rose Hulman Institute of Technology

Speed Reading HW Questions?

 It is in HW 10 folder on ANGEL

 You should have already started working on it

 Ask questions if you got stuck

 Finish before Session 12

Functions mutating parameters

 Can we write a function that exchanges the values of its two

parameters?

attempt to exchange two integers

def swapInts(x, y): x,y = y,x

attempt to exchange two elements of a list

def swapListElements(list, i, j): list[i], list[j] = list[j], list[i]

x,y = 2, 5 print 'Before "swapInts": x=%d, y=%d' %(x, y) swapInts (x, y) print 'After "swapInts": x=%d, y=%d' %(x, y)

aList = [ 3 , 4 , 5 , 6 ] print “State of list before swapListElements:",aList swapListElements(aList, 1 , 3 )

print “State of list after swapListItems:",aList

Modifying Parameters

 How do functions send information back?

 Return statements

 Mutating parameters

 Value of actual parameter must be a mutable object

 State of the mutable object is changed

 The actual parameter itself is NOT changed since it refers to the same object

 Parameter is still passed by value

Some indefinite loop patterns

 Interactive loops

 Sentinel loops

 File loops

 post-test loops

 "loop and a half"

Interactive: Make the user count

 Not the best idea! (Why not?)

# average1.py

# A program to average a set of numbers

# Illustrates counted loop with accumulator

from win_in import *

def main():

n = win_input( "How many numbers do you have? ")

sum = 0.

for i in range(n):

x = win_input( "Enter a number >> ")

sum = sum + x

print "\nThe average of the numbers is", sum / n

main()

Sentinel loop

For this program, a negative input number is the sentinel that signals "no more data"

 User signals end of data by a special "sentinel"value.

 Note that the sentinel value is not used in calculations.

# average3.py

# A program to average a set of numbers

# Illustrates sentinel loop using negative input as sentinel

from win_in import *

def main():

sum = 0.

count = 0

x = win_input( "Enter a number (negative to quit) >> ")

while x >= 0:

sum = sum + x

count = count + 1

x = win_input( "Enter a number (negative to quit) >> ")

print "\nThe average of the numbers is", sum / count

Non-numeric Sentinel

 What if negative numbers are legitimate values?

 Again note: sentinel value is not used in calculations.

# average4.py
# A program to average a set of numbers
# Illustrates sentinel loop using the empty string as sentinel
from win_in import *
def main():
sum = 0.
count = 0
xStr = win_raw_input( "Enter a number ( to quit) >> ")
while xStr != "":
x = eval(xStr)
sum = sum + x
count = count + 1
xStr = win_raw_input( "Enter a number ( to quit) >> ")
print "\nThe average of the numbers is", sum / count

File loop

 Use a for loop as we have seen before:

 Also note the conditional execution of main()

# average5.py

# Computes the average of numbers listed in a file.

from win_in import *

def main():

fileName = win_raw_input( "What file are the numbers in? ")

infile = open(fileName, 'r')

sum = 0.

count = 0

for line in infile:

sum = sum + eval(line)

count = count + 1

print "\nThe average of the numbers is", sum / count

if name == 'main':

main()

Individual Exercise on Using loops

 Define function listAndMax() in module listMax.py that

 Prompts the user to enter numbers, one at a time

 Uses a blank line () as sentinel to terminate input

 Accumulates the numbers in a list

 Returns two values:

 the list of numbers entered in the order they were entered

 the maximum value of the numbers entered

 Define function main() in module listMax.py that

 Calls listAndMax()

 Prints the list of numbers entered

 Prints the maximum value of the list of numbers