C++ Memory Management – Homework Exercises

Slides:



Advertisements
Similar presentations
Lex -- a Lexical Analyzer Generator (by M.E. Lesk and Eric. Schmidt) –Given tokens specified as regular expressions, Lex automatically generates a routine.
Advertisements

Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Tirgul 8 Universal Hashing Remarks on Programming Exercise 1 Solution to question 2 in theoretical homework 2.
Arrays Hanly - Chapter 7 Friedman-Koffman - Chapter 9.
1 CS 201 Passing Function as Parameter & Array Debzani Deb.
1 Lab Session-III CSIT-120 Spring 2001 Revising Previous session Data input and output While loop Exercise Limits and Bounds GOTO SLIDE 13 Lab session.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
Programming Arrays. Question Write a program that reads 3 numbers from the user and print them in ascending order. How many variables do we need to store.
1.7 Arrays academy.zariba.com 1. Lecture Content 1.Basic Operations with Arrays 2.Console Input & Output of Arrays 3.Iterating Over Arrays 4.List 5.Cloning.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
CS161 Topic #14 1 Today in CS161 Lecture #14 Practicing! Writing Programs to Practice Write a program that counts the number of vowels in a sentence, ended.
Chapter Seven Advanced Shell Programming. 2 Lesson A Developing a Fully Featured Program.
1 Software John Sum Institute of Technology Management National Chung Hsing University.
1 Project 5: Median. 2 The median of a collection of numbers is the member for which there are an equal number less than or equal and greater than or.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
CS1020E Sitin 1 Discussion -- Counting Palindromes.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Announcements 1st homework is due on July 16, next Wednesday, at 19:00 Submit to SUCourse About the homework: Add the following at the end of your code.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
COP3530 Data Structures600 Stack Stack is one the most useful ADTs. Like list, it is a collection of data items. Supports “LIFO” (Last In First Out) discipline.
Functions Why we use functions C library functions Creating our own functions.
Processing Sequences of Elements Svetlin Nakov Telerik Corporation
 Pearson Education, Inc. All rights reserved Introduction to Java Applications.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Looping and Counting Lecture 3 Hartmut Kaiser
Strings Mr. Smith AP Computer Science A. What are Strings? Name some of the characteristics of strings: A string is a sequence of characters, such as.
ITI 1120 Lab #3 Tracing and Branching Contributors: G. Arbez, M. Eid, D. Inkpen, A. Williams, D. Amyot.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
Variables and Expressions CMSC 201. Today we start Python! Two ways to use python: You can write a program, as a series of instructions in a file, and.
2. WRITING SIMPLE PROGRAMS Rocky K. C. Chang September 10, 2015 (Adapted from John Zelle’s slides)
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 Introduction An array is a collection of identical boxes.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Processing Sequences of Elements Technical Trainer Telerik Corporation Doncho Minkov.
Data Structures Arrays and Lists Part 2 More List Operations.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CS 330 Programming Languages 09 / 30 / 2008 Instructor: Michael Eckmann.
1 ENERGY 211 / CME 211 Lecture 3 September 26, 2008.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
Processing Sequences of Elements
Strings CSCI 112: Programming in C.
Lecture 19 Strings and Regular Expressions
Basic Elements of C++.
C++ Basic Syntax – Homework Exercises
The Selection Structure
ENEE150 Discussion 13 Section 0101 Adam Wang.
Engineering Innovation Center
Basic Elements of C++ Chapter 2.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Introduction to Python
Software John Sum Institute of Technology Management
Conditions and Ifs BIS1523 – Lecture 8.
© Akhilesh Bajaj, All rights reserved.
Encryption and Decryption
Algorithm Discovery and Design
Java Programming Function Introduction
IPC144 Introduction to Programming Using C Week 8 – Lesson 1
Search,Sort,Recursion.
Classes.
Chapter 9: Data Structures: Arrays
Suggested self-checks: Section 7.11 #1-11
Introduction to Computer Science
Exercise Arrays.
Unit 3: Variables in Java
Java Programming Function Introduction
An Introduction to STL.
Presentation transcript:

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

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

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))

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

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

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

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): 2 1 2 3 4 5 Expected output (sum of 1 2 3 and 4 5): 15

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