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.
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.
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