Need help determining how many entries in an array

Hello everyone,

I am writing a program that reads a list of positive integers entered by the user into an array and determines how many entries of a certain number there are and displays this. The user can only enter up to 50 entries into the array or less by using the sentinel value (any negative entry).

The input/output should look like this :

10, 10, 5, 4, 3, 2, 1, 2, 3, 4, 5, 10, 3, -1

N Count

10 3
5 2
4 2
3 3
2 2
1 1

The problem I am having is that my output displays all inputs by the user under the N column. I only want it to show the certain number once and how many times it is counted, not the exact entry by the user. Besides that, everything else works. Here is my code so far:


#include <iostream>
using namespace std;

void fill_array(int a[], int& num_used); // function that builds array

int main ()
{
char ans;
do {
int sample_array[50], num_used, i, j;
int count = 0;

fill_array(sample_array, num_used); // function fill_array is called

cout << "N Count" << endl; //header line for output display

for(i = 0; i < num_used; i++)
{ count = 0;
for(j = 0; j < num_used; j++)
{ if(sample_array[i] == sample_array[j]) {
count++; }
}

cout << sample_array[i] << " " << count << endl;
}

// Allows user to run program again or not depending on response

cout << "Would you like to run the program again ?\n";
cout << "Enter 'Y' or 'y' if you would like to run again.\n";
cout << "Otherwise enter any key to terminate program.\n";
cin >> ans;
}
while (ans == 'Y' || ans == 'y'); //if not 'Y' or 'y' program terminates
return 0;
}

void fill_array(int a[], int& num_used) //function builds array by user input of positive integers
{
int num;
int count = 0;
cout << "Enter up to 50 positive integers.\n";
cout << "End list by inputtig any negative number.\n";
cin >> num;
while ((num >= 0)&&(count < 50)) //stops building array if condition not satisfied

{ a[count] = num;
count++;
cin >> num;
}
num_used = count;
}



To illustrate my problem, here is my output for a certain trial run :

N Count
1 2
2 2
3 2
4 2
5 2
6 2
7 2
8 2
9 2
1 2
2 2
3 2
4 2
5 2
6 2
7 2
8 2
9 2


I realize this must be a very basic question, but i appreciate all help in advance.
This is just a case of making a too complex code for a simple program. Try this:

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
42
43
44
45
46
47
48
49
#include <iostream>
#include <vector>

using namespace std;

void bubbleSort(vector<int> &numbervect)
{
	int outer,inner,temp;
	
	for(outer=0;outer<numbervect.size()-1;outer++)
		for(inner=0;inner<numbervect.size()-outer-1;inner++)
			if(numbervect[inner] > numbervect[inner+1])
			{	
				temp = numbervect[inner];
				numbervect[inner] = numbervect[inner+1];
				numbervect[inner+1] = temp;
			}
}

int main (int argc, char * const argv[]) {
	
	int countone=0, number, numbercounter, vectend;
	vector<int> numbervect;
	
	cout << "Enter up to 50 numbers or enter a negative number to stop.\n";
	do
	{
		numbervect.resize(numbervect.size()+1);
		cin >> number;
		cin.ignore();
		if(number>=0)
			numbervect[countone] = number;
		countone++;
	}
	while(countone<=49 && number>=0); //takes the inout and stores them in the vector
	
	cout << endl << "N Count\n";
	bubbleSort(numbervect); //sorts the vector
	countone=countone-1;
	vectend=numbervect[countone];
	for(countone=1;countone<vectend+1;countone++)
	{
		numbercounter = (int)count(numbervect.begin(), numbervect.end(), countone); //counts the amount of times that a number is in the vector and stores it as numbercounter
		if(numbercounter>0)
			cout << countone << " " << numbercounter << endl;
	}
	
    return 0;
}


Any questions, just ask.
I have only been coding for a few months now and haven't gotten to vectors or ever really seen them before. I have never seen a few elements of your code before, but im trying to work with it. Is there anything i can add to my current code to make it work although being more complicated than possible?
Let me try to modify your code.. hold on. Also, what have you not seen(give me line numbers so i can explain)
Is this for an assignment? If it is, what lesson is it for. If not, you should use vectors because they are much easier to handle with large quantities of numbers. If you need an explanation of vectors, I'll send you a sheet.
Yes it is for an assignment and was assigned to us after we finished learning about functions and starting a chapter on 1 and 2-D arrays which I understand fairly well (I believe). So, to answer your question, I would say it is a lesson on practicing with arrays.

The lines I had difficulty with are :

6 : vector <int>
10 : .size()
20 : char * const argv[]
28
30
43 : numbervect.begin() & numbervect.end()

Please send me the sheet on vector although I would not be allowed to use them for my assignment.

Ok I will.
And here are the explanations:

6vector <int> This declares the vector so it will store integers. The <int> can be replaced with any type of variable, such as <string>, <char>, etc.(you will learn more about these later)

10.size This is the size of the vector. For example, if vector "myVector" has 10 values within it, myVector.size() will return the size of the vecotr, in this case 10.

20int main (int argc, char * const argv[]) The same as saying int main () except this is what is standard in XCode(a C++ programmer for Mac)

28numbervect.resize(numbervect.size()+1); This resizes the vector "numbervect" to its original size plus one.

30cin.ignore(); Most programmers over look this necessary line of code. Whenever you use cin >>, you should put this after in order to eliminate a trace left behind by cin. See http://www.cplusplus.com/reference/iostream/istream/ignore/ for details

43numbervect.begin() & numbervect.end() This just returns the beginning and the end of the vector or, in other words, the first value and the last value. In this case, it is used to tell the count function where to start counting(numbervect.begin) and to stop counting(numbervect.end())
Last edited on
Thank you very much for the clarification and the sheet on vectors. Both are very helpful. I still cant quite figure out what to include in my original code to get the proper output.
No problem and honestly I have no idea about your original code. I have been trying to modify in all different ways, but I need to use some of the more advanced stuff I learned. Sorry
Its fine, I appreciate all the help you gave me. Thank you
Topic archived. No new replies allowed.