Question about Project

So I'm having a bit of trouble with an assignment. Firstly, I'm trying to create a while loop that will contain a for loop that iterates for at most 50 times or until they input a sentinel. My mind is a bit mixed up with the iteration limit and the sentinel. any suggestions? Prompt and code below.

Here is my assignment:

Develop a program which will read numbers from the keyboard into an array of type ‘int’. You may assume that there will be no more than 50 distinct(unique)entries in the array. Your program allows any number of numbers to be entered, but only 50 may be unique.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. The list willbe sorted on entries in the first column, largest to smallest.The user will enter a series of numbers, terminated with an entry of -999. At that point, the program should perform the sort of the columns and output the results.


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
 
#include <iostream>
#include <iomanip>

using namespace std; 

int main()
{
	int size;
	//int array{ size };
	int i = 0;

	cout << "This program will sort an array and show the most unique numbers." << endl;
	cout << "How many elements between 0 and 50 will your array be?\t";

	cin >> size;
	int* array{ &size };

	if (size > 50)
	{
		cout << "Please input a number less than 50.";
	}

	while (i <= 50)
	{
		cout << "Please input next number.\t";
		cin >> i;
		i++;
	}

	cout << array;
}
Last edited on
You are attempting to use both 'i' to store the user input, and as a general iteration variable. Split this into two different variables; one for iteration, one to store what the user inputs.

You aren't creating an actual array in your code. You're just initializing a pointer to a single int (line 17). If you will not have more than 50 ints in your array, then you can just create a size-50 array like: int array[50];

Then, you can access elements array[0] through array[49].
Last edited on
thank you, I forgot that was an option. so now its time for my sentinel, but it wont work when i type it in when filling my array (-999)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
	const int SIZE = 50;
	int array[50];
	int i = 0;
	int count = 0;

	cout << "This program will sort an array and show the most unique numbers." << endl;

	while (array[i] != -999)
	{
		for (count = 0; count < SIZE; count++)
		{
			cout << "Please input next number.\t";
			cin >> array[count];

		}
	}
}
Line 10: array[i] does not yet have a valid value stored within it, so this should not be checked yet. Besides, -999 should never actually be stored into the array; it is just the sentinel value.

You also never update your value of 'i', so it is always 0.

Line 12 for loop: You are unconditionally forcing the user to enter 50 variables. What if, on the 20th iteration of the inner for loop, the user enters -999? It will still keep looping until 50 times.

You only need one loop, but you need extra checks within the loop, or as the loop's condition.
Every time the user enters input, you should check that this input is not the sentinel value before storing it in the array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
for (int i = 0; i < SIZE; i++)
{
    int number;
    cout << "Please input next number.\t";
    cin >> number;

    // here, check if it is the sentinel
    if (number == -999)
    {
        break;
    }
    else
    {
        // otherwise, save it in the array
        array[i] = number;
    }
}


This is better, but if we want to print the numbers the user entered, we need to keep track of how many numbers the user entered before entering the sentinel.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

int num_items = 0;
for (int i = 0; i < SIZE; i++)
{
    int number;
    cout << "Please input next number.\t";
    cin >> number;

    // here, check if it is the sentinel
    if (number == -999)
    {
        break;
    }
    else
    {
        // otherwise, save it in the array
        array[i] = number;
        num_items++;
    }
}


Then you can do whatever other processing needs to be done with the array:
1
2
3
4
for (int i = 0; i < num_items; i++)
{
    // use array[i] ...
}

Last edited on
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
Topic archived. No new replies allowed.