Image

Imagepixelpoet wrote in Imagecpp


I did as8, it took me forever, care to give me comments about my program?


/*
Write a program that deals with arrays. You should have an array that can hold
20 words (strings). Your program should have the following capabilities:

* Fill the array from a user specified file. Anything currently in the
array is to be discarded. Be sure the array does not get overfilled.
For example,if the file had 38 words in it, your array should just
hold the first 20, and if the file had only 7 words in it, then the
array would hold all 7 and the rest of the array would hold junk.

* Search for a user specified word in the array. Return the position of
the first location this word is in, if it exists and return a -1 if no
such value is in the array.

* Delete a user specified word, if one exists. Delete the first instance
of that word if it appears more than once in the array. Note: the array
should always be filled without holes, using positions 0 through n-1,
where n is the number of words in the array.

* Print all the words that begin with a user specified character.
* Find the length of the longest word.
* Swap the words at two user specified locations.
* Print the words in the array nicely lined up on the screen.
* Print the words in the array nicely lined up to a user specified file.


* EXTRA CREDIT: Specify if the array is full of unique words, or if there
are duplicate words in the array.
* EXTRA CREDIT: Sort the words in the array (alphabetically).
*/
//add the < and > to the libraries cus on html it doesn't show it
#include iostream
#include fstream
#include string

const int SIZE = 20;

using namespace std;

int fillArray (string[]); //fill array from infile
int search (string word, string[], int); //search for word
void deleteWord (string word, string[], int); //deletes word
void printWord (string[], int); //print words with given letter
int wordLength (string[]); //length of word
void swap (int, int, string[], int); //swap word
void printScreen (const string[], int); //prints to screen
void printFile (const string[], int); //prints file
void unique (string[], int); //checks for duplicate words
void sortWord (string[], int); //sorts words

void main()
{
string array[SIZE]; //array of strings to hold input
string word; //word to delete
int num1, num2, array_size = 0, menu; //nums for position, size of array and menu

cout << "This program (array) can hold 20 words (strings), " //title, purpose
<< "by: Carina \n";

do{
cout << endl; //prompts menu for user
cout << "1) Fill the array from a user specified file: \n"
<< "2) Search for a user specified word in the array: \n"
<< "3) Delete a user specified word, if one exists: \n"
<< "4) Print all the words that begin with a specified character: \n"
<< "5) Find the length of the longest word: \n"
<< "6) Swap the words at two user specified locations: \n"
<< "7) Print array to screen: \n"
<< "8) Print array to user-specified file: \n"
<< "9) Sort the words in the array (alphabetically): \n"
<< "10) Specify if the array is full of unique words: \n"
<< "11) Exit Program:\n ";

cout << "\nEnter choice (1-11): "; //enter choice of menu
cin >> menu;
while(cin.fail() || menu < 1 || menu > 11) //error check
{
cin.clear();
cin.ignore(80, '\n');
cout << "Error! Enter correct choice: ";
cin >> menu;
}

switch (menu) //menu options
{
case 1:

//calls function to fill array
array_size = fillArray(array);
break;

case 2:

//prompts to search for a word
cout <<"\nSearch for what word?: ";
cin >>word;

//Tells user what position the word is in
cout << "\nThe word is at the " <
[Error: Irreparable invalid markup ('<search(word,>') in entry. Owner must fix manually. Raw contents below.]

<font size =1>
I did <a href="http://www.cs.ucr.edu/~klick/as8.htm">as8</a>, it took me forever, care to give me comments about my program?
<lj-cut text="Here's my code for as8, *enjoy*">
<font size =1>
/*
Write a program that deals with arrays. You should have an array that can hold
20 words (strings). Your program should have the following capabilities:

* Fill the array from a user specified file. Anything currently in the
array is to be discarded. Be sure the array does not get overfilled.
For example,if the file had 38 words in it, your array should just
hold the first 20, and if the file had only 7 words in it, then the
array would hold all 7 and the rest of the array would hold junk.

* Search for a user specified word in the array. Return the position of
the first location this word is in, if it exists and return a -1 if no
such value is in the array.

* Delete a user specified word, if one exists. Delete the first instance
of that word if it appears more than once in the array. Note: the array
should always be filled without holes, using positions 0 through n-1,
where n is the number of words in the array.

* Print all the words that begin with a user specified character.
* Find the length of the longest word.
* Swap the words at two user specified locations.
* Print the words in the array nicely lined up on the screen.
* Print the words in the array nicely lined up to a user specified file.


* EXTRA CREDIT: Specify if the array is full of unique words, or if there
are duplicate words in the array.
* EXTRA CREDIT: Sort the words in the array (alphabetically).
*/
//add the < and > to the libraries cus on html it doesn't show it
#include iostream
#include fstream
#include string

const int SIZE = 20;

using namespace std;

int fillArray (string[]); //fill array from infile
int search (string word, string[], int); //search for word
void deleteWord (string word, string[], int); //deletes word
void printWord (string[], int); //print words with given letter
int wordLength (string[]); //length of word
void swap (int, int, string[], int); //swap word
void printScreen (const string[], int); //prints to screen
void printFile (const string[], int); //prints file
void unique (string[], int); //checks for duplicate words
void sortWord (string[], int); //sorts words

void main()
{
string array[SIZE]; //array of strings to hold input
string word; //word to delete
int num1, num2, array_size = 0, menu; //nums for position, size of array and menu

cout << "This program (array) can hold 20 words (strings), " //title, purpose
<< "by: Carina \n";

do{
cout << endl; //prompts menu for user
cout << "1) Fill the array from a user specified file: \n"
<< "2) Search for a user specified word in the array: \n"
<< "3) Delete a user specified word, if one exists: \n"
<< "4) Print all the words that begin with a specified character: \n"
<< "5) Find the length of the longest word: \n"
<< "6) Swap the words at two user specified locations: \n"
<< "7) Print array to screen: \n"
<< "8) Print array to user-specified file: \n"
<< "9) Sort the words in the array (alphabetically): \n"
<< "10) Specify if the array is full of unique words: \n"
<< "11) Exit Program:\n ";

cout << "\nEnter choice (1-11): "; //enter choice of menu
cin >> menu;
while(cin.fail() || menu < 1 || menu > 11) //error check
{
cin.clear();
cin.ignore(80, '\n');
cout << "Error! Enter correct choice: ";
cin >> menu;
}

switch (menu) //menu options
{
case 1:

//calls function to fill array
array_size = fillArray(array);
break;

case 2:

//prompts to search for a word
cout <<"\nSearch for what word?: ";
cin >>word;

//Tells user what position the word is in
cout << "\nThe word is at the " <<search(word, array, array_size) <<" position";
break;

case 3:

//call funtion to delete word
deleteWord(word, array, array_size);
break;

case 4:

//calls function to prints all words that start with letter
printWord(array, array_size);
break;

case 5:

//tells user what the longest word is
cout <<"\nThe longest word has " <<wordLength(array) <<" letters.";
break;

case 6:

//prompts users to swap positions
cout <<"\nSwap which positions?: ";
cin >> num1;

//error check
while(cin.fail() || num1 >= SIZE || num1 < 0)
{
cin.clear();
cin.ignore(80, '\n');
cout << "Error! Enter correct value: ";
cin >> num1;
}

cin >>num2;

//error check
while(cin.fail() || num2 >= SIZE || num2 < 0)
{
cin.clear();
cin.ignore(80, '\n');
cout << "Error! Enter correct value: ";
cin >> num2;
}

//calls function to swap
swap(num1, num2, array, array_size);
break;

case 7:

//prints array to screen
cout <<"\nPrinting to screen: \n";
printScreen(array, array_size);
break;

case 8:

//calls funtion to print a file
printFile(array, array_size);
break;

case 9:
//sort array (alphabetically)
cout <<"\nSorting array\n";

sortWord(array, array_size);
printScreen(array, array_size);
break;

case 10:
//find words which have appeared more than once
unique(array, array_size);
break;

default:
break;
}

}while (menu != 11); //end main
}

//Fill the array from a user specified file
int fillArray (string array[])
{
char * filename = new char [40];

//prompts user for filename
cout << "What is the input file: ";
cin >> filename;

ifstream infile; //declare infile
infile.open(filename); //opens userfile

//if file fails to open, exits immediately
if (infile.fail())
{
cout << "Error Opening File!\n";
exit (-1);
}
int array_size = 0;

for(int i = 0; infile >> array[i]; i++) //fills array one string at a time
{
++array_size;
}

infile.close();
return array_size;

}

//Search for a user specified word in the array
int search (string word, string array[], int array_size)
{
if (array_size == 0)
{
cout << "Array is Empty ";
}
else
{
for (int i=0; i < SIZE; i++) //checks each array for userword
{
if (array[i] == word)
{
return i; //returns position
}
}
}
return -1; //return fail
}

//Delete a user specified word, if one exists
void deleteWord (string word, string array[], int array_size)
{
if (array_size==0)
{
cout << "Empty Array";
}

else
{
//promts user to delete word
cout << "\nDelete what word?: ";
cin >> word;
for (int i=0; i < SIZE; i++) //checks each array for userword
{
if (array[i] == word) //checks for userword
{
for (int j=i; j < 19; j++)
{
array[j] = array[j + 1]; //delete word and fills hole
}
}
}
}
}

//Print all the words that begin with a user specified character
void printWord (string array[], int array_size)
{
if (array_size == 0)
{
cout << "Array is Empty";
}
else
{
char letter;
//prompts for start of letter
cout <<"\nPrint words that start with what letter?: ";
cin >> letter;

for (int i = 0; i < SIZE; i++) //checks each array for userword
{
if(array[i][0] == letter) //checks first letter of string
{
cout << array[i] <<" "; //displays word
}

}
}
}


//Find the length of the longest word
int wordLength (string array[])
{
int length = 0;

for (int i = 0; i < SIZE; i++)
{

if(array[i].length() > length) //checks to see which word is bigger
{
length = array[i].length(); //assigns value
}
}
return length;
}

//Swap the words at two user specified locations
void swap (int num0, int num1, string array[], int array_size)
{
string temp; //temp string

if(num0 < array_size && num1 < array_size)
{
temp = array[num1]; //assign string num1 to temp
array[num1] = array[num0]; //assign string num0 to num1
array[num0] = temp; //assign temp to num0, then temp is destroyed
}
else{
cout <<"Error, can't swap with empty position.\n";
}
}

//Print the words in the array nicely lined up on the screen.
void printScreen (const string array[], int arraysize)
{
for(int i = 0; i < arraysize; i++) //goes through each string word
{
cout << array[i] << endl; //prints array
}
if(arraysize == 0)
{
cout <<"Array is empty!\n";
}
}

//Print the words in the array nicely lined up to a user specified file
void printFile ( const string array[], int arraysize)
{
char * filename = new char [40];

//prompts user for filename
cout << "What is the output file: ";
cin >> filename;

ofstream outfile; //declare outfile

outfile.open(filename); //opens userfile

for (int i=0; i < arraysize; i++) //fills array one string at a time
{
outfile << array[i] <<endl;
}
outfile.close();
if(arraysize == 0)
{
cout <<"Array is empty!\n";
}
}

//Specify if the array is full of unique words,
//or if there are duplicate words in the array

void unique (string array[], int array_size)
{
int done = 1;
for(int i = 0; i < array_size; i++)
{
for(int j = 0; j < array_size; j++)
{
//checks to see if there are duplicate words
if ((array[i].c_str() == array[j].c_str()) && i != j && array[i].size()>0)
{
done = 0;
}

if (array[i] == array[j] && i != j)
{
done = 0;
}
}
}
if(done == 1)
{
cout << "There are no duplicate words\n";
}
else if(done == 0)
{
cout << "There are duplicate words\n";
}
}

//sorts words by length (alphabetically)
void sortWord (string array[], int array_size)
{
int temp = 0, next = 0;
string tempo;
if (array_size == 0)
{
}
else
{
for(int i = 0; i < SIZE; i++) //goes through each word of array
{
//seaches size of word
for(int j = 0; j < SIZE; j++)
{
if (j == SIZE - 1)
{
break;
}
else if ((array[j].size()>0) && (array[j+1].size()>0))
{
if ( strcmp(array[j].c_str(), array[j+1].c_str())>0 )
{
tempo = array[j];
array[j] = array[j+1];
array[j+1] = tempo;
}
}
}
}
}
}