Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. /* Lab 2 CS 2360
  2. Written by Ryan Ferreira
  3. Chapter 8
  4.  
  5. Description – uses selection sort and binary search to find a letter
  6. Uses and input file of characters
  7.  
  8. input: letters.txt
  9. A
  10. J
  11. B
  12. D
  13. H
  14. E
  15. F
  16. G
  17. C
  18. I
  19.  
  20. Output: yes or no as to whether the character was found
  21. */
  22. #include <iostream>
  23. #include <fstream>
  24. //1. add include file for file i/o here
  25. #include "letters.txt"
  26. using namespace std;
  27.  
  28. // Function prototypes
  29. bool searchList(char[], int, char);
  30. void selectionSort(char[], int);
  31.  
  32. // Constant for the array size
  33. const int SIZE = 10;
  34.  
  35. int main()
  36. {
  37.     char letter;
  38.     char letarr[10];
  39.  
  40.     ifstream inputfile;
  41.     inputfile.open("letters.txt");
  42.     do
  43.     {
  44.         inputfile >> c;
  45.         cin >>
  46.     }
  47.        
  48.     // 1. Declare an array of chars, size 10
  49.  
  50.     // 2. add filestream variable for file io
  51.  
  52.     // 3. add code to read the letters from the file letters.txt
  53.     // into the array of chars
  54.  
  55.     // 5. add code to close the input file
  56.  
  57.  
  58.     // 6. Write the function call to pass the char array to selectionSort
  59.  
  60.     //7. add code to print out the sorted list of characters
  61.  
  62.     // 8. Get a char from the user using stdinput
  63.  
  64.     // 9. Search the array for the char and indicate
  65.     // whether it is in the array or not by calling the binary
  66.     // search routine
  67.  
  68.  
  69.     return 0;
  70. }
  71.  
  72. //***************************************************
  73. // Function selectionSort.                          *
  74. // This function uses the selection sort algorithm  *
  75. // to sort the array passed as an argument. The     *
  76. // parameter size holds the number of elements.     *
  77. //***************************************************
  78.  
  79. void selectionSort(char array[], int size)
  80. {
  81.     int startScan, minIndex, minValue;
  82.  
  83.     for (startScan = 0; startScan < (size - 1); startScan++)
  84.     {
  85.         minIndex = startScan;
  86.         minValue = array[startScan];
  87.  
  88.  
  89.         for (int index = startScan + 1; index < size; index++)
  90.         {
  91.             if (array[index] < minValue)
  92.             {
  93.                 minValue = array[index];
  94.                 minIndex = index;
  95.  
  96.             }
  97.  
  98.         }
  99.         array[minIndex] = array[startScan];
  100.  
  101.         array[startScan] = minValue;
  102.  
  103.     }
  104. }
  105.  
  106. //***************************************************
  107. // Function searchList.                             *
  108. // This function searches the list array for the    *
  109. // number passed into value. If the number is found *
  110. // the function returns true. Other wise, it returns*
  111. // false.                                           *
  112. //***************************************************
  113.  
  114. bool searchList(char array[], int numElems, char value)
  115. {
  116.     bool found = false;
  117.     int  first = 0,
  118.         last = numElems - 1,
  119.         middle;
  120.  
  121.     while (!found && first <= last)
  122.     {
  123.         middle = (first + last) / 2;
  124.         if (array[middle] == value)
  125.             found = true;
  126.         else if (array[middle] > value)
  127.             last = middle - 1;
  128.         else
  129.             first = middle + 1;
  130.     }
  131.     return found;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement