Lab 6

Due: Oct 21, 11:55pm

Instructions

  • Do the problem below that you did not do on Lab 5
  • 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.

Grading

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

Problem 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

Hint

It is MUCH easier to count the number of occurances if you sort the numbers first, then do the counting.

Turn In

A run with a 10 numbers, and a run 30 numbera


Problem 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
Topic revision: r2 - 2015-10-17 - JimSkon
 
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback