DM560, Introduction to Programming in C++

Sheet 9

Task 0

If you liked the graph drawing exercise carried out in class you may want to extend it to an application for drawing graphs that minimizes the number of crossing edges like the one demonstrated in class. You can make an animation or just draw the final result. The task is described in this document. It is a long reading but the amount of code to add is not that much. You can also limit yourself to read and discover what was behind the animation shown in class.

Task 1

Do the Review on page 623.

This drill has two parts. The first exercises/builds your understanding of free-store-allocated arrays and contrasts arrays with vectors:

  1. Allocate an array of ten ints on the free store using new.
  2. Print the values of the ten ints to cout.
  3. Deallocate the array (using delete[]).
  4. Write a function print_array10(ostream& os, int* a) that prints out the values of a (assumed to have ten elements) to os.
  5. Allocate an array of ten ints on the free store; initialize it with the values 100, 101, 102, etc.; and print out its values.
  6. Allocate an array of 11 ints on the free store; initialize it with the values 100, 101, 102, etc.; and print out its values.
  7. Write a function print_array(ostream& os, int* a, int n) that prints out the values of a (assumed to have n elements) to os .
  8. Allocate an array of 20 ints on the free store; initialize it with the values 100, 101, 102, etc.; and print out its values.
  9. Did you remember to delete the arrays? (If not, do it.)
  10. Do 5, 6, and 8 using a vector instead of an array and a print_vector() instead of print_array().

The second part focuses on pointers and their relation to arrays. Using print_array() from the last drill:

  1. Allocate an int, initialize it to 7, and assign its address to a variable p1.
  2. Print out the value of p1 and of the int it points to.
  3. Allocate an array of seven ints; initialize it to 1, 2, 4, 8, etc.; and assign its address to a variable p2 .
  4. Print out the value of p2 and of the array it points to.
  5. Declare an int* called p3 and initialize it with p2 .
  6. Assign p1 to p2.
  7. Assign p3 to p2.
  8. Print out the values of p1 and p2 and of what they point to.
  9. Deallocate all the memory you allocated from the free store.

Task 2

Do the Review on page 664.

In Chapter 18, we have two drills: one to exercise arrays and one to exercise vector s in roughly the same manner. Do both and compare the effort involved in each.

Array drill:

  1. Define a global int array ga of ten ints initialized to 1, 2, 4, 8, 16, etc.
  2. Define a function f() taking an int array argument and an int argument indicating the number of elements in the array.
  3. In f(): a. Define a local int array la of ten ints. b. Copy the values from ga into la. c. Print out the elements of la. d. Define a pointer p to int and initialize it with an array allocated on the free store with the same number of elements as the argument array. e. Copy the values from the argument array into the free-store array. f. Print out the elements of the free-store array. g. Deallocate the free-store array.
  4. In main(): a. Call f() with ga as its argument. b. Define an array aa with ten elements, and initialize it with the first ten factorial values (1, 2*1, 3*2*1, 4*3*2*1, etc.). c. Call f() with aa as its argument.

Standard library vector drill:

  1. Define a global vector<int> gv; initialize it with ten ints, 1, 2, 4, 8, 16, etc.
  2. Define a function f() taking a vector<int> argument.
  3. In f(): a. Define a local vector<int> lv with the same number of elements as the argument vector. b. Copy the values from gv into lv. c. Print out the elements of lv. d. Define a local vector<int> lv2; initialize it to be a copy of the argument vector. e. Print out the elements of lv2.
  4. In main(): a. Call f() with gv as its argument. b. Define a vector<int> vv, and initialize it with the first ten factorial values (1, 2*1, 3*2*1, 4*3*2*1, etc.). c. Call f() with vv as its argument.

Task 3

Design and implement a program that handles two types of polygons: rectangle and triangles. Both types of polygons are defined by a width and a height. The program must ask the user which polygon it should consider and for the input of the height and width. Then the program must return the calculated area.

Handle this design using polymorphism.

Task 4

Do Exercises 3, 6 and 9 from page 624.

  • Write a function, void to_lower(char* s), that replaces all uppercase characters in the C-style string s with their lowercase equivalents. For example, Hello, World! becomes hello, world!. Do not use any standard library functions. A C-style string is a zero-terminated array of characters, so if you find a char with the value 0 you are at the end.

  • Chapter 17 does not say what happens when you run out of memory using new. That’s called memory exhaustion. Find out what happens. You have two obvious alternatives: look for documentation, or write a program with an infinite loop that allocates but never deallocates. Try both. Approximately how much memory did you manage to allocate before failing? Handle the exception that is returned.

  • Which way does the stack grow: up (toward higher addresses) or down (to- ward lower addresses)? Which way does the free store initially grow (that is, before you use delete)? Write a program to determine the answers.

Task 5

Do Exercises 1, 2 and 3 from page 664.

  • Write a function, char* strdup(const char*), that copies a C-style string into memory it allocates on the free store. Do not use any standard library functions. Do not use subscripting; use the dereference operator * instead.

  • Write a function, char* findx(const char* s, const char* x), that finds the first occurrence of the C-style string x in s. Do not use any standard library functions. Do not use subscripting; use the dereference operator * instead.

  • Write a function, int strcmp(const char* s1, const char* s2), that compares C-style strings. Let it return a negative number if s1 is lexicographically before s2, zero if s1 equals s2, and a positive number if s1 is lexicographically after s2. Do not use any standard library functions. Do not use subscripting; use the dereference operator * instead.

Task 6

Construct a dynamically allocated array of 10 elements, initialize them to 0, iterate through them and print them. Do not use the indexing operator but rather use the ++ incrementer to move through elements. Make sure you understand the meaning of the following:

  • *p++
  • *++p

and determine whether and where you need parentheses to enforce the precedence of operators that you want to achieve.