CSE 220 Assignment 3 Hints and Tips
Some hints for approaching this assignment is as follows:
Approaching the assignment :
General Overview
It would be a good thing if before you start your assignment you try to decompose the problem
you are trying to solve into small segments. Each segment can be then written and tested for
any bugs or errors. Doing so will help you learn the true nature of functions and how effective
they can be.
The assignment requires that you read a string from either the stdin or a file and then count the
frequency of words in it and display it to stdout or to a file. We can look at this in the following
way :
1) Read an input from one of two sources
a. stdin
b. an input file
2) Find the frequency of words from input string
3) Write the words and their frequency to one of two ouputs
a. Stdout
b. An outputifle
One way of solving the problem
One way of approaching this code would be to create a structure which will hold a word and its
frequency and then create an array of that structure to accommodate all the unique words in
the input. So you would create a structure as follows:
struct wordStorage
{
char word[50];
int count;
};
Then when you want to create an array of that structure you would write it out as :
struct wordStorage arr[size];
So we created an array to of size number of elements of the structure wordStorage created
above.
This array can then be used to keep track of all the unique words you come across in the input
string and their frequencies. You can create arrays of this structure after you have defined the
structure itself.
You could then use the following prototypes in your code:
1. void decomposeToArray(char *para,char wordarr[1000][50])
This function takes in a string para and stores all the words in that string. You can
modify it to return the array of words containing all the words using –
char** decomposeToArray(char *para,char wordarr[1000][50]);
2. void frequencyOfWords(struct wordStorage *wordarr,char wordList[1000][50])
This function takes in a structure array in which element contains a word and its
frequency and the array of words that you created using decomposeToArray(args).
Instead of using a void function you can modify it so that it stores the words and
their frequency as an array of the given structure and then returns the structure
array. The prototype then becomes –
struct wordStorage* frequencyOfWords(wordList[1000][50])
3. void readFromFile(char *filename,char sentence[1000])
This function takes in the name of a file and a string to store all the contents of the
file. You can use pass by reference to keep track of the string that is storing the
string OR you could modify the function to read the name of a file and return a
string using:
char* readFromFile(char *filename)
4. void writeToFile(char *filename,struct wordStorage *wordarr);
This function prints the contents of the struct.
CSE 220 Assignment 3 Hints and Tips Some hints for approa.docx
1. CSE 220 Assignment 3 Hints and Tips
Some hints for approaching this assignment is as follows:
Approaching the assignment :
General Overview
It would be a good thing if before you start your assignment you
try to decompose the problem
you are trying to solve into small segments. Each segment can
be then written and tested for
any bugs or errors. Doing so will help you learn the true nature
of functions and how effective
they can be.
The assignment requires that you read a string from either the
stdin or a file and then count the
frequency of words in it and display it to stdout or to a file. We
can look at this in the following
way :
1) Read an input from one of two sources
a. stdin
b. an input file
2) Find the frequency of words from input string
3) Write the words and their frequency to one of two ouputs
a. Stdout
b. An outputifle
2. One way of solving the problem
One way of approaching this code would be to create a structure
which will hold a word and its
frequency and then create an array of that structure to
accommodate all the unique words in
the input. So you would create a structure as follows:
struct wordStorage
{
char word[50];
int count;
};
Then when you want to create an array of that structure you
would write it out as :
struct wordStorage arr[size];
So we created an array to of size number of elements of the
structure wordStorage created
above.
This array can then be used to keep track of all the unique
words you come across in the input
string and their frequencies. You can create arrays of this
structure after you have defined the
structure itself.
You could then use the following prototypes in your code:
1. void decomposeToArray(char *para,char wordarr[1000][50])
This function takes in a string para and stores all the words in
3. that string. You can
modify it to return the array of words containing all the words
using –
char** decomposeToArray(char *para,char wordarr[1000][50]);
2. void frequencyOfWords(struct wordStorage *wordarr,char
wordList[1000][50])
This function takes in a structure array in which element
contains a word and its
frequency and the array of words that you created using
decomposeToArray(args).
Instead of using a void function you can modify it so that it
stores the words and
their frequency as an array of the given structure and then
returns the structure
array. The prototype then becomes –
struct wordStorage* frequencyOfWords(wordList[1000][50])
3. void readFromFile(char *filename,char sentence[1000])
This function takes in the name of a file and a string to store all
the contents of the
file. You can use pass by reference to keep track of the string
that is storing the
string OR you could modify the function to read the name of a
file and return a
string using:
char* readFromFile(char *filename)
4. void writeToFile(char *filename,struct wordStorage
*wordarr);
4. This function prints the contents of the structure array wordarr
into the file whose
name is specified by filename
5. void displayWords(struct wordStorage *wordarr);
This functions writes the contents of the structure array (
containing words and their
frequencies) on the standard output i.e. your display
The prototypes mentioned above will then need to be linked in
the main function depending on
the arguments that are passed through the command line.
For example :
You can read a string from stdin and use
decomposeToArray(args) to break your input
string into a list of words. This list can be stored as a 2D
character array and then sent to the
function
frequencyOfWords(list_you_got_from_decomposeToArray) and
the function will
return a structure array that consists of all the unique words in
the string and their frequencies.
[Note : frequencyOfWords() must have a structure array inside
it in order to be able to return it. If you are using
the void frequencyOfWords(struct wordStorage *wordarr,char
wordList[1000][50]) variation, then you will need
to use Pass By Reference to handle it.]
5. Dealing with 2D Character arrays in C:
2D arrays in C can be a little overwhelming to understand
especially when dealing with strings.
It is important to understand the structure of the 2D character
array while dealing with an array
of strings.
For example if you were to store a string in a single
dimensional character array you could do it
in the following ways :
char *array=”This is a string in C!”;
char array[]=”This is a string in C!”;
char array[100];
strcpy(array,”This will also be a string!”);
So you can see that a string in C is basically an array of
characters.
When dealing with an array of strings, you need to look at it
like an array of strings or in our
case an array of arrays ( where each string is an array!)
So if we were to accept a sentence from the user and then store
each word in an array, we
would need a 2D array to do so.
Think of it like this :
char words[rows][cols];
Each row is a word and each column in a row is a character
6. belonging to a certain word.
So when you find a character in a word, you add it to the
column of the present row ( the
present word you are looking at). If a word is completed, then
you go to the next row.
The way to approach this would be to first accept the sentence
from the user. Then try to
decompose that sentence by selecting end of word markers ( or
string delimiters). In the
example which follows below, we assume that the space (‘ ‘)
character is what signifies the end
of a word. So if we had a string like : “This is a new sentence “,
then we should be able to break
it into 5 words where each word ends with a space character(‘
‘). Now once we have set our
word delimiters we need to check each character in the input
string and decide whether a word
is completely read or not. In order to do so, run a loop, extract
each character into a dummy
variable and check if it is your word delimiter ( here we use
space only, you should consider
punctuation marks and so on). If a character is not the word
delimiter, add it to the word count
2D array along the second dimension( columns in the 2D array)
.Otherwise if it is a word
delimiter, then a word has been completely read from the input
string – so we increment the
counter on the 1st dimension ( rows of the 2D array used) of the
2D array to go to the next word
slot.
For example :
7. char words[100][100]; // Assume that we are going to have a
maximum of 100 words of size
100 characters each
char sentence[1000]; //Assuming that the input string is just
composed of 1000 characters
char ch; // A dummy variable to act as a buffer for the input
string
int len,wc=0,c=0,i=0;
printf("Enter a sentence:");
//Using the %[n]s we can force the scanf to ignore n
characters. This allows us to input n
scanf("%[n]s",&sentence);
strcat(sentence," "); // I am assuming that every word in the
string ends with a space
len=strlen(sentence); //Calculate the length of the string input
//The next part will show you how each word is read from the
string and then stored inside a
// 2D array for processing later
for(i=0;i<len;i++)
{
ch=sentence[i]; //Read each character from the input string
if(ch==' ') // If you have reached a space (‘ ‘)character , it
implies a word is completely
read
{
words[wc][c++]='0'; // Terminate each string with a ‘0’
character
wc++; // We just finished adding a new word
c=0; //Reset the counter which keeps track of the letter inside
the word
}
else // If you read anything other than a ‘ ‘, add it as a letter to
the present word
{
8. words[wc][c++]=ch;
}
}
Reflection Requirement:
Ready for a completely open-ended reflection response? If so,
then go for it!
If not, here are some suggestions to guide your thought process.
Potential topics to address in your reflection:
Why are “standards” so prevalent in education today? Why are
“accountability” and “standards” the buzz words in many
professions and society today? Isn’t it good to have standards in
your life? So why is education any different?
Some of you may have understood that the prevalent use of
standards was just the way that schools are. If so, what was
your reaction to Eisner’s view of the standards system in “The
Centrality of Curriculum and the Function of Standards”?
#include<stdio.h>
9. #include<string.h>
#include<stdlib.h>
#include<ctype.h>
int numWords;
int numUniqWords;
struct wordStorage
{
char word[50];
int count;
};
/* You can modify the functions to return objects instead of
using void and pass by reference */
/* This is your choice and depends on how you wish to code it
*/
void decomposeToArray(char *para,char wordarr[1000][50]);
void frequencyOfWords(struct wordStorage *wordarr,char
wordList[1000][50]);
10. void readFromFile(char *filename,char sentence[1000]);;
void writeToFile(char *filename,struct wordStorage *wordarr);
void displayWords(struct wordStorage *wordarr);
/* This function is optional and you can use it to create a robust
program that can read and write from
any of the sources. You adjust the read and write values and
according to some set values ( eg 0=>STDIO and 1=>FILE)
you call the required functions to process your input
*/
void executeCode(int read,int write,char readFile[100],char
writeFile[100]);
int main(int argc,char *argv[])
{
int i,j;
char readFile[100];
char writeFile[100];
int flag=0; // Checks for -h option. 1=FOUND , 0=NOT
FOUND
11. if(argc<=1)
{
// Fill code here when there are no options given i.e.
the time when you read from STDIN and write to STDOUT
}
else if(argc>=2)
{
for(i=1;i<argc;i++)
{
if(strcmp(argv[i],"-h")==0)
{
printf("nRead English Language text and
calculate word frequencies.n");
printf("nn");
printf("-h Display this help
message.n");
printf("-f file Read text from given
file.n");
printf("-o file Write program output to
file.n");
printf("-v Display version info and
12. exit.n");
flag=1;
break;
}
}
//Write code to link the functions and construct your
program overall
return 0;
}
void decomposeToArray(char *para,char wordarr[1000][50])
{
// Fill your code here
}
void frequencyOfWords(struct wordStorage *wordarr,char
wordList[1000][50])
{
// Fill your code here
13. }
void readFromFile(char *filename,char sentence[1000])
{
// Fill your code here
}
void writeToFile(char *filename,struct wordStorage *wordarr)
{
// Fill your code here
}
void displayWords(struct wordStorage *wordarr)
{
// Fill your code here
}
14. CSE220 Programming for Computer Engineering Assignment 3
CSE220 Page 1
Assignment 3 – CSE220 – Fall 2015
1. Instructions
This is an individual assignment. Please start working on it as
soon as possible and ask for help if you need it.
Your project solution document must be uploaded to Blackboard
by the assignment deadline. Section 6 de-
scribes what to submit and by when; read it now.
2. Assignment Objectives
1. Write a basic C program.
2. Define and use local variables and global constants.
3. Define and use one-dimensional arrays.
4. Define and use C-string variables and C-string arrays.
5. Use flow of control constructs such as if-statements, for-
loops, and while-loops.
6. Define and use C characters and Strings
7. Parse the command line for arguments and options.
8. Use functions from stdio.h to perform I/O with stdin/stdout
and text files.
9. Understand the difference between declaration and definition.
10. Understand the difference between static and nonstatic
functions, variables, and constants.
11. Use the GCC C compiler to compile a C program. Use the
GDB debugger to debug a program.
3. Software Requirements
15. Your project is to write a complete C program which will read
several lines of text and prints a table indicating
the number of occurrences of each different word in the text.
The program should include the words in the table
in the same order in which they appear in the text. For example,
the lines
To be, or not to be: that is the question:
Whether ‘t is nobler in the mind to suffer
Contains the word “to” three times, “be” two times, “or” once,
and so on..
By default, the program will read input from stdin and send
output to stdout. The -f file option will cause the
program to read the input from the text file named file and the -
o file option will cause the program to write the
output to the file named file. The options are,
-h Display a help message and exit.
-f file Read input from the text file named file.
-o file Output the results to the file named file.
-v Display version info and exit.
CSE220 Programming for Computer Engineering Assignment 3
16. CSE220 Page 2
The -h Option
Your program shall output the following help message when the
-h option is specified,
$ ./hw3 -h
Usage: hw3 [option]...
Read English language text and calculate word frequencies.
Options:
-h Display this help message and exit.
-f file Read the text from “file”.
-o file Write the results to “file.n
-v Display version info and exit.n
By default, text is read from stdin and the results are sent to
stdout.
The -h option has higher precedence than all of the other
options, meaning that if it specified along with other
options, the program shall act as if -h were the only option,
$ ./hw3 -f foo -o prog -h -v
The above help message is displayed
The -v Option
Your program shall output the following version information
when the -v option is specified,
17. $ ./hw3 -v
hw3: version 1.0 (2015.8.13)
Faye. Navabi <[email protected]>
Replace my name and email address with your name and email
address, and replace the date with the date you
complete your program. If -h and -v are both specified on the
command line, then -h shall take precedence, i.e.,
display the help message and exit.
$ ./hw3 -v -h
The above help message is displayed
The -f and -o Options
If -f or -o is specified without a file name, display an error
message and terminate,
CSE220 Programming for Computer Engineering Assignment 3
CSE220 Page 3
$ ./hw3 -f
hw3: expecting argument following -f
$ hw3 -o
hw3: expecting argument following -o
If the file specified with -f cannot be opened for reading
(probably because it does not exist or the user made a
typo), then display an error message,
$ ./hw3 -f foo
18. hw3: could not open foo
If the file specified with -o cannot be opened for writing
(probably because the file already exists and is read-
only), then display an error message,
$ ./hw3 -o /bin/date
hw3: could not open /bin/date
Default Input and Output
By default, the program shall read the input from stdin and shall
write the output to stdout,
$ ./hw3
Parting is such sweet sorrow. The dog ate the cat. Goodbye.
^D
The output is then displayed on the terminal window.
4.Submission
When your project is complete, type make clean in your source
code directory to clean your object files. the
Then create a bzipped tarball named cse220-f15-hw3-
lastname.tar.bz2, where lastname is your last name.
$ make clean
$ cd ..
$ tar cvjf cse220-f15-hw3-lastname.tar.bz2 cse220-hw3
Submit this tarball to Blackboard using the assignment
submission link before the deadline.