Chapter 8- Programming Exercises

NOTE: There are no TABS on the codes. THESE ARE MY PERSONAL solutions to the problem. Copy at your own risk.

#2. Lottery Number Generator
REQUIREMENT:
This program generates a 7-digit lottery number. It should have an Integer array with 7 elements. It has a loop that steps through the array, randomly generating a number between 0 and 9 for each element. Another Loop displays the contents of the array.

Pseudo Code:
Module main()
//Declaration
Declare Integer SIZE = 7
Declare Integer numbers[SIZE]
//create numbers
makeNumbers(numbers, SIZE)
//display winning number
Display “The winning numbers are: ”
showWinner(numbers, SIZE)
End Module

//Module makeNumbers : populates an array of specified size with random numbers between 0 and 9
Module makeNumbers (Integer Ref array[], Integer asize)
Declare Integer count = 0
For count = 0 to asize-1
Set array[count] = random(0,9)
End For
End Module

//Module showWinner : display all the numbers in an array of specified size
Module showWinner (Integer array[], Integer asize)
Declare Integer count = 0
For count = 0 to asize-1
Display array[count]
End For
End Module

#4. Number Analysis Program

REQUIREMENT
This program asks the user to enter a series of 20 numbers. The program stores the numbers in an array and then display the following data:
– The lowest number in the array
– The highest number in the array
– The total numbers in the array
– The average numbers in the array

Pseudo Code
Module main()
//Declaration
Declare Integer SIZE = 20
Declare Integer count = 0
Declare Integer numbers[SIZE]
Declare Integer num
Declare Integer lowest = 0
Declare Integer highest = 0
Declare Integer total = 0
Declare Integer average = 0
//Read Input
Display “Enter a series of 20 numbers below:”
For count = 0 to SIZE-1
Display “Enter Input #”, count + 1
Input num
Set numbers[count] = num
End For
//Set results
Set lowest = lowestNum(numbers)
Set highest = highestNum(numbers)
Set total = SIZE
Set average = averageNum(numbers)
//Display results
showResut(lowest, highest, total, average)
End Module

Function lowestNum(Integer array[], int size)
Declare Integer value = 0
Declare Integer cnt = 0
For cnt = 0 to size-1
If value > array[cnt] Then
value = array[cnt]
End If
End For
Return value
End Function

Function highestNum(Integer array[], int size)
Declare Integer value = 0
Declare Integer cnt = 0
For cnt = 0 to size-1
If value < array[cnt] Then value = array[cnt] End If End For Return value End Function Function averageNum(Integer array[], int size) Declare Integer sum = 0 Declare Integer cnt = 0 For cnt = 0 to size-1 Set sum = sum + array[cnt] End For Return (sum / size) End Function //Module showResult : display result Module showResult (plowest, phighest, ptotal, paverage) Display "lowest=", plowest, " highest=", phighest, " total=", ptotal, " average=", paverage End Module

Leave a Reply

Your email address will not be published. Required fields are marked *