int main()
{
// declare a constant for the array size, say 50
// and declare two character arrays of that size
// (one for the original text and one for the sorted copy)
// prompt the user to enter a line of text and read it into the array
// (see the cin.getline example in the syntax examples)
// copy the content into the second array
// (see the strncpy example in the syntax examples)
// then pass the second array to the sorting function to be sorted
// declare a character variable to store the user's chosen search character,
// ask them what character they wish to search for, and store it in the variable
// (a simple cin >> works here)
// declare two int variables to hold the location where the target character is found,
// then call linearSearch on the original array and store the returned value in one of
// the int variables and call binary search on the sorted array (and store the
// returned value in your other int variable)
// call the report function twice:
// once passing the original array, the user char, the found position from linear search,
// and true (indicating this is the original),
// once passing the sorted array, user char, found position from binary search, false
}
void sortLetters(char text[])
{
// bubblesort will need to know how many characters we have stored,
// we can get this using strlen(text)
// I recommend using two for loops for the sorting, one nested inside the other
// our outer loop needs to make N-1 passes for an array holding N chars
// our inner loop can go from positions 1 to N-1
// at each position it compares the character in the current position
// to the character before it, swapping them if they are out of order
// (not doing anything if the order is ok)
}
int linearSearch(char text[], char target)
{
// search each position from 0 to the end,
// we can stop either at strlen(text) or when we reach a position in text
// that contains the null terminator, '\0'
// at each position check if the character there matches the target,
// if so then we found it, return the current position
// if we get out of the loop it means we never found the target value
// so return -1
}
int binarySearch(char text[], char target)
{
// set up the bounds for the array section we're searching (initially the whole thing)
// note that strlen(text) will tell you the position of the last relevant character
// repeat as long as lower is still <= upper:
// compute the midpoint between lower and upper
// if the character at the midpoint is less than the target
// then set lower to midpoint+1
// otherwise if the character at the midpoint is greater than the target
// then change upper to midpoint-1
// otherwise we found it, return midpoint
// after the loop return false (since getting there means we didn't find it)
}
void report(char text[], char target, int pos, bool wasItOrig)
{
// display the text, telling the user which it was (original or sorted)
// cout << text works fine for displaying null-terminated char array content
// tell them if we found the target or not,
// and what position we found it in (if it was found)
}
void swap(char &c1, char &c2)
{
// our usual swap, just with chars this time instead of ints or floats
}
|