Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Lab 2 CS 2360
- Written by Ryan Ferreira
- Chapter 8
- Description – uses selection sort and binary search to find a letter
- Uses and input file of characters
- input: letters.txt
- A
- J
- B
- D
- H
- E
- F
- G
- C
- I
- Output: yes or no as to whether the character was found
- */
- #include <iostream>
- #include <fstream>
- //1. add include file for file i/o here
- #include "letters.txt"
- using namespace std;
- // Function prototypes
- bool searchList(char[], int, char);
- void selectionSort(char[], int);
- // Constant for the array size
- const int SIZE = 10;
- int main()
- {
- char letter;
- char letarr[10];
- ifstream inputfile;
- inputfile.open("letters.txt");
- do
- {
- inputfile >> c;
- cin >>
- }
- // 1. Declare an array of chars, size 10
- // 2. add filestream variable for file io
- // 3. add code to read the letters from the file letters.txt
- // into the array of chars
- // 5. add code to close the input file
- // 6. Write the function call to pass the char array to selectionSort
- //7. add code to print out the sorted list of characters
- // 8. Get a char from the user using stdinput
- // 9. Search the array for the char and indicate
- // whether it is in the array or not by calling the binary
- // search routine
- return 0;
- }
- //***************************************************
- // Function selectionSort. *
- // This function uses the selection sort algorithm *
- // to sort the array passed as an argument. The *
- // parameter size holds the number of elements. *
- //***************************************************
- void selectionSort(char array[], int size)
- {
- int startScan, minIndex, minValue;
- for (startScan = 0; startScan < (size - 1); startScan++)
- {
- minIndex = startScan;
- minValue = array[startScan];
- for (int index = startScan + 1; index < size; index++)
- {
- if (array[index] < minValue)
- {
- minValue = array[index];
- minIndex = index;
- }
- }
- array[minIndex] = array[startScan];
- array[startScan] = minValue;
- }
- }
- //***************************************************
- // Function searchList. *
- // This function searches the list array for the *
- // number passed into value. If the number is found *
- // the function returns true. Other wise, it returns*
- // false. *
- //***************************************************
- bool searchList(char array[], int numElems, char value)
- {
- bool found = false;
- int first = 0,
- last = numElems - 1,
- middle;
- while (!found && first <= last)
- {
- middle = (first + last) / 2;
- if (array[middle] == value)
- found = true;
- else if (array[middle] > value)
- last = middle - 1;
- else
- first = middle + 1;
- }
- return found;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement