need a small halp

hi i need help with counting the occurrences of each integer element on my array. i am always getting a weird output.

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
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>

using namespace std;

int main()
{

    int values;
    cout << "How many input values [max: 30]?" << endl;
    cin >> values;

    cout << endl << "Enter " << values << " numbers." << endl ;

    // Array initialization
    int nums[ values ];

    for( int i = 0; i < values; i++ )
    {
        cin >> nums[ i ];
    }

    // Testing out the initialization of the array
    /*
    for( int i = 0; i < values; i++ )
    {

        cout << nums[i];

    }
    */

    int swap = nums[0];
    for( int i = 0; i < values; i++ )
    {
        if( nums[ i ] > swap )
        {
            swap = nums[ i ];
    	}
    }
    
    cout << endl << "Biggest Number: " << swap << endl;
    
    for( int i = 0; i < values ; i++)
    {
    	int temp ;
    	for( int i = 0; i < values - 1 ; i++)
    	{
    		if(nums[i + 1] < nums[i] )
    		{
    			temp 		= nums[i];
    			nums[i]  	= nums[i + 1];
    			nums[i + 1] = temp;
    		}
    	}		
	}
	
	cout << endl;

	
	
	int countarr[30];
	for(int i = 0; i < values; i++)
	{
		countarr[i] = 0;
	}
	
	for(int i = 0; i < values +1; i++)
	{
		countarr[nums[i]]++;
	}
	
	for(int i = 0; i < values ; i++)
	{
		cout << countarr[i];
	}
	
	
    return 0;
}
Somethings wrong with countarr[nums[i]]++ what are you trying to do here ?

Ex: nums[2] = 60, 55;
That code would try acess countarr[60] and countarr[55]
thanks that makes sense....
Topic archived. No new replies allowed.