CSCI 241 Assignment 1 Assignment XXXXXXXXXXpoints Purpose This assignment is an exercise in writing, compiling, and executing a C++ program on the departmental UNIX system. It also covers the...

i put the assignment instructions in the attach files


CSCI 241 Assignment 1 Assignment 1 - 100 points Purpose This assignment is an exercise in writing, compiling, and executing a C++ program on the departmental UNIX system. It also covers the manipulation of C strings. Set Up 1. Log in to Unix. 2. Change to your csci241 directory: cd csci241 3. Create a directory to hold your files for Assignment 1: mkdir Assign1 4. Change to your Assign1 directory: cd Assign1 5. In that directory, make a symbolic link to the data file for this assignment: ln -s /home/turing/t90kjm1/CS241/Data/Fall2019/Assign1/input.txt 6. In this assignment, you will be creating several source code and header files, as described below. You can create each of these files separately using the nano editor, just as you did on Assignment 1. 7. To create a new source file for this programming assignment, type: nano assign1.cpp If the file assign1.cpp does not exist, an empty text file will be opened for you. If it does exist, it will be opened for editing. You can type your source code into the file. To save, type Ctrl-o. To save and exit, type Ctrl-x. Makefile The file named makefile tells the make utility how to build the final executable file from a collection of C++ source code and header files. The makefile for this assignment is given in its entirety below. Makefiles for future assignments will follow this basic pattern. Like any other text file on Unix, this file can be edited and saved using nano or another text editor. IMPORTANT NOTE:IMPORTANT NOTE: The commands that appear in the makefile below MUSTMUST be indented as shown. Furthermore, the indentation MUSTMUST be done using a tab character, not spaces. If you don't indent your makefile commands, or indent using spaces, your makefile WILL NOT WORKWILL NOT WORK. # # PROGRAM: assign1 # PROGRAMMER: your name # LOGON ID: your z-id # DATE DUE: due date of program # # Compiler variables CCFLAGS = -Wall -Werror -std=c++11 # Rule to link object code files to create executable file assign1: assign1.o g++ $(CCFLAGS) -o assign1 assign1.o # Rule to compile source code file to object code assign1.o: assign1.cpp g++ $(CCFLAGS) -c assign1.cpp # Pseudo-target to remove object code and executable files clean: -rm *.o assign1 Once you've written the file makefile, you can simply type the command make to compile and link your program. To clean up the object code and executable files before submitting your assignment, use the command make clean. To run the executable file created by the make command, type: ./assign1 Program For this assignment, you will write a program to count the number of times the words in an input text file occur. The WordCount Structure Define a C++ struct called WordCount that contains the following data members: An array of 31 characters named word An integer named count Functions Write the following functions: int main(int argc, char* argv[]) This function should declare an array of 200 WordCount objects and an integer numWords to track the number of array elements that are in use. If no input file name is specified as a program argument when the program is run, then argc will be equal to 1. If so, print an error message similar to the following and exit the program: Usage: assign1 [file-name] Call the function countWords() passing argv[1] as the file name parameter. Store the value returned by the function in numWords. Call the function sortWords() to sort the array. Print a header line as shown in the sample output, then call printWords() to print the words and their counts. int countWords(const char* fileName, WordCount wordArray[]) Parameters: 1) A C string that will not be changed by the function and that contains the name of an input file; 2) an array of WordCount objects. Returns: The number of distinct words that the function stored in the array (i.e., the number of array elements filled with valid data). This function should declare a file stream variable and open it for the file name passed in as the first parameter. If the file fails to open successfully, print an error message and exit the program. Declare an integer numWords to keep track of the number of distinct words stored in the array of of WordCount objects. This variable should be initialized to 0. The function should then read words from the file as C strings using the >> operator until end-of-file is reached. For each word read, the function should do the following: 1. Call stripPunctuation() to strip any punctuation from the beginning and end of the word string. If the resulting string is empty (length 0), the remaining steps can be skipped. 2. Call stringToUpper() to convert the word string to uppercase. 3. Call searchForWord() to search for the word string in the array of WordCount objects. 4. If the index returned by the search is -1, this is a new word that must be added to the end of the array. Copy the word string into the word data member of the next empty array element (the one at index numWords), and set its count data member to 1. Then increment numWords. 5. Otherwise, this word has been found in the array. Increment the count data member for the WordCount object at the index returned by the search. Once all words have been read from the file, the file should be closed and numWords should be returned. void stripPunctuation(char* s) Parameters: 1) A C string that contains a word to be stripped of punctuation. Returns: Nothing. This function should remove any punctuation characters at the beginning and end of the C string s. For example: The string "textile" should become textfile The string (Wikipedia, should become Wikipedia The string content. should become content The string generic should remain generic It is possible (although rare) for a string to contain nothing but punctuation characters. In that case, the result of executing this function should be an empty string. There are a number of valid approaches to solving this problem. You will need to be able to distinguish between punctuation characters and non-punctuation (or alphanumeric) characters; the C library functions isalnum() and ispunct() can help you do that. Performing the required modifications to the string "in place" may be difficult, so feel free to use a local temporary character array to make your changes and then copy the final result back into s at the end of the function. void stringToUpper(char* s) Parameters: 1) A C string that contains a word to be converted to uppercase. Returns: Nothing. This function should loop through the characters of the C string s and convert them to uppercase using the C library function toupper(). int searchForWord(const char* word, const WordCount wordArray[], int numWords) Parameters: 1) A C string that will not be changed by this function and that contains a word to search for; 2) an array of WordCount objects to search that will not be changed by this function; 3) the number of elements in the array filled with valid data. Returns: If the search was successful, returns the index of the array element that contains the word that was searched for, or -1 if the search fails. This function should use the linear search algorithm to search for the C string word in wordArray. void sortWords(WordCount wordArray[], int numWords) Parameters: 1) An array of WordCount objects to sort; 2) the number of elements in the array filled with valid data. Returns: Nothing. This function should sort the array of WordCount objects in ascending order by account number using the selection sort algorithm. The sort code linked to above sorts an array of integers called numbers of size size. You will need to make a number of changes to that code to make it work in this program: 1. Change the parameters for the function to those described above. 2. In the function body, change the data type of temp to WordCount. This temporary storage will be used to swap elements of the array of WordCount objects. 3. In the function body, change any occurrence of numbers to the name of your array of WordCount objects and size to numWords (or whatever you called the variable that tracks the number of array elements filled with valid data. 4. The comparison of numbers[j] and numbers[min] will need to use the C string library function strcmp() to perform the comparison. The final version of the if condition should look something like this: if (strcmp(wordArray[j].word, wordArray[min].word) < 0) ... http://www.cplusplus.com/reference/cctype/isalnum/ http://www.cplusplus.com/reference/cctype/ispunct/ http://www.cplusplus.com/reference/cctype/toupper/ http://faculty.cs.niu.edu/~mcmahon/cs241/notes/selection_sort.html 5. it is legal to assign one wordcount object to another; you don't need to write code to copy individual data members. void printwords(const wordcount wordarray[], int numwords) parameters: 1) an array of wordcount objects to print that will not be changed by this function; 2) the number of elements in the array filled with valid data. returns: nothing. this function should loop through the array and print each word and its corresponding count, neatly formatted into columns similar to the sample output. it should also print the number of words in the file (which is equal to the sum of the counts) and the number of distinct words (equal to numwords). programming notes remember that the c++ compiler works in a top-down fashion and that you must declare or define a symbol before you attempt to use it. for library functions, that means you must #include the header file that contains the function's declaration prior to trying to call the function. for your own functions, you should code a function declaration (i.e., a function prototype) prior to trying to call the function. in both cases, this is usually done at the top of the program, before the main() function. any function prototype that makes use of the wordcount data type must be coded after that structure definition. document your functions using the guidelines found on the course web site. programs that do not compile successfully on turing / hopper automatically receive 0 points. submit your program using the electronic submission guidelines posted on the course web site and described in class. assignment 1 - 100 points purpose set up makefile program the wordcount structure functions programming notes csci 241 assignment submission instructions csci 241 - assignment submission instructions all assignments for this class will be submitted electronically from turing/hopper using the e-mail system indirectly. the most important point to be made here is do not send your programs directly with e-maildo not send your programs directly with e-mail. with as many students as there are in the 0)="" ...="" http://www.cplusplus.com/reference/cctype/isalnum/="" http://www.cplusplus.com/reference/cctype/ispunct/="" http://www.cplusplus.com/reference/cctype/toupper/="" http://faculty.cs.niu.edu/~mcmahon/cs241/notes/selection_sort.html="" 5.="" it="" is="" legal="" to="" assign="" one="" wordcount="" object="" to="" another;="" you="" don't="" need="" to="" write="" code="" to="" copy="" individual="" data="" members.="" void="" printwords(const="" wordcount="" wordarray[],="" int="" numwords)="" parameters:="" 1)="" an="" array="" of="" wordcount="" objects="" to="" print="" that="" will="" not="" be="" changed="" by="" this="" function;="" 2)="" the="" number="" of="" elements="" in="" the="" array="" filled="" with="" valid="" data.="" returns:="" nothing.="" this="" function="" should="" loop="" through="" the="" array="" and="" print="" each="" word="" and="" its="" corresponding="" count,="" neatly="" formatted="" into="" columns="" similar="" to="" the="" sample="" output.="" it="" should="" also="" print="" the="" number="" of="" words="" in="" the="" file="" (which="" is="" equal="" to="" the="" sum="" of="" the="" counts)="" and="" the="" number="" of="" distinct="" words="" (equal="" to="" numwords).="" programming="" notes="" remember="" that="" the="" c++="" compiler="" works="" in="" a="" top-down="" fashion="" and="" that="" you="" must="" declare="" or="" define="" a="" symbol="" before="" you="" attempt="" to="" use="" it.="" for="" library="" functions,="" that="" means="" you="" must="" #include="" the="" header="" file="" that="" contains="" the="" function's="" declaration="" prior="" to="" trying="" to="" call="" the="" function.="" for="" your="" own="" functions,="" you="" should="" code="" a="" function="" declaration="" (i.e.,="" a="" function="" prototype)="" prior="" to="" trying="" to="" call="" the="" function.="" in="" both="" cases,="" this="" is="" usually="" done="" at="" the="" top="" of="" the="" program,="" before="" the="" main()="" function.="" any="" function="" prototype="" that="" makes="" use="" of="" the="" wordcount="" data="" type="" must="" be="" coded="" after="" that="" structure="" definition.="" document="" your="" functions="" using="" the="" guidelines="" found="" on="" the="" course="" web="" site.="" programs="" that="" do="" not="" compile="" successfully="" on="" turing="" hopper="" automatically="" receive="" 0="" points.="" submit="" your="" program="" using="" the="" electronic="" submission="" guidelines="" posted="" on="" the="" course="" web="" site="" and="" described="" in="" class.="" assignment="" 1="" -="" 100="" points="" purpose="" set="" up="" makefile="" program="" the="" wordcount="" structure="" functions="" programming="" notes="" csci="" 241="" assignment="" submission="" instructions="" csci="" 241="" -="" assignment="" submission="" instructions="" all="" assignments="" for="" this="" class="" will="" be="" submitted="" electronically="" from="" turing/hopper="" using="" the="" e-mail="" system="" indirectly.="" the="" most="" important="" point="" to="" be="" made="" here="" is="" do="" not="" send="" your="" programs="" directly="" with="" e-maildo="" not="" send="" your="" programs="" directly="" with="" e-mail.="" with="" as="" many="" students="" as="" there="" are="" in="">
Sep 09, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here