Lecture 6 - Arrays

Back to Previous Lecture
Up to Main Page
Forward to Next Lecture

Filling An Array With Values

The program array.c shows you how to fill an array with random numbers. The program then prints those numbers out. This program is used as the basis for the next few programs.

Finding Entries in an Array

There are several programs that demonstrate how to find entries in an array. All these programs build on the array.c program from above. They populate an array with random numbers. They then loop asking the user for a number to find, and then report the results. This continues until the user simply presses ENTER, indicating the are done.

The program first.c finds the first entry in the table that matches the number the user entered. The program last.c finds the last entry in the table. The program count.c tells you how many times the given number appears in the table. Finally, the program list.c shows you how to not only count how many times a number appears, but it will also display at what locations they appear.

Pay particular attention to the list.c program. It displays different types of messages, depending on whether the number was found, and how many times it was found. Notice that if a number is found more than once, it displays the locations using several printf() functions for the one line. You can do this by simply not including a new line character (\n) at the end of your printf() statement. Notice also that we add the word "and" before the number at the end of the list.

Marking the End of a List

You have actually done this type of operation before. Each time you look at a character string, you search until you find the binary zero at the end of the string. The binary zero is a marker indicating the end of the string.

You can modify any of the search functions to use a marker at the end of the table. Simply insert a negative one someplace within the table, indicating that the entry marks the end of the table. The program marker.c is a modified version of last.c that uses a minus one to mark the end of the table. Similar changes could be made to the other programs to do the same thing. You might want to try your hand at changing one of the other programs.

Sorting

The program small.c sorts a random table by moving the smallest numbers to the top of the array. It starts out by moving the smallest number in the array to the first location. Then it find the smallest number in the rest of the array (which would be the second smallest number), and moves that to the second position. This process continues until the entire table has been sorted.

The program big.c also sorts a random table, but does it in the opposite way as the previous program. The smallest number is moved to the last entry, the second smallest to the next to last entry, etc.

By changing the above programs to use > rather than <, you can sort the tables in the opposite direction.

Finally, the bubble.c program illustrates how to write a bubble sort. A bubble sort goes through the array comparing adjacent entries. If a pair are found to be out of order, the two entries are swapped. By the end of the first pass, the largest entry in the table is in the last position of the table. At the end of the next pass, the second largest number will be in the next to last position in the table. If there is ever a pass where no swapping occurred, then the table is entirely sorted.

Back to Previous Lecture
Up to Main Page
Forward to Next Lecture