Pseudocode Algorithm Workbench Ch 6-11

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

How many rows and how many columns are in the following array? Declare Integer points [88][100]

88 rows, 100 columns

Chapter 10

Chapter 10

Chaper 7

Chapter 7

Chapter 8 Arrays

Chapter 8

Chapter 9

Chapter 9

A bookstore keeps books on 50 racks with 10 shelves each. Each shelf holds 25 books. Declare a 3D String array to hold the names of all the books in the store. The array's three dimensions should represent the racks, shelves, and books in the store.

Constant Integer RACKS = 50 Constant Integer SHELVES = 10 Constant Integer BOOKS = 25 Declare String books [RACKS][Shelves][BOOKS]

Write a pseudocode declaration for a two-dimentional array initialized with the following table of data: 12 24 32 21 42 14 67 87 65 90 19 1 24 12 8

Constant Integer ROWS=3 Constant Integer COLS = 5 Declare Integer table [ROWS][COLS] = 12 24 32 21 42 14 67 87 65 90 19 1 24 12 8

You can read values from the keyboard and store them in an array element just as you can a regular variable. You can also output the contents of an array element.

Constant Integer SIZE = 3 //array to hold the number of hours worked by //each emplyee. Declare Integer hours [SIZE] //Declare variable to use in the loops. Declare Integer index For index = 0 To SIZE -1 Display "Enter the hours worked", index +1 Input hours [index] End For For index = 0 to SIZE -1 Display hours[index] End For

A partially filled array is used with an integer variable that holds the number of items that are actually stored in the array. If the array is empty, then 0 is stored in this variable

Constant Integer SIZE=100 //array to hold integer values Declare Integer values [SIZE] //to hold number of items that are actually stored in the array Declare Integer count = 0 //variable to hold user's input Declare Integer number //step through array Declare Integer index Display "Enter a number or -1 to quit." Input number While (number != -1 AND count < SIZE) Set values [count] = number count = count +1 Display "Enter a number or -1 to quit Input number End While Display " Here are the numbers you entered:" Display values[index] End For

Write a pseudocode declaration for a String array initialized with the following strings: "Einstein", "Newton", "Copernicus", "Kepler".

Constant Integer SIZE=4 Declare String scientists [SIZE]="Einstein", "Newton", "Copernicus", "Kepler"

Open File and read the contents into the number array:

Constant Integer SIZE=5 Declare Integer numbers[SIZE] Declare Integer index = 0 Declare InputFile numberFile Open numberFile "values.dat" While (index <= SIZE -1) AND (Not eof (numberFile)) ....Write numberFile numbers [index] ....Set index = index +1 End While Close numberFile

Saving the contents of an array to a file: Program declares an array as:

Constant Integer SIZE=5 Declare Integer numbers[SIZE]= 10,20,30,40,50

Totaling the values in an array

Constant Integer SIZE=5 Declare Integer numbers[SIZE]=2,4,6,8,10 Declare Integer total = 0 Declare Integer index For index = 0 to SIZE -1 Set total = total + numbers [index] End For Display "The total is",total

General logic for finding the highest value in an array.

Constant Integer SIZE=5 Declare Integer numbers[SIZE]=8, 1, 12, 6, 2 Declare Integer index Declare Integer highest Set highest = numbers[0] For index = 1 To SIZE -1 ....If numbers[index] > highest Then ........Set highest numbers[index] ....End If End For

General logic finding the lowest value in an array.

Constant Integer SIZE=5 Declare Integer numbers[SIZE]=8, 1, 12, 6, 2 Declare Integer index Declare Integer lowest Set lowest = numbers[0] For index = 1 To SIZE -1 ....If numbers[index] < lowest Then ........Set lowest = numbers [index] ....End If End For

Design a program that calculates the sale price of an item in a retail business. Get the item's price from the user.

Constant Real DISCOUNT_PERCENTAGE = 0.20 Module main() Declare Real regularPrice, salePrice Set regularPrice = getRegularPrice() Set salePrice = regularPrice - discount (regularPrice) Display "The sale price is $", salePrice End Module Function Real getRegularPrice() Declare Real price Display "Enter the item' s regular price." Input price Return price End Function Function Real discount (Real price) Return price * DISCOUNT_PERCENTAGE End Function

Reading Data from a File: 1. Declare 2. Open 3. Read /write 4. Close

Declare InputFile inventoryFile Open inventoryFile "inventory.dat" Read inventoryFile itemName (could be a name) Write inventoryFile "no3" Close inventoryFile

2. Design a program that opens the my_name.dat file that was created by the algorithm question 1, reads your name from the file, displays the name on the screen, and then closes the file.

Declare InputFile myFile Declare String name1 Open myFile "my_name.dat" Read myFile name1 Close myFile Display "My name is, " name1

4. Design an algorithm that does the following; Opens the number_list.dat file that was created by the algorithm created in question 3, read all of the numbers from the file and displays them, and then closes the file.

Declare InputFile numberList Declare Integer numbers Open numberList "number_list.dat" Display "Here are the numbers: " While NOT eof (numberList) .........Read numberList ,numbers .........Display numbers End While Close numberList

Design an algorithm, that uses a For loop to write the numbers 1 through 10 to a file.

Declare Integer counter Declare OutputFile myFile Open myFile "myfile.dat" For counter = 1 To 10 ....Write myFile, counter End For Close myFile

When this statement executes, the random function is called. The function generates a random number in the range of 1 through 10. The value is returned and then sent to the Display statement.

Declare Integer counter For counter = 1 To 5 Display random (1,10) End For

The function call toInteger (2.5) will return 2 The variable i contains the value 2 after these statements execute.

Declare Integer i Declare Real r = 2.5 Set i = toInteger (r)

If the following were actual code, the variable r would contain the value 7.0 after these statements execute.

Declare Integer i = 7 Declare Real r Set r = toReal (i)

Insertion sort module sorts the first two elements of an array before inserting the remaining elements into that sorted part of the array.

Declare Integer index Declare Integer scan Declare Integer unsortedValue For index = 1 To arraySize -1 ...Set unsortedValue = array [index] ...Set scan = scan -1 While scan > 0 AND array[scan -1] < array [scan] .....Call swap (array[scan -1], array [scan]) .....Setscan = scan -1 End While ...Set array [scan] = unsortedValue End For

Saving the contents of an array to a file:cont. the pseudocode opens a file named values.dat and writes the contents of each element of the numbers array to the file:

Declare Integer index Declare OutputFile numberFile Open numberFile "values.dat" For index = 0 To SIZE -1 ...Write numberFile numbers [index] End For Close numberFile

Assume the following declarations appear in a pseudocode program. Also, assume that values have been stored in each element of firstArray. Design an algorithm that copies the contents of first Array to secondArray. Constant Integer SIZE = 100 Declare Integer firstArray[SIZE] Declare Integer secondArray [SIZE]

Declare Integer index For index = 0 To SIZE - 1 ....Set secondArray[index] = firstArray[index] End For

5. Modify the algorithm that you designed in question 4 so it adds all of the numbers read from the file and displays their total.

Declare Integer index = 0 Declare InputFile numberList Declare Integer total = 0 Open numberList "number_list.dat" While (index <= SIZE - 1) AND (NOT eof(numberList)) total = total + numbers[index] write numberList numbers[index] Set index = index + 1 End While Close numberList Display total

Bubble Sort module makes passes through and compares the elements of the array, certain values move toward the end of the array with each pass.

Declare Integer maxElement Declare Integer index For maxElement = arraySize -1 To 0 Step -1 ...For index = 0 To maxElement -1 .....If array [index] > array [index =1] Then ........Call swap (array[index], array [index +1] .....End If ...End For End For

Square Root library Function

Declare Integer number Declare Real squareRoot Display "Enter a number" Input number Set squareRoot = sqrt (number) Display "The square root of that number is ", squareRoot

This program uses a For loop that iterates five times. Inside the loop the statements call the random function to generate a random number in the range of 1 through 100.

Declare Integer number, counter For counter = 1 To 5 Set number = random (1,100) Display number End For

Write a pseudocode statement that generates a random number in the range of 1 through 100 and assigns it to a variable named rand.

Declare Integer rand Set rand = random (1,100) Display rand

A pseudocode program contains the following function definition: Function Integer cube (Integer num) Return num * num * num End Function Write a statement that passes the value 4 to this function and assigns its return value to the variable result.

Declare Integer result result = cube (4)

Assume a program has the following declarations: Write pseudocode with a set of nested loops that stores the value of 99 in each element of the info array. Constant Integer ROWS= 100 Constant Integer COLS = 50 Declare Integer info [ROWS][COLS]

Declare Integer row Declare Integer col For row = 0 To ROWS -1 ...For col = 0 To COLS -1 ........Set info = [row][col]=99 ...End For End For

Selection sort module moves elements to their final sorted position in the array.

Declare Integer startScan Declare Integer minIndex Declare Integer minValue Declare Integer index For startScan = 0 To arraySize -2 ...Set minIndex = startScan ...Set minValue = array [startScan] For index = startScan =1 To arraysize -1 ...If array [index] < minValue ........Set minValue = array [index] ........Set minIndex = index ...End If End For Call swap (array [minIndex}, array [startScan]) End For

Append data to a file - write new data to the end of the data that already exists in the file

Declare OutputFile AppendMode myFile Open myFile "friends.dat" Write myFile "Matt" Close myFile

6. Write pseudocode that opens an output file with the external name number_list.dat, but does not erase the file's contents if it already exists.

Declare OutputFile AppendMode numberList Open numberList "number_list.dat" Close numberList

1.Design a program that opens an output file with the external name my_name.dat, writes your name to the file, and the closes the file.

Declare OutputFile myFile Open myFile "my_name.dat" Write myFile "Marion" Close myFile

3. Design an algorithm that does the following: Opens an output file with the external name number_list.dat , uses a loop to write the number 1 through 100 to the file, and then closes the file.

Declare OutputFile numberList Declare Integer counter Open numberList "number_list.dat" For counter = 1 To 100 .....Write numberList ,counter End For Close numberList

Example of how the currencyFormat function can be used.

Declare Real amount = 6450.879 Display currencyFormat (amount) It would display: $6,450.88

Design an algorithm that prompts the user to enter a number in the range of 1 through 100 and validates the input.

Declare Real number Display " Enter a number in the range of 1 through 100" Input number While number < 1 Or number > 100 Display " ERROR: The number cannot be less" Display "than 1 or greater than 100." Display " Enter the correct number." Input number End While

Design an algorithm that prompts the user to enter a positive nonzero number and validates the input.

Declare Real number Display "Enter a positive nonzero number Input number While number < 0 Display "ERROR: The number cannot be less than zero." Display "Enter the correct number." End While

Design an algorithm that prompts the user to enter a number that is greater than 99 and validates the input.

Declare Real number Display "Enter nu that is greater than 99." Input number While number <=99 Display " ERROR: The number cannot be less" Display "than or equal to 99." Display " Enter the correct number." Input number End While

Write a statement that declares an array named salesAmounts that can hold 7 real numbers.

Declare Real salesAmounts [7]

The loop displays "Hello!" and then propts the user to enter "Y" to see it again. The expression toUpper (again) == "Y" will be true if the user enters either "y" or "Y"

Declare String again Do Display "Hello!" Display "Do you want to see that again? (Y=Yes)" Input again While toUpper (again) =="Y"

Design an algorithm that prompts the user to enter "yes" or "no" and validates the input (Use a case-insensitive comparison.)

Declare String answer Display "yes or no" Input answer While toLower(answer) != "yes" AND toLower(answer) != "no" Display "Please answer yes or no." Input answer End While

append Function accepts two strings as arguments, it returns a third string that is created by appending string2 to the end of string1. Example

Declare String lastName = "Conway" Declare String salutation = "Mr." Declare String properName Set properName = append (salutation, lastName) Display properName It would display: Mr. Conway

toUpper toLower Functions

Declare String str = "Hello World!" Display toUpper (str) Declare String str = "WARNING!" Display toLower (str)

Design an algorithm that prompts the user to enter a secret word the secret word should be at least 8 characters long. Validate the input.

Declare String word Display "Enter the secret word." Input word While length(word) < 8 Display "The secret word must be at least eight" Display "characters long." Display "Enter your new secret word." Input word End While

7. A File exists on the disk named students.dat. The File contains several records, and each record contains two Fields: (1) the student's name, and (2) the student's score for the final exam. Design an algorithm that deletes the record containing "John Perez" as the student name

Declare string searchFile Display "Enter the file you would like to delete" Input searchFile While NOT eof (students) If description != searchFile Write tempFile description End If End While

You are designing a program that reads a test score and you want to make sure the user does not enter a value less than 0. Input validation loop to reject any input value that is less than 0

Display "Enter a test score." Input score While score < 0 Display "ERROR: The score cannot be less than 0." Display "Enter the correct score." Input score End While

The length of a password is checked to make sure it is at least six characters long. length function returns the length of a string

Display "Enter your new password." Input password If length (password) < 6 Then Display "Your password must be at least six characters long." End If

Assume names is an Integer array with 20 elements. Design a For loop that displays each element of the array.

For index = 0 To 19 Display names[index] End For

Assume the arrays numberArray1 and numberArray2 each have 100 elements. Design an algorithm that copies the values in nmberArray1 to numberArray2.

For index = 0 to 99 Set numberArray2[index] = numberArray1[index] End For

Design Boolean function named isEven that accepts a number as an argument and returns True if the number is even, or False otherwise.

Function Boolean isEven (Integer number) Declare Boolean status If number MOD 2 == 0 Set status = True Else Set status = False End If Return status End Function or If isEven (number) Then Display "The number is even." Else Display "The number is odd." End If

Design a function named timesTen that accepts an Integer argument. When the function is called, it should return the value of its argument multiplied times 10.

Function Integer timesTen (Integer num) Declare Integer num Return num *10 End Function

Design an algorithm for a function that accepts an Integer array as an argument and returns the total of the values in the array.

Function Real getTotal(Integer array[], Integer arraySize) // Loop counter Declare Integer index // Accumulator, initialized to 0 Declare Integer total = 0 // Calculate the total of the array elements. For index = 0 To arraySize - 1 Set total = total + array[index] End For // Return the total. Return total End Function

The following pseudocode statement calls a function named half, which returns a value that is half that of the argument (Assume both the result and number variables are Real). Write pseudocode for the function. Set result = half(number)

Function Real half (Real number) Return number/2 End Function

Design a function named getFirstName that asks the user to enter his or her first name, and return it.

Function String getFirstName () Declare String name Display" Enter your name." Input name Return name End Function

Design a program that will ask the user to enter a number, and then determine whether that number is even or odd. (MOD divides two integers and returns the remainder of the division.

If number MOD 2 == 0 Then Display "The number is even" Else Display "The number is odd" End If

Design a swap module that accepts two arguments of the Real data type and swaps them.

Module swap (Real Ref a, Real ref b) ....Declare Real temp ....Set temp = a ....Set a = b ....Set b = temp End Module

This statement calls the pow function, passes 4 and 2 as arguments. The function returns the value of 4 raised to the power of 2, which is assigned to the area variable.

Set area = pow (4,2)

The statement works like this: The value of the expression a^2 +b^2 is calculated, that value is passed as an argument to the sqrt function. The sqrt function returns the square root of the argument, which is then assigned to the variable c.

Set c = sqrt (a^2 +b^2)

Write a pseudocode statement that assigns the value 100 to the very last element in the points array. Declare Integer points [88][100]

Set points [87][99] = 100

The following statement converts the contents of the str variable to a Real and stores it in the realNumber variable

Set realNumber = stringToReal (str)

The following statement converts the contents of the str variable to a Real and stores it in the realNumber variable.

Set realNumber = stringToReal (str)

This statement calls the sqrt function, passes 16 as an argument. The function returns the square root of 16, which is then assigned to the result variable.

Set result = sqrt (16)

A loop that reads all of the items from the file accociated with myFile?

While NOT eof(myFile) ...Read myFile item End While

8. A file exists on the disk named students.dat. The file contains several records, and each record contains two fields: (1) the student's name, and (2) the student's score for the final exam. Design an algorithm that changes Julie Milan's score to 100

While Not eof (students) Read students description, studentScore If description == searchValue Then Write tempFile description, newScore Set found = True Else Write tempFile description, newScore End If

Assume that a program has two String variables named str1 and str2. Write a pseudocode statement that assigns an all upercase version of str1 to the str2 variable.

str2 = toUpper(str1)


Set pelajaran terkait

Midterm-Management Information System

View Set

Prescribing for Pediatric Patients

View Set

Jobs, Careers, & Education- Education and Training

View Set