# Lab 6 - Tallying

### Instructions

• Turn in the code (a cpp file or repl.it 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.
• You will need to do a sort for this problem. You may use the sort function given in class, or write your own!

Easy to use user interface   10
C++ code includes comments, with project information at top, pre and post conditions for each functions and other cmments as needed.   10
The C++ code has good formatting, indentation, and organization.   10
Good variable and function names, appropriate use of constants rather then literal numbers.   10
Functions: Logic divided up into cohesive functions with a single purpose   20
Runs: Run examples from trials with correct output   40
Total   100

### Problem

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 message, 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 that element. The list should be sorted on entries in the first column, smallest to largest.

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
Number  Count
-12     4
-1      1
1       4
2       2
3       3
4       2
```

### Hints

1. Don't try to code this program until you work out an algorithm on paper, and can manually step through it on paper!
2. It is MUCH easier to count the number of occurances if you sort the numbers first, then do the counting.
3. You could use additional arrays to store the unique numbers found, and the matching counts for each of those unique numbers. There is a more efficent method without using extra arrays.
4. You will need to break this up into functions. Do no attempt to do it all in a single program. Some likely functions:
• Enter Array
• Sort array of integers (given array)
• Display solution table using the sorted array
• Tally numbers in an array (only if you create intermediate arrays)

### Turn In

Runs of the following:

### #1

```Input the number of entries:40
Enter the numbers:3 5 4 5 7 6 8 2 3 2 3 1 2 1 1 1 5 4 5 6 4 6 4 6 4 2 5 4 6 2 7 6 7 4 8 5 7 8 9 2
```

### #2

```Input the number of entries:20
Enter the numbers:23 54 23 -12 54 34 54 -34 14 0 -17 54 -12 0 0 54 34 0 12 45
```

### #3

```Input the number of entries:50
Enter the numbers:1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 ```

### #4

Show how the program responds to bad input with too many entries (>50) selected, and zero numbers selected.

• N001_0690_002.jpg:

Topic attachments
I Attachment History Action Size Date Who Comment
jpg N001_0690_002.jpg r1 manage 1503.6 K 2016-09-30 - 18:41 JimSkon

This topic: SCMP118/Fall2016 > WebHome > LabKenyon
Topic revision: r13 - 2019-11-05 - JimSkon

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