Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logic array in C

Tags:

c

I am trying to test a logic function from four inputs a,b,c and d.

Each input is either 0 or 1.

I have been trying to accomplish this with arrays.

I want to pass back a 1 if a column in logicXY array matches that of the combLogic array.

int comb(int** combLogic, int** logicXY){
    int x, y, logicOut;
    for ( x = 0; x < 4; x++ ){
        if (logicXY[0][x] == combLogic[x]){
                logicOut = 1;
                }
    }
    return logicOut;
}


int main void(){
    int** combLogic[4] = {a,b,c,d};  /*logic input*/
    int** logic[4][4] = {{1,0,0,1}, {1,0,1,1}, {0,1,0,0}, {0,1}}; /*return 1 if any of these combinations match the logic input*/
    int comb(combLogic, logicXY); /*send to function*/
}

I know the function is not complete but I don't think I am passing the arrays correctly. I have read a number of tutorials but I cant seem to grasp the theory.

EDIT I have got a few steps forward but it still isn't working. This is what I have now.

Function declaration in .h

int comb(logicInput,logicTest);

Function in .c

/* Function - Combination */
int comb(int** logicInput, int** logicTest){
int x, y, logicOut;
    for ( x = 0; x < 4; x++ ){
        if (logicTest[0][x] == logicInput[x]){
                logicOut = 1;
                }
    }
    return logicOut;
}

The loop in part of the main.c

int output = 0;
int logicInput[4] = {0,1,1,1};
int logicTest[4][4] = {{1,0,0,1}, {1,0,1,1}, {0,1,0,0}, {0,1,1,1}};
int comb(logicInput,logicTest);
output = comb;

The code steps over the int comb(logicInput,LogicTest) and never carries out the function. If I take out int from the line then it carries out the function, returns the value but when the value is written to output it is nothing like the value that was returned from the function.

EDIT

I have made a few changes to the code so it does appear to work and with only one warning from the compiler for the function declaration in .h that I cannot seem to fix.

warning: parameter names (without types) in function declaration [enabled by default]

Function declaration in .h

int comb(logicInput,logicTest);

Function in .c

int comb(int** logicInput, int** logicTest){ /*Points to the arrarys in the main   program*/
int x, i, logicOut;
    for(i = 0; i < 4; i++){ /*test each column*/
     for ( x = 0; x < 4; x++ ){ /*test each row*/
      if (logicTest[i][x] == logicInput[i][x]){
            logicOut = 1;
            break;
            }
}
   if(logicOut == 1)break; /*Break when logicOut == 1 the first time it happens*/
}
return logicOut;
}

Loop in main.c

int output;
int logicInputC1[4] = {0,1,0,1};
int logicTestC1[4][4] = {{1,0,0,1}, {1,0,1,1}, {0,1,0,0}, {0,1,0,1}};
output = comb(logicInputC1,logicTestC1);

If I deviate from this code I just seem to end up with the compiler failing to build and even more warnings.

like image 279
StuntMonkeh Avatar asked Jul 16 '26 05:07

StuntMonkeh


2 Answers

int * comblogic[4] = {a, b, c, d} //where a, b, c, and d, are arrays of size 4; aka int     a[4];
int logicArray[4][4] = values; 

in your loop:

int comb(int** combLogic, int** logicXY){
    int x, y, logicOut;
    for(int i = 0; i < 4; i++){
     for ( x = 0; x < 4; x++ ){
      if (logicXY[i][x] == combLogic[i][x]){
            logicOut = 1;
            break;
            }
}
   if(logicOut == 1)break; //incase you want to break when logicOut == 1 the first time it happens
}
return logicOut;
}
like image 51
Magn3s1um Avatar answered Jul 18 '26 20:07

Magn3s1um


This is wrong:

int comb(int** logicInput, int** logicTest)

Try this:

int comb(int logicInput[4], int logicTest[4][4])

The difference is that int ** expects a pointer to a pointer (or an array of pointers), but when you define an array like you have then there are no pointers at all, so it doesn't work.

OK, technically, when you pass an array to a function it takes the address of that array and passes it by reference. This means you have a pointer to an array, but there are still no pointers inside that array.

When you pass an array you must always say what the dimensions of all but the first axes are, or else the compiler will not know how many entries to skip to get to the next row. In this case I gave the example as int [4][4], but it could just as well have been int [][4], should you have wished to give an unknown number of data rows.

The confusion arrises becuase C uses the same a[x] notation to access both int ** and int[n][m], but they do not look the same in memory.

like image 32
ams Avatar answered Jul 18 '26 19:07

ams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!