简体   繁体   中英

Printf outputting a bunch of zeros

I'm new to C, so I apologize if the answer to this is painfully obvious! I mean to loop through two 2D arrays, passing correspondingly indexed members as arguments to my chineseRemainder routine, ie for each iteration, array1[i] and array2[i] should be passed to the routine, where i = i. I am expecting the output of a call to printf to be a certain set of numbers -- instead I am getting all zeros. Here is the main routine, where I call the CR function. **edit I gave xp and xq arbitrary int values, since they do not seem to be the problem, and giving them such values gives the same output.

int main(){

int xp, xq, p = 61, q = 3;
int i, j;

reverseInteger();

for(i = 0; i < 32; ++i){
    for(j = 0; j < 10; ++j){
        xq = 4;
        xp = 1;
        printf("%i\n", chineseRemainder(xq, xp, p, q));
            }
        }


return 0;

}

For troubleshooting's sake, I dumped the contents of xq and xp to make sure those assignments were going through: they are. The problem must be with the CR routine, because it is printing zero even when I pass any set of integers to it. So, here is that function, and its dependencies:

float power(int base, int exp) {

int i;
float result = 1;

if (exp == 0)
    result = 1;

else if (exp == 1)
    result = base;

else if(exp > 1){
    for (i = 0; i < exp; ++i)
        result *= base;
}

else
    result = 1/power(base, -exp);

return result;

}

float powerMod(int q, int e, int p){

float result;

result = (int)power(q, e) % p;

return result;

}

typedef struct arrayInside{

        int array[30][10];

    } arrayInside;              

arrayInside codesInside;            

struct arrayInside reverseInteger(){

int i, j, number;

for(i = 0; i < 30; ++i){
    j = 10;
    number = (aryConversion(q3[i], 3));
    do {
        codesInside.array[i][j-1] = number % 10;
        --j;
        number = number / 10;
    }
    while (number);
    codesInside.array[i][0] = 0;

};

return codesInside;
}

int chineseRemainder(int xq, int xp, int p, int q){

int tp;
int ceiling = (p*q-1)/2;

tp = ((int)(q * (powerMod(q, -1, p))*xp + p * powerMod(p, -1, q) * xq) % (p*q));
    if(tp > ceiling)
            tp-=p*q;

return tp;

}

Your chineseRemainder actually returns 0 every time. Look at this -

((int)(q * (powerMod(q, -1, p))*xp + p * powerMod(p, -1, q) * xq) % (p*q));

powerMod(q, -1, p) is zero. So multiplying and adding it also going to give zero. Your function actually returns zero. There is nothing wrong. You probably need to check the logic or change the data types.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM