Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

COP3502C Computer Science 1 Dr. Andrew Steinberg Spring 2022 Programming Assignment 1 Tell me a Random Story Max Points 100 Due: 2/6/2022 at 11:59pm Background Story of this Assignment All of you...

2 answer below »

COP3502C Computer Science 1
Dr. Andrew Steinberg
Spring 2022
Programming Assignment 1
Tell me a Random Story
Max Points 100
Due: 2/6/2022 at 11:59pm
Background Story of this Assignment
All of you should of passed COP3223C Introduction to Programming in C (or a similar course) to be
enrolled in CS1. Some of you took the prerequisite last semester, some of you may have taken the
prerequisite two semesters ago, and some of you may have taken the prerequisite over a year ago or more.
Nevertheless, the goal of this assignment is to serve as a warmup with dynamic memory as it will play a
heavy role in learning our data structures for CS1. If it has been months since you worked with C, then
you may feel a little rusty (don’t wo
y that is normal). That is why the first assignment is a review from
some prerequisite topics (such as strings and dynamic memory). However, it is critical, to regain these
skills in dynamic memory, strings, and structs (even though this assignment doesn’t cover structs) so we
can smoothly cover data structures and learn their ADT.
In this assignment, we will touch upon the topics of strings and dynamic memory. You are going to create
a program that generates random sentences. These random sentences won’t make sense however we will
still follow the general rules of English of forming one. Since we are going to generate random sentences,
we will also create the most random story that won’t even make sense.
Start Early and see the TAs and ULAs! They are here to help you! Don’t procrastinate!
Assignment Details
This section covers the assignment in more detail.
• The random sentence generated will be formulated by these types of words.
sentence = article1 adjective1 noun1 ve
preposition article2 adjective2 noun2
Note: The subscript represents that a different word of the respective category can be generated.
For example article1 = ”The” and article2 = ”A”.
• Here is a sample random sentence generated from the working program using the sentence
formula from above. Sometimes words picked from respective categories can be the same (such
as the article and adjective in this case). Note that there is one blank white space between each
word and a period at the end of the sentence.
Here is another example where each word from the categories are all unique.
COP3502C Computer Science 1
Dr. Andrew Steinberg
Spring 2022
• You might notice that the first letter in articles are all capitalized. This is due that the sentence
formula starts with an article. Make sure for the articles, that they are all capitalized with the first
letter. It should already be provided that way for you in the C file on Webcourses. Do not change
it!
• You will use 5 separate dynamic 2D a
ays (utilizing double pointers) in storing the respective
words in their respective categories (noun, ve
, adjective, preposition, and article).
The Provided Skeleton File
You were provided with a skeleton C file that has the main function and function prototypes. This section
will discuss the lines of code provided for you in the main function to assist you with understanding how
the code will execute.
• Lines 6 – 9 are preprocessor directive statements. The first three import content from the standard
I/O li
ary, standard li
ary, and string li
ary. This will allow you to use certain built in
functions in completing the assignment.
• Line 9 defines a macro constant called LIMIT that holds the value 20. This will be number of
characters a string can store. That means the words being read can only be of size 19 (we have to
include \0 character).
• Lines 11 – 16 are the function prototypes. This is discussed in the next section in more detail.
• Lines 18 – 126 is the main function. Let’s divide up parts of the main function.
o Lines 21 – 24 sets up the randomness of our program. This will low the script the grader
use in determining a random scenario to truly check your work properly.
o Line 24 calls srand(). This is a function part of stdlib that generates a seed for the
pseudo random generator. This will allow you to run the code with the same scenario
epeatedly. If you change the seed, then you will get a different scenario which will result
in a different output.
o Lines 27 – 30 declares/initializes variables that keeps track of the size each dynamic 2D
a
ays of each category respectively cu
ently stores. In simple terms, it will tell you how
many words are in each category. They start empty which is why is 0 is the first value
assigned.
o Line 32 – 36 declares/initializes variables that keeps track of the max size each dynamic
a
ay can hold. In simple terms, it keeps track of how much the heaps can store fully
(number of elements). Note: The number of articles used is 3 (“a”, “an”, and “the”). That
is why const is used since we will not be changing the size of the a
ay during the
program run.
o Lines 41 – 45 declares 5 double char pointers and assigns them to NULL.
o Lines 47 – 51 assigns each of the double char pointers to a heap of single char
pointers.
o Lines 54 – 59 checks to make sure that the previous malloc call was successful in
providing an address to a heap. If it was not successful then, the program terminates.
o Lines 62 – 67 populates the articles 2D a
ay with the respective three articles used in the
English Language. It also allocates a heap to store the string.
COP3502C Computer Science 1
Dr. Andrew Steinberg
Spring 2022
o Lines 70 – 73 opens the text files of words of each respective category. Those text files
must be in the same directory as your C file.
o Lines 76 – 81 checks to make sure the files were properly opened, otherwise the program
terminates.
o Lines 84 – 87 calls a user defined function (you will have to implement) populate that
will fill the dynamic a
ays with the respective words in their category. Details of
populate are in the section.
o Lines 90 – 93 closes the text files since they will no longer be needed. The words should
e in the 2D dynamic a
ays.
o Lines 99 – 104 contains a for loop that will generate some random sentences. This is
done by the user defined function generatesentence (which you will implement).
Line 101 specifically calls a user defined function displaysentence that will display
a random sentence to the terminal window. Line 103 frees the heap to prevent memory
leaks. Think about how this will relate to the function generatesentence. That is
part of the challenge.
o Lines 106 – 117 will open 3 text files to store 3 random stories respectively.
o Line 119 calls a user defined function cleanUp that frees any heap memory in use.
You will have to implement this user defined function. That is part of the challenge.
The Function Prototypes
You are going to implement 6 user defined functions. In the provided main function, you were provided
the prototypes. This section will describe what it is expected from each definition.
void displaysentence(char * sentence);
The displaysentence function will display the sentence to the terminal window. The function takes
one argument which is a char pointer (dynamic string) and will display it on a single line. Here is a
sample screenshot of the function being called three times consecutively.

char ** populate(char ** words, FILE *fptr, int *cu
entsize, int *maxsize);
The populate function will assign actual values to the dynamic a
ay. It will fill it with strings from the
provided text files. The function will return the populated 2D dynamic string a
ay. The functions takes 4
arguments:
1. A double pointer of type char called words. This is a 2D dynamic a
ay that contains words
(strings) from the respective category (noun, ve
, adjective, preposition).
2. A file pointer that contains the location of the text file from being read. The file should be
open in the main function.
3. A pointer of type int called cu
entsize. This pointer holds the address of an integer that
keeps track of the amount of strings stored in the 2D dynamic a
ay.
COP3502C Computer Science 1
Dr. Andrew Steinberg
Spring 2022
4. A pointer of type int called maxsize. This pointer holds the address of an integer that keeps
track of the maximum amount of strings the 2D dynamic a
ay can store.
There are some things you need to consider for this function implementation.
• When allocating heap space for the string, make sure it was successful. Remember malloc
doesn’t always return an address. Perform the necessary action recommended if that is the
case. It is good practice to prevent segmentation faults.
• When the program starts running, the 2D dynamic a
ay starts with 5 strings it can hold
potentially. However, you will notice that the provided text files has more than 5 strings to
ead. That means we will need to allocate more heap space some how. Think about how
doubleIt will work in this scenario. That is part of the challenge in this assignment.
char ** doubleIt(char **a
, int *maxsize);
The doubleIt function will increase the heap size by doubling the original space. The functions takes 2
arguments:
1. A double pointer of type char called a
. This is a 2D dynamic a
ay that contains words
(strings) from the respective category (noun, ve
, adjective, preposition). This is the original
heap.
2. A pointer of type int called maxsize. This pointer holds the address of an integer that keeps
track of the maximum amount of strings the 2D dynamic a
ay can store. When this function
is invoked, the cu
ent size of the dynamic a
ay is at capacity so the values should match.
This value will need to be multiplied by 2.
The function allocates a new heap that is twice the size of the original heap and copy the values from the
original heap into the new heap. One important note about this is that you have to properly copy the
values over in order to avoid segmentation fault. Think about how this can be avoided.
Answered 3 days After Jan 30, 2022

Solution

Savita answered on Feb 03 2022
121 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here