Returning array in C

I am writing a program that needs to return the number of values that are divisible by 3 and 7 and the sum of these values.Here is my code

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
#include<stdio.h>

#define SIZE 10

int* divisible(int*,int);

int main(){

    int a[100];
    int i;
    int *p;
    printf("Enter ten integers:\n");
    for(i=0;i<SIZE;i++)
        scanf("%d",&a[i]);

    p=divisible(a,SIZE);
    int sum=0;

    printf("Values divisible by 3 and 7 is:");
    for(i=0;i<SIZE;i++){
        printf("%d ",*(p+i));

        sum=sum + *(p+i);
    }
    printf("\nSum of these values is:%d",sum);

    return 0;
}

int* divisible(int a[],int size){
    int i;
    for(i=0;i<size;i++){
            if((a[i]%3)==0){
                if((a[i]%7)==0){

                        return a;
                }
            }
    }

}


problem is that this only returns the original array passed to the function divisible(). e.g. entered by the user 11,12,13,21,14,15,42,42,16,72 returns the same values.How can I make divisible() function return only values divisible by 3 and 7.Thanks
> Return array in C

Use a struct to wrap the array.

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
#include <stdio.h>

enum { MAX_SIZE = 100 } ;

typedef struct array array ;
struct array
{
    size_t actual_size ;
    int values[MAX_SIZE] ;
};

array divisible_by_3_and_or_7( const int* numbers, size_t n ) 
{
    array result ; 
    
    size_t cnt = 0 ;
    for( size_t i = 0 ; i < n ; ++i )
    {
        if( numbers[i]%3 == 0 || numbers[i]%7 == 0 )
        {
            if( cnt < MAX_SIZE ) 
            {
                result.values[cnt] = numbers[i] ;
                ++cnt ; 
            }
            else break ; // no more space
        }
    }
    result.actual_size = cnt ;
    
    return result ;
}

int main()
{
    int a[10] = { 11, 12, 13, 21, 14, 15, 42, 42, 16, 72 };
    
    array divisible = divisible_by_3_and_or_7( a, 10 ) ;
    for( size_t i = 0 ; i < divisible.actual_size ; ++i ) printf( "%d ", divisible.values[i] ) ;
    puts( "" ) ;
}

http://coliru.stacked-crooked.com/a/c95cedd294543229
Thanks for reply.I'm a newbie and not that familiar with structures but let me check it out.
Last edited on
Instead of returning an array from the function, we can fill an array that is passed to the function.
If you have not encountered struct till now, that is probably what is expected.

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
#include <stdio.h>

// assumes that result can hold up to 'n' integers
// return the actual number of integers in result
size_t divisible_by_3_and_or_7( const int* numbers, size_t n, int* result ) 
{
    size_t cnt = 0 ;

    for( size_t i = 0 ; i < n ; ++i )
    {
        if( numbers[i]%3 == 0 || numbers[i]%7 == 0 )
        {
                result[cnt] = numbers[i] ;
                ++cnt ; 
        }
    }

    return cnt ; // number of integers divisible by 3 0r 7
}

int main()
{
    enum { N = 10 };
    int a[N] = { 11, 12, 13, 21, 14, 15, 42, 42, 16, 72 };

    int divisible[N] ;
    size_t cnt = divisible_by_3_and_or_7( a, N, divisible ) ;
    
    for( size_t i = 0 ; i < cnt ; ++i ) printf( "%d ", divisible[i] ) ;
    puts( "" ) ;
}

http://coliru.stacked-crooked.com/a/f38ccf40ce8afd2b
Topic archived. No new replies allowed.