Home > database >  Write a C function GetEvenNumber
Write a C function GetEvenNumber

Time:04-19

For my studies, I have to write a C function GetEvenNumber:

  • parameters: array with n integers array size;
  • returns tr array which contains even integers from td.

I don't know a priori the length of the array tr.

My below code returns errors:

#include <stdio.h> // define the header file  

int *GetEvenNumber(int t[], int size)
{
    int tr[];
    int j = 0;

    for (int i = 0; i < size; i  )
    {
        if (t[i] % 2 == 0)
        {
            printf("%d is even \n", t[i]);
            tr[j] = t[i];
            j  ;
        }
    }
    return tr;
}

int main()   // define the main function  
{   
    int *t; // = {4, 3, 1, 8, 6 };
    int *tr = GetEvenNumber(t, 5);
    
    for (int i = 0; i < 5; i  )
        printf("%d \n", tr[i]);
}  

I get error:

error: array size missing in 'tr'
     int tr[];

warning: function returns address of local variable [-Wreturn-local-addr]
     return tr;

How do I fix that? Thanks.

CodePudding user response:

You mentioned that you could not use malloc() to dynamically create tr within GetEvenNumber() to address the two issues raised by your copmiler. This leaves making tr a global variable, or as here pass in the result array tr to be filled out:

#include <stdio.h>
#include <stdlib.h>

void GetEvenNumber(size_t size, const int *td, size_t *size2, int *tr) {
    *size2 = 0;
    for(size_t i=0; size; i  )
        if(td[i] % 2 == 0)
            tr[(*size2)  ] = td[i];
}

int main() {
    int td[] = {4, 3, 1, 8, 6 };
    size_t size = sizeof(td) / sizeof(*td);
    int tr[size];
    size_t size2;
    GetEvenNumber(size, td, &size2, tr);
    for (size_t i=0; i < size2; i  )
        printf("%d \n", tr[i]);
}

If the input array td contains uneven elements, then the result array tr have fewer valid elements than the input. I used size2 here to tell caller how many elements are valid in tr. Your code did not assign any values to, in this example, last 3 elements. You don't tell us what should happen with those last elements.

In modern C, if you specify the size before the array in the argument, then you can use the size in array specification which help document what is going on.

CodePudding user response:

The error is due to

int tr[];

because you have to specify the size of your array during its creation.

I suggest trying to add a function that returns the number of even numbers in the array:

int getEvenNum(int t[], int lent){
   int numEven = 0;                     // initialize counter to zero
   for(int i = 0; i < lent; i  ){     // for each element of the array
      if ((t[i] % 2) == 0){             // if it's even,
         numEven  ;                     // add 1 to counter
      }
   }
   return(numEven);                     // finaly returns the value of the counter
}

and then you replace the int tr[]; by int tr[getEvenNum(t, size)]; (maybe there's a ; after the getEvenNum(t, size) but I'm not sure)

CodePudding user response:

Since the array tr can have AT MOST the same number of elements as the original integer array, it would be safe to declare the array with the same size as the array 't[]'.

I have made some changes to your code. Try the following:

#include<stdio.h> // define the header file

void GetEvenNumber(int *t, int* tr, int size, int *pCountEven)
{


    int i, j=0;

    for (i=0; i < size; i  )
    {

        if(t[i]%2==0)
        {
            printf("%d is even \n", t[i]);
            tr[j  ] = t[i];

        }

    }
    *pCountEven = j;

}

int main()   // define the main function
{
    int t[] = {4, 3, 1, 8, 6 };
    int tr[5], countEven = 0, i;
    GetEvenNumber(t, tr, 5, &countEven);

    for (i=0; i < countEven; i  )
        printf("%d\n", tr[i]);

    return 0;
}

Edit: As @chqrlie (who is an experienced coder) pointed out, we can simply return the length of the array instead of taking the address of a variable. So alternatively, you can try this:

#include<stdio.h> // define the header file

int GetEvenNumber(int *t, int* tr, int size){
    int i, j=0;

    for (i=0; i < size; i  ){
            if(t[i]%2==0){
            printf("%d is even \n", t[i]);
            tr[j  ] = t[i];
            }
        }
    return j;
}

int main()   // define the main function
{
    int t[] = {4, 3, 1, 8, 6 };
    int tr[5], countEven = 0, i;
    countEven = GetEvenNumber(t, tr, 5);

    for (i=0; i < countEven; i  )
        printf("%d\n", tr[i]);

return 0;
}
  •  Tags:  
  • c
  • Related