x.Hmk in C environment. The purpose of this program exercise is to expand the pr
ID: 3614884 • Letter: X
Question
x.Hmk in C environment.The purpose of this program exercise is to expand the previous program you wrote earlier to use a dynamic array rather than a static one to hold the words read from the Hangman.dat file. Modify previous program to request an array size from the user and then allocate memory from the heap for a WordArray of the size provided by the user. Allow the user to add additional words, and increase the size of the array as necessary. See below specs.
1) Modify the declaration of WordArray to make it a pointer variable that can point to a dynamic array of words.
2) After reading and storing the name of the input file, ask the user for the number of words to be read, and use this number as the initial size of your WordArray.
3) Allocate an appropriate amount of memory from the heap to create a dynamic array (WordArray) large enough to hold the number of words given by the user.
4) After using Read_words to read the words that are in the named file and storing them in WordArray as provided in previous program, provide a menu that allows the user to choose among the following options: (a) Add additional words to the WordArray (b) Randomly choose a word from the WordArray (c) Print the current list of words or (d) Quit the program
5) Create a new function called "ResizeArray" to be used whenever the number of words to be added to WordArray would exceed the current bounds of the array. (The user should not be aware that the size of the array is changing. Rather, he should simply be allowed to keep adding words until he is done, and ResizeArray should be called (transparently to the user) whenever the number of words to be added would exceed the bounds of the array so that the user may add as many words as he likes. If by any chance all heap memory is exhausted, an appropriate error message should be issued to the user)
6) Make sure that num_words is appropriately modified prior to calling get_random. Also, seed the random number generator just once (in main()), and use the system time to do so.
7) Each time option (b) is chosen (randomly choosing a word from WordArray), write the word to your output file using the "append" mode. Thuse, your output file may have several words in it when your program concludes.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_GUESSES 5
#define MAX_INCORRECT_GUESSES 9
#define MAX_WORD_LENGTH 15
#define MAX_WORDS 50
int read_words(char[],char[][MAX_WORD_LENGTH]);
void get_word(char[][MAX_WORD_LENGTH],char[],int);
int get_random(int);
int main()
{
FILE *output;
int i,count;
char filename[20];
char WordArray[MAX_WORDS][MAX_WORD_LENGTH];
char Word[MAX_WORD_LENGTH];
printf("This is the game of Hangman ");
printf("I will choose a word, and you will enter characters to ");
printf("try to guess the characters in my word without making ");
printf("more than nine incorrect guesses. If you guess all the letter ");
printf("in my word before making nine incorrect guesses, you win! ");
printf("What file would you like to use for data input: ");
scanf("%s",&filename);
printf(" ");
printf("We will be using the file %s for our word choices today. ");
count=read_words(filename,WordArray);
if(count==-1)
return 1;
get_word(WordArray,Word,count);
printf("Your file had %d words ", count);
printf("Your *hangman secret word* is: %s ",Word);
printf("What file name would you like to use for saving the chosen word?: ");
scanf("%s",&filename);
printf(" ");
printf("The chosen word has been saved! ");
printf("That's all for now. ");
printf("Goodbye! ");
output= fopen(filename,"w");
if(output==NULL)
{
printf("Error opening Output File! ");
getchar();
return 0;
}
fprintf(output,"%s ",Word);
fclose(output);
getchar();
system("pause");
}
void get_word(char a[][MAX_WORD_LENGTH],char w[],int count)
{
int n,i;
n=get_random(count);
//printf("Random number is %d ",n);
for(i=0;i<MAX_WORD_LENGTH;i++)
w[i]=a[n][i];
return;
}
int get_random(int num_words)
{
srand(23); //"seeds" the random number generator
return(rand() % num_words);
}
int read_words(char filename[],char a[][MAX_WORD_LENGTH])
{
int i,j,count=0,k;
FILE *input;
input= fopen(filename,"r");
if(input==NULL)
{
printf("Error opening Input File! ");
getchar();
return -1;
}
i=0;
while(i<MAX_WORDS)
{
j=0;
k=0;
while(j<MAX_WORD_LENGTH)
{
a[i][j]=fgetc(input);
k=1;
if(a[i][j]==EOF)
k=0;
if(a[i][j]==' '||k==0||a[i][j]==' ')
{
a[i][j]='';
if(j!=0)
//{
count++;
Explanation / Answer
x.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.