Question

Assignment 1 In this assignment you will be writing a tool to help you play the...

Assignment 1

In this assignment you will be writing a tool to help you play the word puzzle game AlphaBear. In the game, certain letters must be used at each round or else they will turn into rocks! Therefore, we want to create a tool that you can provide with a list of letters you MUST use and a list of available letters and the program returns a list of all the words that contain required letters and only contain available letters.

Starter Code:

  • main.cpp - main function to run your program.
  • BearHelper.h - Header file with function prototypes.
  • BearHelper.cpp - Your code goes in this file. must write the code as instructed to practice disk access and properly allocating C strings. The next assignment will look at ways we can improve the efficiency,

The Program:

The program (main) reads in a dictionary file, uses a list of required letters and a list of available letters. Then it goes through the dictionary and removes all words that do not contain required letters and all words that contain unavailable letters. Finally, the program prints the words that were not deleted

Structs and Functions to Implement:

The following functions should be implemented by the student.

  • int GetNumEntriesInFile(const char* f);
  • char** ReadWords(const char* f, int count);
  • bool AllLettersInSet(const char* letters, const char* group);

Header File

#ifndef __FunctionTemplates__bearHelper__

#define __FunctionTemplates__bearHelper__

#include <stdio.h>

int GetNumEntriesInFile(const char *f);

char **ReadWords(const char *f, int count);

bool AllLettersInSet(const char *letters, const char *set);

#endif /* defined(__FunctionTemplates__bearHelper__) */

#include <string>

#include <iostream>

#include <stdio.h>

#include <vector>

#include <stdlib.h>

#include <unordered_map>

#include <fstream>

#include "bearHelper.h"

/*

* Counts the number of words in a file named f

* parameter: file name

* return: number of words in file

*/

int GetNumEntriesInFile(const char *f)

{

//1. Open the file for reading

//2. Count the number of words in file. Hint: You can count the number of '\n' characters in file. Use fgetc to read one character at a time.

  

//3. close file

  

//4.return count

  

}

/*

* Creates an array of strings of size count and populates it with words in file named f

* parameter: f - file name, cout - number of words in file

* return: An array of c-strings

*/

char **ReadWords(const char *f, int count)

{

  

//1. Open file for reading

//2. Allocate the array of c-strings

//3. For each word in the file

//3.a Determine the length of word

// (hint: use ftell to determine the location of the pointer in file before you start counting the letters, use fgetc to read a single character, then fseek to the location of the start of the word and read it).

//3.b Allocate a string long enough to store word

//3.c store word in array of words

//4. close file

//5. return array of words

  

}

/*

* Returns true if all letters in "letters" are in group of letters "group" (duplicates should be accounted for)

* therefore the letters: "ee" are in group "tree" but not in group "tea"

*

* parameter: letters - list of letters, group - a collection of letter

* return: true of all letters are in set

*/

bool AllLettersInSet(const char *letters, const char *group)

{

  

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Below are the required code segments which have been requested.

you may add the following functions to your required files. You may test the functions directly by running this program's main

remove the cout from ReadWords, if it is not required

The explanations are in the comments.

//------------------------------------------------------------------------------------------------------------------

#include<iostream>
//defines file pointer
#include <stdlib.h>
#include<stdio.h>

using namespace std;
int GetNumEntriesInFile(const char *f)
{
int word_counter=0;
FILE *fp=fopen(f,"r");
if (fp == NULL)
{
cout<<"Cannot open file ";
exit(0);
}
char ch = fgetc(fp);
while (ch != EOF)
{
if(ch==' ')
word_counter++;
ch = fgetc(fp);
}
fclose(fp);
return word_counter;
}

/*
* Creates an array of strings of size count and populates it with words in file named f
* parameter: f - file name, cout - number of words in file
* return: An array of c-strings
*/
char **ReadWords(const char *f, int cnt)
{
//1. Open file for reading
FILE *fp=fopen(f,"r");
if (fp == NULL)
{
cout<<"Cannot open file ";
exit(0);
}
//2. Allocate the array of c-strings
int word_length=1;
char **array_of_words=(char **)malloc(cnt*sizeof(char*));
//for(int m=0;m<cnt;m++)
// array_of_words[m]=(char*)malloc(max_word_length*sizeof(char));
int i=0,j=0;
char ch;
long start=ftell(fp),ending=0;
while((ch=fgetc(fp))!=EOF)
   {
   //3. For each word in the file
   //NOTE: It is assumed that every word has ended with a newline character in the
   //dictionary file
       if(ch==' ')//ending of current string
       {
//calculating current position of file ptr
ending=ftell(fp);
//3.a Determine the length of word
word_length=ending-start;
//we deduct 1 from the diffenece since we dont want to include newline character
//3.b Allocate a string long enough to store word
           array_of_words[i]=(char*)malloc((word_length-1)*sizeof(char));
           //move the fp to starting of string
           fseek(fp,-word_length,SEEK_CUR);
           j=0;
           while(ftell(fp)<ending-1){
char v=fgetc(fp);
if(v!=' '){//skipping newline characters
//3.c store word in array of words
array_of_words[i][j]=v;
j++;
}
           }
           array_of_words[i][j]='';
           //remove the cout, if you dont want to print the stored words
           cout<<array_of_words[i]<<endl;
           //sets flag for next string
           start=ftell(fp);
           i++;
       }
   }
   //4. close file
   fclose(fp);
//5. return array of words
return array_of_words;
}
/*
* Returns true if all letters in "letters" are in group of letters "group" (duplicates should be accounted for)
* therefore the letters: "ee" are in group "tree" but not in group "tea"
* parameter: letters - list of letters, group - a collection of letter
* return: true of all letters are in set
*/
bool AllLettersInSet(const char *letters, const char *group)
{
int freq[26];
int freq_group[26];
int i=0,j=0;
//initializes the fequency array to 0
for(i=0;i<26;i++){
freq[i]=0;
freq_group[i]=0;
}
//traversing array of letters and storing frequencies of the crrent characters
while(letters[j]!='')
{
int ascii_converted_index=(int)(letters[j]-'a');
freq[ascii_converted_index]++;
j++;
}
j=0;
//frequencies of characters in groups of characters
while(group[j]!='')
{
int ascii_converted_index=(int)(group[j]-'a');
freq_group[ascii_converted_index]++;
j++;
}
for(i=0;i<26;i++){
//checks if frequency of reqd characters are at least upto the requirements
if(freq_group[i]<freq[i])
return false;
}
return true;
}

//NOTE: THIS MAIN IS JUST TO TEST THE FUNCTIONS AND NOT A PART OF YOUR REQUIRED CODE
int main()
{
char *filename="dictionary.txt";
int n=GetNumEntriesInFile(filename);
ReadWords(filename,n);
cout<<n<<" words in file"<<endl;
if(AllLettersInSet("ee", "tea"))
cout<<"yes";
else
cout<<"no";
return 0;
}

//---------------------------TESTING-------------------------------------------------------------------

dictionary.txt

//------------------------------------------------------------------------------------------------------------

privacy
tea
items
company
HomeworkLib
read
group
elephant
need
many

//------------------------------------------------------------------------------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
Assignment 1 In this assignment you will be writing a tool to help you play the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • My question is listed the below please any help this assignment ; There is a skeleton...

    My question is listed the below please any help this assignment ; There is a skeleton code:  copy_file_01.c #include <stdio.h> #include <stdlib.h> int main(int argc, char* argv[]) { char ch ; FILE *source , *target;    if(argc != 3){ printf ("Usage: copy file1 file2"); exit(EXIT_FAILURE); } source = fopen(argv[1], "r"); if (source == NULL) { printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } target = fopen(argv[2], "w"); if (target == NULL) { fclose(source); printf("Press any key to exit...\n"); exit(EXIT_FAILURE); } while ((ch...

  • C++ assignment help! The instructions are below, i included the main driver, i just need help...

    C++ assignment help! The instructions are below, i included the main driver, i just need help with calling the functions in the main function This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a...

  • Assignment 4 Real Deal: Crier On Us Some word games, like Scrabble, require rearranging a combination of letters to make...

    Assignment 4 Real Deal: Crier On Us Some word games, like Scrabble, require rearranging a combination of letters to make a word. This type of arrangement is generally referred to as an anagram, it's known as a permutation in mathematics. This assignment will give you some experience thinking about and writing recursive functions. Write a C++ program that searches for ``anagrams'' in a dictionary. An anagram is a word obtained by scrambling the letters of some string. For example, the...

  • Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will...

    Data Structures and Algorithm Analysis – Cop 3530 Module 3 – Programming Assignment This assignment will access your skills using C++ strings and dynamic arrays. After completing this assignment you will be able to do the following: (1) allocate memory dynamically, (2) implement a default constructor, (3) insert and remove an item from an unsorted dynamic array of strings, (4) use the string class member functions, (5) implement a copy constructor, (6) overload the assignment operator, (7) overload the insertion...

  • You will be reading in 3 files in the program. One will contain a list of...

    You will be reading in 3 files in the program. One will contain a list of 1000 words in unsorted order. The second file will contain 1000 words in sorted order. The final file will contain 20 words to be searched for. The main program has been written for you. You will be implementing three functions: bool readWords(string array[], int size, string fileName); int linearSearch(string wordToFind, const string words[], int size); int binarySearch(string wordToFind, const string words[], int size); The...

  • Attached to this assignment as a separate document is the C++ code for a program that...

    Attached to this assignment as a separate document is the C++ code for a program that determines if a given string is a palindrome or not. A palindrome is a word that is spelled the same backward or forward. "bob" is an example of a palindrome. The program is sometimes a talking point during lecture, and may not fully adhere to the many bobisms I've been telling you about. In fact, it may not entirely work but that wasn't its...

  • Dictionary.java DictionaryInterface.java Spell.java SpellCheck.java In this lab you will write a spell check program. The program...

    Dictionary.java DictionaryInterface.java Spell.java SpellCheck.java In this lab you will write a spell check program. The program has two input files: one is the dictionary (a list of valid words) and the other is the input file to be spell checked. The program will read in the words for the dictionary, then will read the input file and check whether each word is found in the dictionary. If not, the user will be prompted to leave the word as is, add...

  • Writing a program in C please help!! My file worked fine before but when I put...

    Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...

  • Vliestion (1) while make testman I will generate an executable called Testman IX to test your...

    Vliestion (1) while make testman I will generate an executable called Testman IX to test your program for Question (2). If you just issue make, then both will be generated. (1) In this question, you will re-implement the question in Assignment 1 by using vectors instead dynamic arrays. All the functionalities are the same. Of course, you need to make appropriate changes to the function declarations by using vectors instead of pointers to strings or string arrays). See below. Also...

  • I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt...

    I need help writing this code for java class. Starter file: Project3.java and input file: dictionary.txt Project#3 is an extension of the concepts and tasks of Lab#3. You will again read the dictionary file and resize the array as needed to store the words. Project#3 will require you to update a frequency counter of word lengths every time a word is read from the dictionary into the wordList. When your program is finished this histogram array will contain the following:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT