Hello luckylukebrooks,
As I read your directions:
Develop a program which will read numbers from the keyboard into an array of type ‘int’.
1. You may assume that there will be no more than 50 distinct (unique) entries in the array.
2. Your program allows any number of numbers to be entered, but only 50 may be unique.
3. The output is to be a two-column list. The first column is a list of the distinct
numberelements, the second column is the count of the number of occurrences of each
distinct number.
4. The list will be sorted on entries in the first column, largest to smallest.
5. The user will enter a series of numbers, terminated with an entry of -999.
6. At that point, the program should perform the sort of the columns and output the results.
|
Point 1 says you need an array of 50 elements. Point 3 gives me the impression that you will need a second array to keep track of how many times the number in the first array has been entered. Because your array may only hold 50 elements, but the user could enter 150 numbers or more and there is a good chance that 1 or more of the numbers in the first array would be repeated and you need to keep track of this.
I know there are better ways to store your numbers like a "map" or "List", but that is not the assignment.
Your code leads me to believe that you may want to so something like:
numbersArray[size]
, but this is not legal C++ code. The size of the array must be known at compile time meaning that what is between the [] would either be a constant variable of a number like 50.
Call the array variable "numbersArray", "numArray" or "arrayOfNumbers" anything but "array". It is a possibility that "array" could be confused with "std::array".
If you are going to use
using namespace std;
then you need to learn what is in the standard namespace so you know what names you can not use. It is easier to learn what is in the standard namespace a little at a time as you progress with your code.
This should give you some ideas to start with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
constexpr int MAXSIZE{ 50 };
int numbersArray[MAXSIZE]{}; // <--- ALWAYS initialize all your variables.
int numberOfOccurences[MAXSIZE]{};
//int size{}; // <--- Used to limit how much of the array you will use. Not needed.
int idx{};
int currentIdx{};
int inputNum{};
cout << "This program will sort an array and show the most unique numbers.\n";
//cout << "How many elements between 0 and 50 will your array be?\t"; // <--- Has no use in the program. The directions call for 50 not less than 50.
//cin >> size;
//int* array{ &size }; // <--- Again has no use.
//if (size > MAXSIZE)
//{
// cout << "Please input a number less than 50.";
//}
while (idx <= MAXSIZE)
{
cout << "Please input number " << idx + 1 << ": ";
cin >> inputNum;
idx++;
currentIdx++;
}
//cout << array; // <--- This will not print the whole array. You will need a for loop here.
return 0; // <--- Not required, but makes a good break point for testing.
}
| |
In line 29 the while loop will not work the way that you are thinking. It will loop 1 more time than you want because of the (<=). Remove the (=) since the "idx" variables start at (0)zero.
I am thinking that after you input a number you will need to check if "idx" is (0) and if true store the number otherwise you will need to check all of that has bee used, that is where the "currentIdx" comes in, to see if input matches anything stored in the array. I think should take care of the 50 unique numbers.
Do not forget that if the number does exist in the array you will need to add 1 to the correct index of the "numberOfOccurences" array.
The "cout" on line 38 will not print the entire array. It will either print the first elements' value or its address. Also you need to print from 2 different arrays.
Prefer to use the new line, (\n), over the function "endl". These days with updates and revisions from the newer standards the (\n) works much the same as the "endl". Also a "cout" followed by a "cin" the "cin" will flush the buffer before any input is allowed.
Be careful using (\t) in a string. It may not space the way that you want or it may be to much space. A prompt like:
cout << "Please input number: "
and the space after the (:) should be there. This will allow the "cin" to be entered on the same line. Which I feel looks better than having the input on the next line.
Andy