Saturday, April 03, 2010

Introduction to Programming: Starting Out With Programming Logic and Design: Chapter 11: Menu Driven Programs

Starting Out with Programming Logic and Design (2nd Edition)

1. Introduction to Menu Driven Programs

A menu driven program displays a screen of options that a user can make a choice from, usually by typing a number or a letter.

Using Decision Structure to Perform Menu Selections

To make menus easier in programs most Languages have a Case structure. This allows for the user to enter input and the program Check it against what the program expects as inputs. While it is the simplest, it is not the only way. An if..than...else series will do the same thing.

Validating the Menu Selection

The case selection should have as its default if nothing matches a way to exit the program gracefully or even better, loop back and ask the user to do it again. Another way to do this would be to use while loop after the display asking for input and validate that you have gotten a correct response.

2. Modularizing a Menu Driven Program

In order to simplify things in menu driven programs, the suggestion is to break the parts of the program into modules. There would be one main module that would call a menu module. The menu module would display the menu and validate input returning the menu choice to the main module. With that choice the main module would call the module it needs.

3. Using a Loop to Repeat the Menu

If using a menu, you most likely want to leave the menu up till the user exits it. The best way to do this is to give a menu choice to exit the program and then use a loop to keep presenting the menu when a program ends. When a user exits a module it will present the menu again till the user exits it.

4. Multiple-Level Menus

Big menus are not good so often there is a need to break them up into smaller menus with menu choices calling up other menus. To do this you would use the same modular style and call the sub menus from main. Then that module would call the other menus.

 

The Psychology of Menu Selection: Designing Cognitive Control at the Human/Computer Interface (Human/Computer Interaction)


Introduction to Programming: Starting Out with Programming Logic and Design: Chapter 9: Sorting and Searching Arrays

Starting Out with Programming Logic and Design (2nd Edition)

1. The Bubble Sort Algorithm

Sorting Algorithms

Often information in an array will need to be sorted in some way. It could be by number or alphabetical, and it could be ascending or descending order. One type of sort is the bubble sort. It is called the bubble sort because as you make passes thru it, values will bubble to the top (or bottom). So how does it work? Let's assume ascending order.

On the first pass thru, position 0 and 1 are compared. If 0 > 1 than they are swapped. Then 1 and 2 are compared and the same is done. This continues till all elements in the array are done. When this pass is done the last element should have the largest value in it. The next time thru, we do the same except we do it till the size of the array minus 1. We subtract 1 each time. Why? Because each pass thru will leave the largest value at the end.

Swapping Array Elements

We cannot just make the 0 element the 1 element and vice-versa. This is not possible so we must use temporary locations. We must assign element 0 to a temporary location, switch 1 into 0 and then move the temporary location into 1. This is the same for all elements that we compare and need to be swapped.

Designing the Bubble Sort Algorithm

Usually the bubble sort algorithm is a module that data is sent to. The details for its pseudocode are too complicated for these notes. They are in the book.

Sorting an Array of Strings

As noted in previous chapters, characters can be compared by each letters numeric equivalent. Just substitute a string for the number.

Sorting in Descending Order

To get descending order, change the greater than to a less than in the comparison.

2. The Selection Sort Algorithm

The bubble sort is simple, easy to understand but not efficient. A selection sort algorithm will take fewer steps to do. A scan is done of the array to find the lowest number. A pointer to it is stored in a temporary location. When the scan is done the swap is made between the lowest number and the first element. Then starting with the second element we scan again for the lowest number and swap it. We keep doing this cycle until we are at the last element.

3. The Insertion Sort Algorithm

The insertion sort algorithm swaps the first two elements and then looks at the third and inserts it where it needs to be in the list. This is done with all subsequent elements.

4. The Binary Search Algorithm

While the sequential search is simple it is not efficient. If we have a large array, let’s say 10,000, there is a possibility that we will have to go through 10,000 elements to find what we are looking for. By putting the elements is order first, if we search to the middle location we can determine if it is before or after that location. We have eliminated half the array. We can then keep halving things till we find what we are looking for.

Efficiency of a Binary Search

Because the binary search eliminates half the field each time it cuts the amount of time it will take to do a search. In fact a 1000 item array can be sorted in 10 tries at a maximum.

 

Sorting and Sort Systems (The Systems programming series)


Introduction to Programming: Programming Logic and Design: Chapter 10:Files

Starting Out with Programming Logic and Design (2nd Edition)

1. Introduction to File Input and Output

All programs looked at so far make us enter information each time. Programs can be allowed to read data from a disk as well as write it to a disk to be used later. An output file is one that a program writes to. An input file is one that a program would read data from. To use any data a program must:

  1. Open the file - make it so that the file is available to read or write.
  2. Process the file - information is either read from or written to the disk.
  3. Close the file - disconnect itself from the file so it cannot use it any more.

Types of Files

Text - alphabetic and numeric characters as well as some symbols. Can be open in a simple text editor.

Binary - data that has been stored in a special way and cannot be read easily in a text editor.

File Access Methods

In accessing data from a disk the files are stored two ways.

Sequential - think cassette tape. In order to get to a certain song you must go through each one to get to it. Likewise with data, if you the last piece of data you have to go through all the rest of them.

Direct access - (random access) think CD player, you can access the last song directly. Likewise with direct access, you can jump directly to the data you want.

We will be looking at sequential access files in this chapter.

Creating a File and Writing Data to it

The general way to create a file is multi-stepped:

  • Use a declare statement - general format - declare outputfile filename - filename is a name we use in the program, outputfile is the name of the file we will save it as
  • Next we open the file - general format - open filename "customer.dat" - filename like before, customer.dat the name of the file we will create. WARNING - If the file exist it will be overwritten
  • Write the data to the file (actually file buffer) - general format - write filename data - data is the data that you want to write
  • Close the file - whatever information that has not been written from the buffer gets done now and file is marked as closed.

Delimiters and the EOF Marker

A delimiter is a piece of information that separates the data into recognizable chunks. An EOF (end of file) marker is a piece of information that tells the program the data has stopped and the file goes no further.

Reading Data From a File

Reading data from a file is similar Process.

Declare Inputfile filename

Open filename "diskfile.name"

Read filename itemname

Similar naming as above only now the read takes information in to memory. We then close the file.

Close filename

When the program reads in data it will read to the delimiter and then make a pointer to where it left off. The next time it reads it will pick up from that pointer (read position).

Appending Data to an Existing File

As noted before, opening a file to be written to will cause the file to be overwritten if it exists. For this reason programming languages use an append mode.

Declare output file appendmode filename

Open filename "nameOn.dsk"

Write filename data

2. Using Loops to Process Data

Usually if a file is used to read or write data on the drive a loop is involved.

Reading a File with a Loop and Detecting the End of the File

Often we will not know how much data we have so we cannot use a numbered loop. Most languages provide for this with an EOF function that the while loop can test for. As soon as the EOF marker is noted, it will go true and drop out of the loop.

3. Using Files and Arrays

By using loops, we can loop through an array and move the data to a file by writing one element at a time to the file. Likewise we could read the data in one chunk at a time and then place it into the array.

4. Processing Records.

When data is written it is often done in records and fields. A field is a piece of data, and all the fields that are related to one item are records.

Writing Records

By using one write statement we can put several variables of data together and write a record at a time.

Reading Records

Likewise we can read in several variables of information at one time by doing a read statement with multiple variable names.

5. Control Break Logic

Often there is q need to limit the amount of information that will display. For this we will do a control break routine. To do this put a counter in the loop that displays information and when it gets to the maximum display, display a message' wait for a keystroke and the reset counter to start again.

 

 

Hello World! Computer Programming for Kids (and Other Beginners): Computer Programming for Kids and Other Beginners