Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Memory Management – Homework Exercises

Similar presentations


Presentation on theme: "C++ Memory Management – Homework Exercises"— Presentation transcript:

1 C++ Memory Management – Homework Exercises
NOTE: tasks requiring string-matching (e.g. finding) should be case-sensitive Write C++ code for solving the tasks on the following slides Code should compile under the C++03 or the C++11 standard Please submit only the .cpp files for these exercises Each exercise should be a single, separate .cpp file, named with the exercise number followed by what you feel describes the exercise in a few words E.g. a good name for exercise 2 of this homework would be 2.longest-subsequence.cpp or 2.find-longest-subsequence.cpp, 2.subsequence.cpp is also acceptable Don’t worry about the name too much, just make sure the number and the file extension are correct

2 Exercises Write a program that reads two integer arrays from the console and compares them element by element. For better code reusability, you could do the comparison in a bool compArr(int arr1[], int length1, int arr2[], int length2) function, which returns true if they are equal and false if not Write a program that finds the longest sequence of equal elements in an integer array and then prints that sequence on the console (integers separated by space or newline) Write a void selectionSort(int a[], int start, int end) function that uses the selection sort algorithm to sort the elements from arr[start] to arr[end – 1] in increasing order (the elements outside the [start, end) range shouldn’t be sorted). This function modifies the array, so that the elements between start and end are sorted. Selection sort: in this case it would just find the smallest element between start and end, and place it at the start, then find the next smallest between the remaining (aka start + 1 and end) and place it at the next position (aka start + 1) and so on

3 Exercises Write a program that reads a line from the console containing a mathematical expression. Write a bool function that checks whether the brackets in the expression () are placed correctly (assume everything else is correct, i.e. you don’t need to check for correct signs, correct variables/numbers, etc.) and returns true if they are correct and false if they are not correct Examples of correctly placed brackets: ((a+b)/5-d); a+b; a+(b); ((a)) Examples of incorrectly placed brackets: ((a+b)/5-d; (a+b; a+b); (a))

4 Exercises Write a function void makeTitleCase(string& text) which changes each word in text to start with a capital letter (don’t bother with the exact title-case rules about not capitalizing things like in, from, etc.). Assume the first letter of a word is an English alphabetical symbol preceded by a non-alphabetical symbol (so in “we will--rock you”, “we”, “will”, “rock” and “you” are all considered words which need to be capitalized). Call the function from main() with a line of text read from the console and then print the modified line of text. Example input: On the south Carpathian mountains,a tree is swinging Expected output: On The South Carpathian Mountains,A Tree Is Swinging

5 Exercises Write a function int occurences(const string& text, const string& search) which returns the number of times search is contained in text. Example call: string text = “Write a function int occurences(const string& text, const string& search) which returns the number of times search is contained in text” string search = “on”; occurences(text, search); Expected result: 4 Make a program which reads two lines of text from the console – first the text, then the search string and prints the number of times search is contained in text

6 Exercises Write a program which is given a line of text, another line with a find string and another line with a replace string. Any part of text which matches the find string should be replaced with the replace string. Print the resulting text on the console. Hint: things like string.find(), string.insert(), string.replace() might be useful Example input: I am the night. Dark Night! No, not the knight night day Example output: I am the day. Dark Night! No, not the kday

7 Exercises Write a function int * parseNumbers(const string& str, int& resultLength) which returns a pointer to new-allocated array with the numbers parsed from str (assume you don’t need to handle wrongly-formatted input). str will contain integer numbers separated by spaces. The function writes the length of the allocated array in resultLength. Write a program which lets the user enter a number of lines of integers from the console, and prints their sum. Use the parseNumbers function in your program, but make sure you delete each array once you’re done with it. Example input (note: first line is the count of lines of numbers, in this case: 2 lines): Expected output (sum of and 4 5): 15

8 Exercises Write a program which does the same as task 8, but instead of printing to the console, asks the user for the name of an input file and an output file (each entered on a separate line) and reads the input from the input file and prints the output in the output file. If the output file already exists, it should be overwritten. Note: the input and output file could be the same. Note2: just copy-paste the code from 8 to reuse it Write a program which checks if the lines of two text files are the same (same number of lines and each line from the first file should be equal to the line of the second file). (not part of task 10) How can you assert the output from your other programs with the program from task 10? Hint: look up redirecting console output/input. Note: this is not a task you need to submit


Download ppt "C++ Memory Management – Homework Exercises"

Similar presentations


Ads by Google