Midterm CP264 spring 2021 PDF

Title Midterm CP264 spring 2021
Course Discrete Structures
Institution Wilfrid Laurier University
Pages 11
File Size 302.6 KB
File Type PDF
Total Downloads 57
Total Views 137

Summary

Midterm questions for cp264 (coding ones)...


Description

CP264 Spring 2021

Midterm

CP264: Spring 2021 Midterm Midterm Details: Date: Monday, June 21, 2020 Time: 11:00 – 2:00 pm Location: Take-home Weight: 15 pts Midterm Guidelines: 1- The midterm is available from 11:00 am to 2:00 pm for all students, unless another time period have been assigned. 2- The midterm is designed to be completed in 2 hours. However, an extra hour is granted to accommodate for any technical issues that could arise. ALC extra time is computed based on the 2 hours. 3- No late submissions will be accepted for the midterm. Therefore, I suggest that you upload your solution after each question. Note, that your new submission will override your previous submission permanently. 4- If for any reason, you did not submit in time, then you can submit through the appeals dropbox. Submission through appeals may be subject to a penalty up to 50%. 5- The midterm is open book, open notes and open internet access. You can search online for syntax and technical issues, but you cannot seek assistance of a third entity, whether in person or on the internet. 6- If you have any questions, post it in the forums. No one other than the instructor is allowed to respond to posts in the midterm forum. Also, you should not post screenshots of your solution. 1

CP264 Spring 2021

Midterm

7- I will be checking the forum sometime between 11:30 am to 1:00 pm. I will only respond to questions that I find necessary to answer. I will be having a lecture at 1:00 pm, therefore, I can’t respond to questions posted after 1:00 pm. 8- If you have any comments or notes that you need to inform the instructor about, write it on the designated box on to top of the midterm.c file.

Grading Guidelines: 1- No submission will be accepted without credentials on top of file or without the academic honesty certification. 2- The midterm is composed of four programming tasks. All tasks are equally weighted. 3- Check your answers against those presented in the output.txt file. Having a 100% match is a precondition for getting a full mark in a question. 4- Your program should have no errors and warnings. A penalty of -0.2 will be applied for any warning. Your program should not crash. For each task that has errors, a 25% penalty will be applied to that task. If the instructor is unable to make your task run, then that question will have a 0. 5- The instructor will be inspecting your code. Therefore, you do not necessarily receive full mark with 100% match. The instructor will be checking for major issues, not minor coding issues. Major issues include, using illegal operations (even if they produce correct output) and not following task guidelines. Using techniques not taught in class is not allowed. 6- Be considerate of code readability (e.g. naming variables) and to some extent efficiency. You will not lose marks on efficiency, unless you make something excessive, like providing an O(n^3) solution which could be solved in O(n). 7- Use: https://text-compare.com/ to check your output against the output.txt. You need to have exact matching including spaces and punctuation. 2

CP264 Spring 2021

Midterm

File and Coding Instructions: 1- Create a C project in eclipse called midterm. 2- Download the midterm.zip files and extract the files into to a folder in your machine. Drag and drop the files into the midterm folder. 3- Rename the file: “`” to “midterm.c”. 4- Open the file and Enter your credentials on top of file 5- Before submission, type the following certification on top of the file:

6- Do not edit any file other than midterm.c. You may edit the midterm_test.c file locally, for your own testing. 7- When you are done, you need to submit only your “midterm.c”. Do not export the project, upload a .zip file, or upload any other file. 8- Do not change the include instructions at the midterm.c

3

CP264 Spring 2021

Midterm

Task 1: Replacing a Character in a File Implement the following function: int replace_char(char* filename, char letter1, char letter2); The function receives a filename and two characters. The first character letter1 is an alphabetical character, while the second character letter2 is a non-alpha character. The function replaces every occurrence of letter1 in the file with letter2. All file handling should happen inside the function. The function is not case sensitive. This means all instances of letter1 , upper or lower case, should be replaced by the non-alpha character letter2. The function returns an integer representing the number of replacements made in the file. The function handles two error scenarios: 1- Failure to open the given file. 2- Receiving a non-alpha character for letter1 or an alpha character for letter2. In both error cases, the function returns -1. When running the testing file, your output for each case is produced in the files: “input11.txt”, “input12.txt”, “input13.txt” and “input14.txt” respectively. You do not need to do anything to create or maintain these files. The original text is available at “input1.txt”, and gets copied to the above four files at each run of the program. The four files gets wiped clean with each run. The testing file prints the contents of these files to the console. Your output should match the following: --------------Testing replace_char -------------Case 1: Replacing a with _ Number of matches = 90 In Engl_nd, there w_s sc_rcely _n _mount of order _nd protection to justify much n_tion_l bo_sting. D_ring burgl_ries by _rmed men, _nd highw_y robberies, took pl_ce in the c_pit_l itself every night; f_milies were publicly c_utioned not to go out of town without removing their furniture to upholsterers' w_rehouses for security; the highw_ym_n in the d_rk w_s _ City tr_desm_n in the light, _nd, being recognised _nd ch_llenged by his fellow-tr_desm_n whom he stopped in his ch_r_cter of "the C_pt_in," g_ll_ntly shot him through the he_d _nd rode _w_y; the m_il w_s w_yl_id by seven robbers, _nd the gu_rd shot three de_d, _nd then got shot de_d himself by the other four, "in consequence of the f_ilure of his _mmunition: "_fter which the m_il w_s robbed in pe_ce; th_t m_gnificent potent_te, the Lord M_yor of London, w_s m_de to st_nd _nd deliver on Turnh_m Green, by one highw_ym_n, who despoiled the illustrious cre_ture in sight of _ll his retinue; prisoners in London g_ols fought b_ttles with their turnkeys, _nd the m_jesty of the l_w fired blunderbusses in _mong them, lo_ded with rounds of shot _nd b_ll; thieves snipped off di_mond crosses from the necks of noble lords _t Court dr_wing-rooms; musketeers went into St. Giles's, to se_rch for contr_b_nd goods, _nd the mob fired on the musketeers, _nd the musketeers fired on the mob, _nd nobody thought _ny of these occurrences much out of the common w_y.

4

CP264 Spring 2021

Midterm

Case 2: Replacing g with $ Number of matches = 28 In En$land, there was scarcely an amount of order and protection to justify much national boastin$. Darin$ bur$laries by armed men, and hi$hway robberies, took place in the capital itself every ni$ht; families were publicly cautioned not to $o out of town without removin$ their furniture to upholsterers' warehouses for security; the hi$hwayman in the dark was a City tradesman in the li$ht, and, bein$ reco$nised and challen$ed by his fellow-tradesman whom he stopped in his character of "the Captain," $allantly shot him throu$h the head and rode away; the mail was waylaid by seven robbers, and the $uard shot three dead, and then $ot shot dead himself by the other four, "in consequence of the failure of his ammunition: "after which the mail was robbed in peace; that ma$nificent potentate, the Lord Mayor of London, was made to stand and deliver on Turnham $reen, by one hi$hwayman, who despoiled the illustrious creature in si$ht of all his retinue; prisoners in London $aols fou$ht battles with their turnkeys, and the majesty of the law fired blunderbusses in amon$ them, loaded with rounds of shot and ball; thieves snipped off diamond crosses from the necks of noble lords at Court drawin$-rooms; musketeers went into St. $iles's, to search for contraband $oods, and the mob fired on the musketeers, and the musketeers fired on the mob, and nobody thou$ht any of these occurrences much out of the common way. Case 3: Replacing N with * Number of matches = 87 I* E*gla*d, there was scarcely a* amou*t of order a*d protectio* to justify much *atio*al boasti*g. Dari*g burglaries by armed me*, a*d highway robberies, took place i* the capital itself every *ight; families were publicly cautio*ed *ot to go out of tow* without removi*g their fur*iture to upholsterers' warehouses for security; the highwayma* i* the dark was a City tradesma* i* the light, a*d, bei*g recog*ised a*d challe*ged by his fellow-tradesma* whom he stopped i* his character of "the Captai*," galla*tly shot him through the head a*d rode away; the mail was waylaid by seve* robbers, a*d the guard shot three dead, a*d the* got shot dead himself by the other four, "i* co*seque*ce of the failure of his ammu*itio*: "after which the mail was robbed i* peace; that mag*ifice*t pote*tate, the Lord Mayor of Lo*do*, was made to sta*d a*d deliver o* Tur*ham Gree*, by o*e highwayma*, who despoiled the illustrious creature i* sight of all his reti*ue; priso*ers i* Lo*do* gaols fought battles with their tur*keys, a*d the majesty of the law fired blu*derbusses i* amo*g them, loaded with rou*ds of shot a*d ball; thieves s*ipped off diamo*d crosses from the *ecks of *oble lords at Court drawi*g-rooms; musketeers we*t i*to St. Giles's, to search for co*traba*d goods, a*d the mob fired o* the musketeers, a*d the musketeers fired o* the mob, a*d *obody thought a*y of these occurre*ces much out of the commo* way. Case 4: Error scenarios: Error (remove_letter): invalid input characters Number of matches = -1 Error (remove_letter): invalid input characters Number of matches = -1 Error (remove_letter): unable to open file nothing.txt Number of matches = -1 --------------End of Testing replace_char --------------

5

CP264 Spring 2021

Midterm

Task 2: Updating an Array Implement the function: void update_array(int** array, const int size, int multiplier) The function receives an array of integers along with its size and an input parameter called multiplier. The array is passed as a double integer pointer, to allow the function to make changes to the array. The function repeats the array elements based on the multiplier. For instance, if the array is [10, 20, 30] and multiplier is 2 the output will be: [10, 10, 20, 20, 30, 30].

You can see how each element is being repeated twice. If the multiplier is 4, then the output will be: [10, 10, 10, 10, 20, 20, 20, 20, 30, 30, 30, 30]

And so forth. The function does not return any value. The array had been created using dynamic memory allocation. Therefore, it should be resized inside the function based on the extra spots controlled by the multiplier. Since the array is passed as a double pointer, the changes made unto the array inside the function will be preserved when existing the function. If the value of multiplier is 0 or a negative number, then the function should print an error message and exist without making changes to the array. If the multiplier equals to 1, then this is valid, but no changes are made to the array. Assume that the array pointer and the size are always passed as valid values. Therefore, no error checking is required for them. Running the testing file will produce the following output:

6

CP264 Spring 2021

Midterm

--------------Testing update_array -------------Case 1: size = 6, multiplier = 2 Array Before update: [ 10 , 20 , 30 , 40 , 50 , 60 ] Array after update: [ 10 , 10 , 20 , 20 , 30 , 30 , 40 , 40 , 50 , 50 , 60 , 60 ] Case 2: size = 4, multiplier = 3 Array Before update: [ 10 , 20 , 30 , 40 ] Array after update: [ 10 , 10 , 10 , 20 , 20 , 20 , 30 , 30 , 30 , 40 , 40 , 40 ] Case 3: size = 10, multiplier = 1 Array Before update: [ 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 ] Array after update: [ 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 90 , 100 ] Case 4: size = 5, multiplier = 0 Array Before update: [ 10 , 20 , 30 , 40 , 50 ] Error (update_array): invalid multiplier Array after update: [ 10 , 20 , 30 , 40 , 50 ] --------------End of Testing update_array --------------

7

CP264 Spring 2021

Midterm

Task 3: Sentence to Words Implement the following function: char* sentence_to_words(char* sentence) The function receives one single parameter: a string called “sentence”. The main objective of the function is to detect words in the sentence. The function returns the last word in the sentence, in the type of a char*. This should be created using dynamic memory allocation inside the function. There are four scenarios that can take place. Scenario 1: if the sentence is passed as a NULL pointer, the function should print an error message and returns an empty string as the last word. Scenario 2: if the sentence is an empty string, the function should print that the sentence is empty and that there are no words found (see output sample). Scenario 3: if the sentence is composed of a single word, then the function should declare this (see sample output). In this case, the last word is the same as the sentence. Scenario 4: If the sentence is composed of multiple words, then the function should declare that this is a multi-word sentence and then print every word in the sentence (see sample output). Assume that the sentence contain no white spaces other than the space character. Also, assume that the maximum number of characters in the sentence is MAX, which is defined in the header file. Note that the function does not make any changes to the passed the sentence. Also, the last word string is not a pointer to the last word in the sentence. It is a copy of the last word in the sentence returned as a pointer. Running the testing file should give you an output matching the following: ------------ Testing sentence_to_words -------------Case 1: sentence with 2 words: sentence = Good luck! This is a multi-word sentence: word 1: Good word 2: luck! last word is: luck!

8

CP264 Spring 2021

Midterm

Case 2: sentence with 1 word: sentence = Fantastic! This is a single word sentence word: Fantastic! last word is: Fantastic! Case 3: sentence with 5 words: sentence = Best wishes for the midterm This is a multi-word sentence: word 1: Best word 2: wishes word 3: for word 4: the word 5: midterm last word is: midterm Case 4: empty sentence: sentence = This is an empty sentence No words found last word is: Case 5: NULL sentence: Error (sentence_to_words): invalid input last word is:

9

CP264 Spring 2021

Midterm

Task 4: Updating Pointers Implement the following function: void update_pointers(float* array, const int size, float** ptr1, float** ptr2, char mode) The function receives a float array along with its size, two double pointers and a mode character. The main task of the function is to update the two pointers based on the given mode. When passed to the function, ptr1 points to the first element of the array, and ptr2 points to the last item in the array. The function defines two modes of operation: ‘a’ and ‘z’. If the mode equals to ‘a’, the function should change ptr1 to point to the minimum value in the array, and ptr2 to point to the maximum value in the array. If the mode equals to ‘z’, the function should change ptr1 to point to the maximum value in the array, and ptr2 to point to the minimum value in the array. The function does not return any value The function handles four error scenarios: 1234-

If the array is NULL If the array size is 0 or less If one of the double pointers, or reference pointers is a NULL If the mode is other than ‘a’ or ‘z’

Running the testing function will produce an output similar to the following: ------------ Testing update_pointers -------------Case 1: [ 10.20 , 7.30 , 5.60 , 9.80 Before: p1 = 10.20 , Using mode 'a': p1 = 5.60 , Using mode 'z': p1 = 11.20 ,

, 11.20 , 7.50 ] p2 = 7.50 p2 = 11.20 p2 = 5.60

Case 2: [ 10.20 , 7.30 , 5.60 , 9.80 Before: p1 = 10.20 , Using mode 'a': p1 = 5.60 , Using mode 'z': p1 = 10.20 ,

] p2 = 9.80 p2 = 10.20 p2 = 5.60

Case 3: [ 10.20 , 7.30 , 5.60 , 9.80 , 11.20 , 7.50 , 3.70 , 11.10 , 5.80 , 9.10 ] Before: p1 = 10.20 , p2 = 9.10

10

CP264 Spring 2021

Midterm

Using mode 'a': p1 = 3.70 , p2 = 11.20 Using mode 'z': p1 = 11.20 , p2 = 3.70 Case 4: [ 10.20 ] Before: p1 = 10.20 , p2 = 10.20 Using mode 'a': p1 = 10.20 , p2 = 10.20 Using mode 'z': p1 = 10.20 , p2 = 10.20 Case 5: Error cases: Error (update_pointers): Error (update_pointers): Error (update_pointers): Error (update_pointers): Error (update_pointers):

Null array invalid size invalid mode invalid pointer invalid pointer

----------- End of Testing update_pointers --------------

Best of Luck

11...


Similar Free PDFs