C get middle index from two arrays , variable initialization and ternary opperators

Viewed 56

I have this c problem in witch i try to get the middle indexes from two arrays and i don't want to assign the middle index variables manualy. i wanted to write a algorithm so if the array has an even number of elements or not the middle index is chosen automatically

#include <stdio.h>

int middle(int array1[], int array2[], int array_s1, int array_s2)
{
    int middle_index1, middle_index2;
    
    array_s1 % 2 == 0 ?  middle_index1 = array_s1 / 2 : (array_s1 / 2) - 1;
    array_s2 % 2 == 0 ?  middle_index2 = array_s2 / 2 : (array_s2 / 2) - 1; 
   
    
    printf("Elements in the original arrays:\n");
    
    for (int i = 0 ; i < array_s1 ; i ++)
    {
        printf("%i, ", array1[i]);
    }

    printf("\n");

    for (int i = 0 ; i < array_s2 ; i ++)
    {
        printf("%i, ", array2[i]);
    }

    printf("\nMiddle indexes = %i , %i", middle_index1, middle_index2);
    
    int new_array[] = {array1[middle_index1], array2[middle_index2]};
    int array_s3 = sizeof(new_array) / sizeof(new_array[0]);

    printf("\nElements in the new array:\n");

    for (int i = 0 ; i < array_s3 ; i++)
    {
        printf("%i, ", new_array[i]);
    }
}

int main(void)
{

    int array1[] = {10, 20, -30, -40, 30};
    int array2[] = {120, 20 , 30, 40, 30};
    int array_s1 =  sizeof(array1) / sizeof(array1[0]);
    int array_s2 = sizeof(array2) / sizeof(array2[0]);

    middle(array1, array2, array_s1, array_s2);
}

But those two lines are evaluated as 0

    array_s1 % 2 == 0 ?  middle_index1 = array_s1 / 2 : (array_s1 / 2) - 1;
    array_s2 % 2 == 0 ?  middle_index2 = array_s2 / 2 : (array_s2 / 2) - 1; 

Why am i doing wrong ? Can ternary operators be used inside variable initialization ?

code output:

Elements in the original arrays:
10, 20, -30, -40, 30, 
120, 20, 30, 40, 30, 
Middle indexes = 0 , 0
Elements in the new array:
10, 120, 
2 Answers

These statements with the conditional operator

array_s1 % 2 == 0 ?  middle_index1 = array_s1 / 2 : (array_s1 / 2) - 1;
array_s2 % 2 == 0 ?  middle_index2 = array_s2 / 2 : (array_s2 / 2) - 1; 

can be equivalently rewritten like

( array_s1 % 2 == 0 ) ? ( middle_index1 = array_s1 / 2 ) : ( (array_s1 / 2) - 1 );
( array_s2 % 2 == 0 ) ? ( middle_index2 = array_s2 / 2 ) : ( (array_s2 / 2) - 1 );

So if the expressions ( array_s1 % 2 == 0 ) and ( array_s2 % 2 == 0 ) evaluate to logical false then the statements do not have an effect. What you need is the following

middle_index1 = array_s1 % 2 == 0 ?  array_s1 / 2 : (array_s1 / 2) - 1;
middle_index2 = array_s2 % 2 == 0 ?  array_s2 / 2 : (array_s2 / 2) - 1; 

Pay attention to that it would be more logically clear just to write

middle_index1 = array_s1 / 2;
middle_index2 = array_s2 / 2; 

That is there is no great sense to subtract 1 when the number of elements in an array is odd.

For example if an array contains only 2 elements then the middle index will be equal to 1. On the other hand, if an array contains 3 elements then again the middle index will be equal to 1 instead of 0 as in your case.

The assignment must be in the beginning of the expression:

middle_index1 = array_s1 % 2 == 0 ?  array_s1 / 2 : (array_s1 / 2) - 1;
middle_index2 = array_s2 % 2 == 0 ?  array_s2 / 2 : (array_s2 / 2) - 1; 

It reads, if array_s1 % 2 == 0 then assign array_s1 / 2 to middle_index1 else assign (array_s1 / 2) - 1.

As for the expressions themselves, they will basically amount to the same value in the end, in all integer divisions the decimal part is truncated, instead of being rounded, as one could expect.

Related