Tags: %TAGME{ tpaction="" web="KenyonCpp" tag="" }% view all tags

# Lab 5

### Instructions

• Turn in the code (a cpp file or ideone.com link), and the run outputs as requested below.
• Remember to format the code as described and the book and text, and to include comments including complete commetns at the beginning of the program.

Feature %
Code Format (Indenting, variable names) 10%
Turning in complete run of every function at least twice with difference values. 10%
Program correctness and completeness with respect to defination 70%

### Problems

1. Write a program that reads in a list of integers into an array with base type `int` . The program will require the user to input the numbers. You may assume that there are fewer than 50 entries in the array. If the user specifies more than 50, you should provide an error meaage, and ask them to reenter. The output is to be a two-column list. The first column is a list of the distinct array elements; the second column is the count of the number of occurrences of each element. The list should be sorted on entries in the first column, largest to smallest.

For example, for the following input

```Input the number of entries: 16
Enter the numbers: -12 3 -12 4 1 1 -12 1 -1 1 2 3 4 2 3 -12
```

the output should be

 N Count 4 2 3 3 2 2 1 4 -1 1 -12 4

Turn in: A run with a 10 numbers, and a run 30 numbera

2. Pattern Matching: Write a program that looks for a pattern of integers in an array of integers. The program first asks the user to input up to 100 numbers greater than or equal to zero. The user terminates the input with a negative number or sentinal. The program automatically moves to the next step after 100 numbers has been entered.

The program then enters a loop, and asks the users to enter a sequence of up to 10 numbers. As before, they enter numbers until either a negative number is entered, or they have entered 10 numbers. This array will be called the pattern.

The program then searches the larger array for a sub-sequence of numbers that matches the pattern. It will continue to search until it finds all instances of that pattern, counting the matches. (Note that for this exercise patterns cannot overlap, e.g. when you find a pattern, start searching for then next only AFTER the previous pattern.

The program will then report the number of matching, and then ask for another pattern to be entered.

If the user enters a negative number when as the first number in a pattern, the pattern is empty, and the program terminates.

If the user enters a pattern that is longer then the search array, give the user an error.

Example:

```Enter up to 100 integers:
1 4 3 6 8 5 12 5 6 8 5 2 9 3 1 4 3 6 6 8 5 -1
Enter a pattern: 2 -1
Matches:1  Pattern 2

Enter a pattern: 6 8 5 -1
Matches:3  Pattern 6 8 5

Enter a pattern: -1
Good bye!
```

</pre>

Turn in:

1. A search in array "3 6 4 7 8 4 6 2 4 10 4 8 6 5 4 6 4 6 8 6 5 6 4 2 7 9 6 5 6 5 6 5 5 1 2 1 3 6 4 7 8 6 8 6"

• Search for 3
• Search for 6 4 2
• Search for 6 6 4 7 8
• Search for 5 4 3 2 1
2. A search in array "1 2 3 4 5 6 7 8 9 10"

• Search for 8 9 10
• Search for 1 2 3 4 5 6 7 8 9 10
• Search for 1 2 3 4 5 6 7 8 9 10 11
Edit | Attach | Watch | Print version |  | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r4 - 2015-10-05 - JimSkon

 Home KenyonCpp Web View Edit Account
 Edit Attach
Copyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback