Comparing Multiple Integers in C Workaround

23 Views Asked by At

So my goal is to create a function that takes 4 inputs ranging from 1-3 each with their own separate meaning, and prints the correct output given those 4 inputs. In order to do that, I need to compare the 4 integer inputs inside a single if statement.

Below is a snippet of my code:

#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "mp4.h"

void 
plan_swim_meet (int32_t pref_A, int32_t pref_B, int32_t pref_C, int32_t pref_D)
{
    // your code goes here

    /*
    Function Functionality: 
    Given 4 lane preferences of each swimmer, prints the minimum number of trials (ranging from 2-4) that satisfies each swimmer's lane preferences 

    Input meaning: 
    - 1 = Lane 1 is preferred
    - 2 = Lane 2 is preferred
    - 3 = No lanes are preferred (Can use lane 1 or lane 2)

    For example:
    - if pref_A, pref_B, pref_C, pref_D = 1 1 1 1 then the function will return A/B/C/D since they all have the same preferences 
    - if pref_A, pref_B, pref_C, pref_D = 1 2 1 2 then the function will return AB/CD since A and B have different preferences so they can be group into a single trial
    same thing with C and D

    Function I/O: 
    Input: pref_A, pref_B, pref_C, pref_D  all integers ranging from 1-3 
    Output: plan for the minimum number of trials that obeys all swimmers' lane preferences
    */

    if ((pref_A == pref_B == pref_C == pref_D) && ((pref_A < 3) || (pref_B < 3) || (pref_C < 3) || (pref_D < 3 ))) // if True, trials = 4
    {

        printf("%s/%s/%s/%s\n", "A", "B", "C", "D");
    }
    else if (
    (pref_A == pref_B == pref_C) || (pref_A == pref_B == pref_D) || (pref_A == pref_C == pref_D) || (pref_B == pref_C == pref_D)    
    ) // if True, trials = 3
    {

        const char * trial_1;
        const char * trial_2;
        const char * trial_3;

        trial_1 = (pref_A != pref_B) ? "AB" : ((pref_A != pref_C)? "AC" : "AD" ) ;
        trial_2 = (strcmp(trial_1, "AB") == 0) ? "C" : "B" ;
        trial_3 = ((strcmp(trial_1, "AB") == 0) || (strcmp(trial_1, "AC") == 0) ) ? "D" :"C" ; 

        printf("%s/%s/%s\n", trial_1, trial_2, trial_3);
    }
    else // Trials = 2
    {
        if ( ((pref_A == pref_B) == pref_C) == pref_D)
        {
            printf("%s/%s\n", "AB", "CD");
        }
        else
        {
            const char * trial_1 ;
            const char * trial_2 ;
            
            trial_1 = (pref_A != pref_B) ? "AB" : ((pref_A != pref_C) ? "AC" : "AD");
            trial_2 = (strcmp(trial_1,"AB") == 0) ? "CD" : ((strcmp(trial_1, "AC") == 0) ? "BD" : "BC") ;

            printf("%s/%s\n", trial_1, trial_2);
        }

    }
 
}

However, I realized that if I were to only use ''' if ( pref_A == pref_B == pref_C == pref_D ) ''' then there are some cases where the if statement doesn't work as intended (e.g. if pref_A = 2, pref_ B = 2, pref_C = 1, pref_D = 1)

so pref_A == pref_B would evaluate to 1, which will then be compared with pref_D and pref_C. but A, B, C, and D are not actually the same with each other.

What is the correct way of comparing multiple integers like this such that my first if statement work as I intend it to?

0

There are 0 best solutions below