Number check

I want to check how many does each number appeared.

What's wrong here?

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
  #include <iostream>
#include <iomanip>
using namespace std;
void mode( int [], int );
int main (){
    const int arraySize = 100;
    int a[ arraySize ] = { 6, 7, 8, 9, 8, 7, 8,
                            7, 8, 9, 5, 9, 8, 7, 8,
                            6, 7, 8, 9, 3, 9, 8, 7,
                            7, 8, 9, 8, 9, 8, 9, 7,
                            6, 7, 8, 7, 8, 7, 9, 8,
                            7, 8, 9, 8, 9, 8, 9, 7,
                            5, 6, 7, 2, 5, 3, 9, 4,
                            7, 8, 9, 6, 8, 7, 8, 9,
                            7, 4, 4, 2, 5, 3, 8, 7,
                            4, 5, 6, 1, 6, 5, 7, 8, 
                            9, 8, 9, 7, 1, 1, 7, 1, 
                            9, 9, 2, 5, 3, 6, 4, 7,
                            1, 5, 6, 7, 9};
    
   mode (a, arraySize );
    
}
void mode(int a[], int asize ){
    int g = 0;
    int count[9] = { 0 };
    for (int f = 0; f < 9; ++f){
    for (int j = 0; j < asize; ++j){
    for (int i = 0; i < asize; ++i){
        if (a[j] == a[i]){
            g++;
            count[f] = g;
    }
    }
    }
    }
  
    for (int k = 0; k < 9; ++k){
        if ( k % 10 == 0){
            cout << "\n";
        }
    cout <<setw(7) << count[k];
}
}
What wrong should we expect to find? Compiler error, linker error, runtime error, Spanish Inquisition, unexpected result, or bad style?

Your triple nested loop makes no sense at all.

Can we assume that all the values are in range [1-9]?
You have a few issues. One is that you only set g to zero once, so it will continue to get bigger. You should set it to zero in a loop. Also, your first for loop is not doing 9 different numbers.
@keskiverto
I am a learner, You don't have to offense me ~Thanks.

@fabtasticwill
I got what you mean thank you.
It is not about offending, but about learning to supply as much details with the question as possible. In best case the act of writing of the question properly can give an idea, rendering the question unnecessary.

Think about this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <iomanip>
#include <algorithm>

int main (){
    int a[] { 6, 7, 8, 9, 8, 7, 8 };

    size_t count[10] { 0 };    
    for ( auto x : a ) {
        ++count[x];
    }
    for ( size_t k = 1; k < 10; ++k ) {
        std::cout << k << ": " << std::setw(7) << count[k] << '\n';
    }
    std::cout << std::accumulate( count+1, count+10, 0 ) << '\n';
    return 0;
}
Topic archived. No new replies allowed.