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.
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 vector
s:
int
s on the free store using new
.int
s to cout
.delete[]
).print_array10(ostream& os, int* a)
that prints out the
values of a (assumed to have ten elements) to os
.int
s on the free store; initialize it with the values
100, 101, 102, etc.; and print out its values.int
s on the free store; initialize it with the values
100, 101, 102, etc.; and print out its values.print_array(ostream& os, int* a, int n)
that prints out the
values of a (assumed to have n elements) to os .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:
int
, initialize it to 7, and assign its address to a variable p1
.p1
and of the int
it points to.int
s; initialize it to 1, 2, 4, 8, etc.; and assign its
address to a variable p2 .p2
and of the array it points to.int*
called p3
and initialize it with p2
.p1
to p2
.p3
to p2
.p1
and p2
and of what they point to.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:
int
array ga
of ten int
s initialized to 1, 2, 4, 8, 16
, etc.f()
taking an int
array argument and an int
argument
indicating the number of elements in the array.f()
:
a. Define a local int
array la
of ten int
s.
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.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:
vector<int> gv
; initialize it with ten int
s, 1, 2, 4, 8, 16,
etc.f()
taking a vector<int>
argument.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
.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.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.
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.
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.
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.