1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
//This program will allow the user to enter a list of words into an array, however, the list cannot exceed 25 words.
//The program will then output the list in the original order that it was entered,
//as well as output it alphabetized by using a bubble sort function.
//Josef Jaworski
//Date: December 8, 2013
const int MAX_WORDS = 25; //the maximum amount of words that the user inputs
const int MAX_CHARS = 30; //the maximum amount of characters that the words can be.
//prototypes for the different functions
void getwords (char [MAX_WORDS][MAX_CHARS]); //this function will allow the user to enter the words.
void outputwords (char [MAX_WORDS][MAX_CHARS]); //this function will output the words in the order they were put in.
void sortArray (char [MAX_WORDS][MAX_CHARS],int size); //this function will use a bubble sort to alphabetize the words.
void showArray (char [MAX_WORDS][MAX_CHARS],int size); //this function will output the alphabetized words.
int main ()
{
char strings[MAX_WORDS][MAX_CHARS] = {0};
getwords(strings);
cout<<"The list of animals as it was input: "<<endl;
outputwords(strings);
cout<<"\n\n";
sortArray (strings, MAX_WORDS);
cout<<"The list of animals alphabetized: "<<endl;
showArray (strings, MAX_WORDS);
cout<<endl;
system ("pause");
return 0;
}
//this function will allow the user to enter the words.
void getwords (char strings[][MAX_CHARS])
{
cout<<"Please enter a list of animal names, ending the list with a period."<<endl;
for (int i = 0;i < MAX_WORDS; i++) //This will allow the user to enter as many words as they want, up to 25.
{
cin >> strings[i];
if (strcmp(strings[i],".")==0) //this will stop the function if the user enters a "." before entering 25 words.
{
break;
}
}
cout<<endl;
}
//this function will output the words in the order they were put in.
void outputwords (char strings [MAX_WORDS][MAX_CHARS])
{
for (int i = 0; i < MAX_WORDS;i++) //This allows the user to put in the words up to 25 of them.
{
cout<< strings[i]<<"\t"; //This outputs the words in the order they were input and gives them some spacing.
if (strcmp(strings[i],".")==0)
{
break;
}
}
}
//this function will use a bubble sort to alphabetize the words.
void sortArray (char strings [MAX_WORDS][MAX_CHARS], int size)
{
bool swap; //This be used to create a true/false statement of sorts.
char temp[MAX_CHARS]; //This is a variable used to temporarily hold a word.
do //This do while loop will perform the necessary steps to alphabetize the words.
{
swap = false;
for (int count = 0; count < (size-1); count++)
{
if (strncmp(strings[count],strings[count+1],MAX_CHARS) > 0) //This will decide whether the first variable is greater than the second or not.
{
for(int i=0;i<MAX_CHARS;i++)
{
temp[i] = strings[count][i];
strings[count][i] = strings[count+1][i];
strings[count+1][i] = temp[i];
}
swap = true;
}
}
} while (swap);
}
//this function will output the alphabetized words.
void showArray (char strings[MAX_WORDS][MAX_CHARS], int size)
{
for (int count = 0; count < size; count++)
cout << strings[count] << "\t"; //outputs the alphabetized list and gives them a better spacing.
cout<<endl;
}
|