New to programming, I have tried just about everything to get my program to run, but cannot figure it out. This is for my Into to programming course, so I'll be posting the instructions for it as well for context purposes.
I know for a fact that my program is running properly up until the closing bracket in line 62. The primary issue that im having is right after that, where I call the function sharedLetters. For context, I have attempted multiple ways of trying to fix the function definition, so at this point its a little jumbled, but it was wrong to begin with, so there's that. As for the remainder of the code, im not entirely sure if its written correct or not, as I cant get beyond the called function.
Assignment Instructions
For this assignment, you will write a program that forms the basis of a crossword puzzle generator. In order to create a crossword puzzle you need to identify letters that are common to one or more words. This program helps you generate crossword puzzles by identifying letters that are common to several words and storing them in an intermediate data structure.
Using intermediate data structures is one way to simplify your coding. You create a data structure that organizes your input so the final output is easier to create. Your CrosswordGenerator program will open a file containing a list of words. To prepare for crossword puzzle generation your code should implement the following pseudo code:
- Open a file named wordlist.txt
- Determine the number of words (N) in the file.
- Create an array of that size called wordArray[];
- Read all the words from the file into an array called wordArray[N].
- Sort the array of words by length in descending order
- Start at the beginning of the array (the longest word) and examine the word
- Find all other words in the array that can intersect the current word Two words can intersect if they share a letter.
Write a function called
String sharedLetters(String S1, String S2)
.Implement
sharedLetters(S1,S2)
that will return a string containing letters shared by the two parameter strings S1 and S2. If no letters are shared between S1 and S2sharedLetters()
should return the empty string""
.sharedLetters(S1,S2)
should ignore case; capital and lower case letters are considered equal.Create a two dimensional array of Strings
wordsIntersect[N][N]
where each dimension N is the size ofwordArray
.Iterate over
wordArray
comparing distinct words. Do not compare a word to itself. IfwordArray[i]
shares any letters withwordArray[j]
place the result ofsharedLetters(wordArray[i],wordArray[j])
intowordsIntersect[i][j]
. Non-alphabetic characters should be ignored.Upper and lower case letters should be equivalent. Your program should not treat them as distinct tokens.
The order of shared letters is not a consideration for this program.
Write the contents of the
wordsIntersect
array to a Comma Separated Values (CSV) text file namedwordsIntersect.csv
in row-major form: row,col,value. In the example below, there is an entry inwordsIntersect[12][33]
which contains the string "tci". There would also be a line inwordsIntersect.csv
that contains12,33,tci
-- there are no quotes around the letters and the line ends after the letter 'i'. Given thatwordsIntersect[12][78]
will contains""
there would be a line in the file containing:12,78,
where there is an end of line after the last comma.N.B.
Example of what sharedLetters() output.
SharedLetters(transaction,dictum)
returns"tci"
SharedLetters(Transaction,kudzu)
returns""
If
"transaction"
is stored inwordArray[12]
and"dictum"
is stored inwordArray[33]
thenwordsIntersect[12][33]
will contain"tci"
.If
"kudzu"
is stored inwordArray[78]
thenwordsIntersect[12][78]
will contain""
.
My actual code
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using std::string;
//calling function that will record shared letters between words, to be called later
string sharedLetters(string S1[], string S2[]);
int main ()
{
//variables for indexes used in arrays
int i, compareIndex, count = 0;
//declaring array and its total size based on total words in file
const int N = 644;
string wordArray[N];
//declaring a 2-D array wordsIntersect, setting size of both dimensions to 'N'
string wordsIntersect[644][644];
//declaring file
ifstream file;
//opening file
file.open("wordlist.txt");
cout << "Reading data from the file." << endl;
while (count < N)
{
file >> wordArray[count];
count++;
}
//closing the file
cout << "Closing the wordlist.txt file now." << endl;
file.close();
//sort the array of strings in descending order using SelectionSort
//going through array of strings, wordArray[]
for (int i = 0; i < N; i++)
{
//creating a nested for loop to compare indexes
for (int compareIndex = i + 1; compareIndex < N; compareIndex++)
{
//compare the words at both indexes to determine which one is longer
if (wordArray[i].length() < wordArray[compareIndex].length())
{
//if true, swap the elements using selectionSort
string longerWord = wordArray[compareIndex];
//swap indexes i and compareIndex
wordArray[compareIndex] = wordArray[i];
wordArray[i] = longerWord;
}
}
}
//calling sharedLetters function
for(i = 0; i < N; i++)
{
for(compareIndex = 0; compareIndex < N; compareIndex++)
{
string sharedLetters(wordArray[i], wordArray[compareIndex]);
}
}
string word = string sharedLetters(wordArray[i], wordArray[compareIndex]);
cout << word << endl;
//compare every word in wordArray to every other word within the array, using the sharedLetters funtion
for (int i = 0; i < N; i++)
{
for (int compareIndex = i + 1; compareIndex < N; compareIndex++)
{
//saving a string result within the main function that will equal
//the return value of the called function in the previous step
wordsIntersect[i][compareIndex] = sharedLetters(wordArray[i], wordArray[compareIndex]);
}
}
for (int i = 0; i < N; i++)
{
for (int compareIndex = i + 1; compareIndex < N; compareIndex++)
{
cout << wordArray[i] << "," << wordArray[compareIndex] << " " << wordsIntersect[i][compareIndex] << endl;
}
}
return 0;
}
//setting up 2D array wordsIntersect
for (int i = 0; i < N; i++)
{
for (int compareIndex = 0; compareIndex < N; compareIndex++)
{
wordsIntersect[i][compareIndex];
}
}
//writing the data in 2-D array into file wordsIntersect.csv
cout << "Now opening the file wordsIntersect.csv." << endl;
ofstream outputFile;
outputFile.open("wordsIntersect.csv");
cout << "Inputting sorted array wordsIntersect within file, now." << endl;
outputFile << wordsIntersect[i][compareIndex];
//closing file
outputFile.close();
cout << "Data has successfully been entered into the file. The file is now closed." << endl;
return 0;
}
//function that will identify common letters between each word
string sharedLetters(string S1[], string S2[])
{
//declaring string for sharedLetters
string sharedletters = "";
//declaring a string for words with common letters
string commonLetter = "";
//nested for loop to compare letters in S1 and S2
for (int i = 0; i < S1.length(); i++)
{
for (int compareIndex = 0; compareIndex < S2.length(); compareIndex++)
{
//comparing letters in strings
if(S1[i] == S2[compareIndex])
{
//assigning the letter at index i whenever common is found
commonLetter = S1[i];
//adding the value of commonLetter to sharedletters
sharedletters = commonLetter;
break;
}
}
}
//return string of sharedletters
return sharedletters;
}