0

I apologize if this question seems too vague, but I am a complete beginner and I am stuck on how to count instances of numbers from an array.

I have a function called create_hist() which has three inputs: an input double array for which the numbers range from 0 to 16, an input integer which indicates how many elements are in the array, and an output integer array of size 17. The goal is to count how many of each numbers are in the first array, and then assign that number to the correct index of the output array i.e:

If the input array contains {0,1,1,2,3,4,4} then the count will be 7, and the output array should be {1,2,1,1,2,0,0,0,0,0,0,0,0,0,0,0,0}.

I am very new to C programming and I don't know where to start. My thoughts were that I could iterate through each element in the input array, and if the element was equal to zero, then add 1 to the first index of the output array and so on. I know this is wrong but I don't know another way. Could someone please guide me on how I could begin this? My first attempt is below.

int create_hist( double input_array[], int count, int hist[17] ) {
    for ( int i = 0; i < count; i++ ) { 
        if ( input_array[i] == 0; ) {
            // then add 1 to hist[0]
        }
        if ( input_array[i] == 1; ) {
            // then add 1 to hist[1]
            // etc.
    }
}   

2 Answers 2

1

You're not a million miles away from a good solution. If you already have count available to pass in, that's handy. Otherwise, you can replace it with:

sizeof(input_array)/sizeof(double)

If you know for sure the maximum value that can appear in input array, then the rest of the problem should be pretty easy.

Loop through each item in input_array, like you're already doing. Then increment the index of histwhich relates to the value of the current item, like so:

for(int i = 0; i<count; i++){
    hist[input_array[i]]++;
}

This should give the output you're looking for. For future reference, this is called a count occurrence algorithm.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, that makes sense. The only problem I am having is that my input_array is declared as a double, and I am getting "array subscript is not an integer" when I try to run it. Should I be trying to convert the double input_array to an int somehow?
That complicates things slightly - as we are using the values of input_array as indices for the hist array, we need them to be integers. If you are sure they will be integers, you can cast them, by putting (int) directly in front of a reference to the variable. You can read more about this by searching "c cast double to int". If you are not sure that they will be integers, you will have to think about using a different data structure for the output.
Yes it seems I have to truncate some of the array values to use them. I have tried ### for ( int i = 0; i < count; i++ ) { input_array[i] = int floor(input_array[i]); hist[input_array[i]]++; ### but this spits out plenty of errors
With the assignment you're using, you're trying to put an integer back into a double array. Try replacing the contents of the loop with: hist[(int)input_array[i]]++;
1

The function can look the following way

size_t create_hist( const double in[], size_t n, int out[] ) 
{
    const size_t N = 17;

    memset( out, 0, N * sizeof( *out ) );

    for ( size_t i = 0; i < n; i++ ) ++out[( size_t )in[i]];

    size_t m = 0;

    for ( size_t i = 0; i < N; i++ )
    {
        if ( out[i] ) ++m;
    }

    return m;
}

Here is a demonstrative program

#include <stdio.h>
#include <string.h>

#define SIZE    17

size_t create_hist( const double in[], size_t n, int out[] ) 
{
    const size_t N = SIZE;

    memset( out, 0, N * sizeof( *out ) );

    for ( size_t i = 0; i < n; i++ ) ++out[( size_t )in[i]];

    size_t m = 0;

    for ( size_t i = 0; i < N; i++ )
    {
        if ( out[i] ) ++m;
    }

    return m;
}

int main(void) 
{
    double a[] = { 0, 1, 1, 2, 3, 4, 4 };
    int b[SIZE];

    size_t n = create_hist( a, sizeof( a ) / sizeof( *a ), b );

    printf( "There are %zu unique elements\n", n );

    for ( size_t i = 0; i < SIZE; i++ ) printf( "%d ", b[i] );
    putchar( '\n' );

    return 0;
}

Its output is

There are 5 unique elements
1 2 1 1 2 0 0 0 0 0 0 0 0 0 0 0 0 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.