简体   繁体   中英

Bigger number in array than the sum of the numbers to the right of it

I'm trying to create a program in C. I'm trying to find "special numbers" in a array. A special number is a number that is bigger than the sum of the numbers to the right of it.

I have this array

int input[] = {20,5,16,17,4,3,5,2,1};

Special numbers in this array are 17, 5, 2, 1... because 17 > 4+3+5+2; ...

I have function int special_numbers() . This function should find this special numbers and store them into array called result() and return it.

I have been trying for a very long time but I can't find a solution.

My code:

#include <stdio.h>
#include <math.h>

int special_numbers();

int main(){
    int input[] = {20,5,16,17,4,3,5,2,1};
    int result[9];
   

    for(int i = 0; i < count; i++){
        printf("%d ", result[i]);
    }

    printf("%d", special_numbers(input, 9));

    return 0;

}

int special_numbers(const int input[], const int array_size, int result[]){
    int result = 0;

    for (int i = 0; i < array_size; i++){
        if(input[i] > input[i+1]){
        
        }
    }

    return result;
}

There are more efficient ways, but the simple way is to use a nested loop that gets the sum of all the elements after i .

int special_numbers(const int input_array[], const int array_size, int result_array[]){
    int result = 0;

    for (int i = 0; i < array_size; i++){
        int sum = 0;
        for (int j = i+1; j < array_size; j++) {
            sum += input_array[j];
        }
        if (array[i] > sum) {
            result_array[result++] = array[i];
        }
    }

    return result;
}

You are calling the function twice

int count = special_numbers(input_array, 6, result_array);
//...
printf("%d", special_numbers(input_array, 6));

where the second call is redundant because you already have the variable count and is invalid because you forgot to specify the third argument.

Also it is a bad idea to use magic numbers as 6 .

Within the function you need to calculate the sum of elements that follow the current element. It is better to declare the variable that will store the sum as having the type long long int to avoid an overflow.

The function can be defined the following way as it is shown in the demonstrative program below.

#include <stdio.h>

size_t special_numbers( const int input_array[], size_t array_size, int result_array[] )
{
    size_t result = 0;

    long long int sum = 0;
    
    for ( size_t i = array_size; i != 0; --i )
    {
        if ( i == array_size || sum < input_array[i - 1] ) ++result;
        sum += input_array[i-1];
    }
    
    sum = 0;
    
    for ( size_t i = array_size, j = result; i != 0; --i )
    {
        if ( i == array_size || sum < input_array[i - 1] ) 
        {
            result_array[--j] = input_array[i-1];
        }
        
        sum += input_array[i-1];
    }
    
    return result;
}

int main( void ) 
{
    int input_array[] = { 16, 17, 4, 3, 5, 2 };
    int result_array[sizeof( input_array ) / sizeof( *input_array )];
    
    const size_t N = sizeof( input_array ) / sizeof( *input_array );
    
    size_t count = special_numbers( input_array, N, result_array );
    
    printf( "%zu: ", count );

    for ( size_t i = 0; i < count; i++ )
    {
        printf( "%d ", result_array[i] );
    }
    
    putchar( '\n' );
    
    return 0;
}

The program output is

3: 17 5 2

If the last element of the input array shall be greater than 0 then change the function the following way

size_t special_numbers( const int input_array[], size_t array_size, int result_array[] )
{
    size_t result = 0;

    long long int sum = 0;
    
    for ( size_t i = array_size; i != 0; --i )
    {
        if ( sum < input_array[i - 1] ) ++result;
        sum += input_array[i-1];
    }
    
    sum = 0;
    
    for ( size_t i = array_size, j = result; i != 0; --i )
    {
        if ( sum < input_array[i - 1] ) 
        {
            result_array[--j] = input_array[i-1];
        }
        
        sum += input_array[i-1];
    }
    
    return result;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM