Download Day_2_prob

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
Transcript
Exercises for Day 2
Write C++ programs for each of the exercises below. You should use only features
in the language which have been discussed in the notes through this day.
Shorter Exercises
1. Linked Lists (*)
Write a program that reads in a list of integers, stores them in a linked list. This program
is very similar to the first program in this chapter. That is, the first integer read will be at
the front of the list and the last integer read will be at the tail.
2. Testing A Famous Conjecture with Linked Lists
Read in one integer. Then create a linked list starting with this integer. Each subsequent
integer in the list is created by dividing the last integer by 2 if it was an even number, or
multiplying the last integer by 3 and adding 1, if it was an odd number. When the integer
1 is added to the linked list, you should stop.
For example: if you read in 5, the linked list should consist of the numbers: 5, 16, 8, 4, 2,
1, in that order from head to tail. Note: It is a famous unproved conjecture that this
program always terminates, and hence the linked list is always finite in length.
3. Traversing Linked Lists (*)
Create a linked list of floats as you did for integers in problem 1. Traverse this list,
adding 10% to each value in the list. Traverse the list again, printing the numbers out.
4. Traversing Linked Lists
Create a linked list of integers as in problem 1, but each integer must be between 0 and 9
inclusive. Traverse the list and determine which integer if any appears first three
different times.
For example: if the list was 2, 3, 3, 1, 4, 5, 7, 1, 2, 1, 4, 3, 7, then the first integer to
appear 3 different times is the integer 1.
5. Reversing a Linked List (*)
Create a linked list of characters as you did for integers in problem 1. Then traverse this
list and create a new linked list with the same characters but in reverse order. As you
traverse the old list, copy the data values from each node to a new node and add each new
node to the head (rather than the tail) of the new list. If “head” points to the head of the
new list and p points to the new node then the instructions to add the new node to the
head of the new list are:
p -> next = head;
head = p;
Note: Any linked list can be constructed from scratch using this simple method rather
than the slightly more complicated method of this chapter. The downside is that the first
value read ends up at the tail of the list and the last value read ends up at the head, rather
than the more natural ordering of the method used in this chapter. It should be no
surprise therefore, that the new method of this problem is used in Chapter 4 for
implementing stacks with linked lists.
6. More Practice with Linked Lists – Union (*)
Write a program that reads in two linked lists of characters and outputs a linked list
consisting of the union of the original linked lists. In this problem it doesn’t matter
which method is used to read in the linked lists.
7. More Practice with Linked Lists – Intersection
Write a program that reads in two linked lists of characters and outputs a linked list
consisting of the intersection of the original linked lists. In this problem it doesn’t matter
which method is used to read in the linked lists.
8. Arrays of Strings (*)
Write a program that reads in an array of strings, and a search string. The program
should output all the strings that contain the search string as a sub-string.
9. Dynamic Length Strings (*)
Write a program that reads in a string of dynamic length. The program should calculate
and print the sum of the ascii values of all the characters in the string.
Longer Exercises
1. Designing a Sorting Algorithm (*)
Let’s write a program MAXSORT to sort an array A of size strings. MAXSORT works
by finding the index of the maximum element in the array from A[0] through A[size-1]
and swapping it with the current last location. The current last location starts at size - 1
and size is decremented at each iteration until it equals 1.
Your program should read in the array of strings, sort them using MAXSORT, and print
out the results.
2. An Array of Arrays (*)
Write a program that reads in ten integer grades for each student in a class. The
maximum number of students is not known at compile time. Calculate and print a table
showing the grades of each student, the average of each student’s ten grades, and the
average of all the students’ for each of the ten grades. Note that 2-dimensional arrays are
simply an array of arrays, and the notation A[i, j] is just a convenient way to write A[i][j].
Grade 1
Grade 2
...
Grade 10
Average
Student 1:
Student 2:
...
____________________________________________________________________
Average:
Note on Dynamic Length Arrays:
This problem could also be done if both the number of students and the number of grades
per student were unknown at compile time (rather than fixed at 10). The next chapter has
a problem involving this idea of a dynamic array of dynamic arrays.