C: how to convert this code for an use with array?

77 Views Asked by At

Consider I'm still a newbie with C, so I made a lot of confusion. This code works fine

int error(int a, int b, int c)
{ 
  
  if (( a < 0 ) || (a > 255)) {
  printf("Number is over 255 or under 0, exit \n");
  exit(1); }

  if ((a < 0)|| (a > 255)) {
  printf("Number is over 255 or under 0, exit \n");
  exit(1);
  }
  else if ((b < 0)|| (b > 255)) {
  printf("Number is over 255 or under 0, exit \n");
  exit(1);
  } 
  else if ((c < 0)|| (c > 255)) {
  printf("Number is over 255 or under 0, exit \n");
  exit(1);
  } 
  else {
  true;
 }
}

But is too long and I don't like it, I want to create an array which take the values of a b and c, and compare it with 255 or 0 (separately: for example a=256 failed, b=23 ok, c=33 ok), and if is over 255 or under 0, exit. I have tried this code but I have failed

int array[] = {a, b, c};
  
        if( array[a] >= MAX_SIZE){
         printf("Number is over 255 or under 0, exit \n");
         exit(1);
     } 
3

There are 3 best solutions below

4
Vlad from Moscow On BEST ANSWER

For starters it is not a godd design when the program exits from the function.

Using an array your function can look the following way

bool error(int a, int b, int c)
{ 
    int data[] = { a, b, c };
    size_t n = sizeof( data ) / sizeof( *data );

    size_t i = 0;

    while ( i < n && !( data[i] < 0 || data[i] > 255 ) ) i++;

    if ( i != n )
    {   
        printf("Number is over 255 or under 0, exit \n");
    }

    return i == n;
}

Though as the function is named error then it should return true when the numbers do not satisfy the condition that is it is better to rewrite the return statement like

return i != n;

Also pay attention to that the function should not output any message. It is the caller of the function based on the return value will decide whether to output a message. So I would define the function the following way

bool error(int a, int b, int c)
{ 
    int data[] = { a, b, c };
    size_t n = sizeof( data ) / sizeof( *data );

    size_t i = 0;

    while ( i < n && !( data[i] < 0 || data[i] > 255 ) ) i++;

    return i != n;
}

and in the caller you can write

if ( error( a, b, c ) )
{
    printf("Number is over 255 or under 0, exit \n");
}

If you want to use an array of values then the function can look the following way

bool error( const int data[], size_t n, int low, int high )
{ 
    size_t i = 0;

    while ( i < n && !( data[i] < low || data[i] > high ) ) i++;

    return i != n;
}

The values of low and high can be for example 1 and 255.

2
Some programmer dude On

I would keep the logic basically as-is, but try to combine it into a single condition:

void error(int a, int b, int c)
{
    if (a < 0 || b < 0 || c < 0 ||
        a > 255 || b > 255 || c > 255)
    {
        printf("Invalid input, one of the numbers is outside the range 0-255\n");
        exit(EXIT_FAILURE);
    }
}

Note that I have changed the return-type to void, because if the check fails then the program exits. If the function returns then the values are valid.

On a different note, the name error doesn't really describe what the function does. Perhaps something like verify_values_range would be better?

0
Harith On

Simply iterate through the elements of the array with all the values:

#include <stdbool.h>

static inline bool is_in_range(int val, int start, int end) 
{
     return val >= start && val <= end;
} 

...
int array[] = {a, b, c};
  
for (size_t i = 0; i < sizeof array / sizeof *array; ++i) {
    if(!is_in_range(array[i], 0, 255)) {
        printf("Number is over 255 or under 0, exit \n");
        exit(1);
}