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!
Grading Table
Requirement |
Grading Comments |
Points |
Score |
C++ code includes comments, with project information at top, pre and post conditions for each functions and other cmments as needed. |
|
10 |
|
Easy to use user interface |
|
10 |
|
Functions: Logic divided up into cohesive functions with a single purpose |
|
20 |
|
Good variable and function names, appropriate use of constants rather then literal numbers. |
|
10 |
|
Runs: Run examples from trials with correct output |
|
40 |
|
The C++ code has good formatting, indentation, and organization. |
|
10 |
|
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
- Don't try to code this program until you work out an algorithm on paper, and can manually step through it on paper!
- It is MUCH easier to count the number of occurances if you sort the numbers first, then do the counting.
- 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.
- 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:
Trials
#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.
Solutions
Topic revision: r13 - 2019-11-05
- JimSkon