Basically my assignment is create the best Boggle board. Basically so far I've successfully created a randomly generated board, read in the dictionary, allocated dynamic memory for the dictionary, counted the frequency of each letter (for example 'a' has x amount of characters in the dictionary), and found the percentage of the frequency for each letter. Now I have to implement that frequency percentage into my boggle board... and that is where I am totally stuck. Can someone please tell me how to go about it.
Thank you!
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <ctime>
#include <cassert>
#include <cctype>
using namespace std;
int FrequencyCount[26]; // Counts frequency of characters
float FrequencyPercentage[26];
const int Size = 17; // 17 characters for the line + 1 for the '\0'
const int MaxNumberOfWords = 263533; // maximum number of words to be stored
char** theWords; // Global dictionary
void allocateArray( char ** & dictionary)
{
// Allocate space for large array of C-style strings
dictionary = new char*[MaxNumberOfWords];
// For each array entry, allocate space for the string to be stored there
for (int i=0; i < MaxNumberOfWords; i++) {
dictionary[i] = new char[ Size];
// just to be safe, initialize C-string to all null characters
for (int j=0; j < Size; j++) {
dictionary[i][j] = NULL;
}//end for (int j=0...)
}//end for (int i...)
for (int j = 0; j < 26; j++){
FrequencyCount[j] = 0;
FrequencyPercentage[j] = 0;
}
}//end allocateArray()
// Deallocate memory for the dictionary words array. I suppose
// the '&' to make matrix a reference parameter is not strictly
// necessary, since the memory is freed up in either case.
void deallocateArray( char ** & dictionary)
{
// Deallocate dynamically allocated space for the array
for (int i=0; i < MaxNumberOfWords; i++){
delete [] dictionary[ i];
}
delete [] dictionary; // delete the array at the outermost level
}
/*Reads in dictionary, lowercases all the letters, does character frequency count. Also
calls a function to allocate dynamic memory*/
void readInDictionary(){
ifstream inStream; // declare an input stream for my use
int wordRow = 0; // Row for the current word
allocateArray(theWords);
inStream.open( "dictionary.txt");
assert(! inStream.fail() ); // make sure file open was OK
// Keep repeating while input from the file yields a word
while (wordRow < MaxNumberOfWords && inStream >> theWords[wordRow])
if (strlen(theWords[wordRow]) >= 3)
if (strlen(theWords[wordRow]) <= 16){
for (int i = 0; i < strlen(theWords[wordRow]); i++){
theWords[wordRow][i] = tolower(theWords[wordRow][i]);
FrequencyCount[theWords[wordRow][i] - 97]++;
}
theWords[wordRow] = (theWords[wordRow]);
wordRow++;
}
// Go through the array of words, displaying each one in turn.
// Note that we only display the words read, not the empty array
// rows.
}
int main(){
char boggleBoard[6][6], randomLetter;
int tableColumn, tableRow;
float totalAmountOfCharacters = 0;
srand(time(NULL)); //seeds random number
/*adding asterisks manually around the boggle board in order to search for
words around the edge faster*/
boggleBoard[0][0] = '*';
boggleBoard[0][1] = '*';
boggleBoard[0][2] = '*';
boggleBoard[0][3] = '*';
boggleBoard[0][4] = '*';
boggleBoard[0][5] = '*';
boggleBoard[1][0] = '*';
boggleBoard[2][0] = '*';
boggleBoard[3][0] = '*';
boggleBoard[4][0] = '*';
boggleBoard[5][0] = '*';
boggleBoard[5][1] = '*';
boggleBoard[5][2] = '*';
boggleBoard[5][3] = '*';
boggleBoard[5][4] = '*';
boggleBoard[5][5] = '*';
boggleBoard[0][5] = '*';
boggleBoard[1][5] = '*';
boggleBoard[2][5] = '*';
boggleBoard[3][5] = '*';
boggleBoard[4][5] = '*';
boggleBoard[5][5] = '*';
/*Generates table and prints out random letters*/
for(tableRow = 1; tableRow < 5; tableRow++) //fills array with char values
for(tableColumn = 1; tableColumn < 5; tableColumn++){
randomLetter = rand() % 26 + 65;
boggleBoard[tableRow][tableColumn] = randomLetter;
}
for(tableRow = 1; tableRow < 5; tableRow++){ //prints out array as a table
cout << "\n";
for(tableColumn = 1; tableColumn < 5; tableColumn++)
cout << boggleBoard[tableRow][tableColumn] << " ";
}
cout << "\n\n";
readInDictionary();
for(int j = 0; j < 26; j++) // adds up all of the characters in the dictionary
totalAmountOfCharacters += FrequencyCount[j];
for(int j = 0; j< 26; j++) // gives percentages of character vs overall characters
FrequencyPercentage[j] = FrequencyCount[j]/totalAmountOfCharacters;
return 0;
}