counters

I am trying to come up with a solution for finding the number of integers that i can input into an array. What i mean is i have an array with a set size, but i want to be able to enter as little or as many numbers without there being a set number to enter. Then im trying to figure out how to get that number of integers i put in and have it printed out. I'm pretty sure i have to use counters as for what im told, but how do i go about using them in this situation? The code i have so far is here. if anyone could help that would be great! Thanks.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66

#include <iostream>
using namespace std;

int main()
{
	
	const int max=10;//want to eliminate the set amount of 10 and use quantity of user input
	int number[max];
	int j;
	int qty; //was going to use qty as my variable for how many numbers i've entered
	int mode;
	int maxFrequency = -1;
        bool many = false;
    
    
	cout << "Enter 10 integers..." <<endl; 
	cout <<" "<<endl;

	for( int j = 0; j < max; j++ )
	{
		cout << "Value " << j+1 << ":        ";
		cin >> number[j] ;
	}
	for(int j=0; j<max; )
    {
        int cur = number[j];
        int localFrequency = 0;
        while(number[j] == cur) 
        {
           localFrequency++;
           j++;
        }
        
        if(localFrequency > maxFrequency)
        {
            maxFrequency = localFrequency;
            mode = cur;
            many = false;
        }
        else if(localFrequency == maxFrequency)
        {
            many = true;
        }
    }
    
    if( many )
        mode = -1;
    
    cout << "\n Mode = " << mode;



	
	cout << endl;
	cout << "There are "<< qty <<" integers." << endl; //qty equals zero right now
	
	cout << endl;





	return 0;
}
Unless you have been told not to do so, i would use vectors:

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

int main()
{
    std::vector<int> array(1);
    int i = 0;
    unsigned int num = 0;
    
    std::cout << "Enter an integer: ";
    std::cin >> num;
    std::cin.ignore();
    // do error checking
    
    array[i] = num;
    
    // ask if user wants to enter another digit, if yes:
    array.reszie(array.size()+1);
    // get more imputs, incrememnt i and assign num to the vector...
    
    // if no, output vector content and quit
    
    std::cin.get();
    return 0;
}
Last edited on
ok i see. Now would this still operate if i were to try and find the mode by using my array this way with vectors? I know i would have to switch things around a bit, but i was curious before i run into a headache trying to do something that might not be possible.
C++ vector == C array.

Anything that can be done with one can be done with the other.

EDIT: sorry, a better statement would be:

C++ : vector :: C : array

Last edited on
Alright, sounds good. I appreciate the help. Thanks guys.
Alright, sounds good. I appreciate the help. Thanks guys.
Topic archived. No new replies allowed.