ProgrammingAssignment1

advertisement
CSC 221
Data Structures
Fall 2009
Burg
Programming Assignment #1
In this assignment, you’ll implement a number of similar versions of a short program.
None of these are very hard. They’re just a good way to remind yourself of basic features
of programming and C++ that are reviewed in Chapter 1.
Generally, all your programs will have a function that prints out a list of elements in
reverse order. Call the function reverse_print. Here are the variations:
1. Use a statically-allocated array. The array is declared in main and passed to the
reverse_print function. You can assume that the elements are integers, and you know the
size of the array. Implement the function recursively.
2. Use a dynamically-allocated contiguous array. The array is declared in main and
passed to the reverse_print function. You can assume that the elements are integers.
You ask the user at run-time how many elements there will be, and the user inputs the
integer elements at run-time. Implement the function any way you want. It doesn’t have
to be recursive.
3. Create a template class called Reversible_List that allows the user to create a list
containing any type of data. The array to be reversed should be a data member called
my_array in the class. Write a constructor function for the class that takes an array as an
argument. This array becomes my_array. If this constructor function is used, you’ll have
to create an array before creating an instance of the class Revsersible_List. If an instance
of the class is created without an array as an argument, then the user is prompted to create
an array inside the constructor function. (This would be a constructor function with no
arguments). The Reversible_List class contains the reverse_print function. If you use
cout << to print elements of the list, it is assumed that << is defined for the type of data in
the list.
4. Use a dynamically-allocated linked list. The elements of the linked list are
dynamically allocated inside the function. You can assume that the elements are integers.
You ask the user at run-time how many elements there will be, and the user inputs the
integer elements at run-time. Implement the function any way you want. It doesn’t have
to be recursive.
5. Use a vector of integers (the vector of the stl). Create a vector containing 20 elements
that are created randomly (using rand()). You can implement the reverse_print function
any way you want to. It doesn’t have to be recursive. You can use any functions you
want to from the vector class. For extra credit, sort the integers first, and then print them
out in reverse order.
6. Use the string (the string of the stl). Create a string containing your first and last name.
You can implement the reverse_print function any way you want to. It doesn’t have to be
recursive. You can use any functions you want to from the string class.
Download