invalid operands to binary %

This is a discussion on invalid operands to binary % within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <math.h> #define MAXZO 100 void findMinValue(int n, int *zo, int *tail) { unsigned long ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    77

    invalid operands to binary %

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define MAXZO 100
    
    void findMinValue(int n, int *zo, int *tail)
    {
    	unsigned long i, j, d, zoValue;
    	for (i = 0; i < MAXZO; i++)
    		zo[i] = 0;
    	*tail = (int)log(n);
    	zo[*tail] = 1;
    	do {
    		zoValue = n;
    		i = 0;
    		while (zo[i] == 2) {
    			zo[i] = 0;
    			zo[++i]++;
    		}
    		if (*tail > MAXZO) exit(0);
    		if (zo[*tail+1] == 1) *tail++;
    		for (i = 0; i <= *tail; i++) {
    			if (zo[i] == 1) {
    				d = pow(10, (int)log(n)+1) % n;
    				for (j = i; j <= (int)i/log(n); j++)
    					zoValue = (zoValue*d) % n;
    			}
    			zoValue = (zoValue*pow(10, n-(int)i/log(n))) % n;
    		}
    		printf("%d\n", zoValue);
    		zo[0]++;
    	} while (zoValue != 0);
    }
    
    int main()
    {
    	FILE *fr, *fw;
    	int n, tail, i;
    	int zo[MAXZO];
    	fr = fopen("02.dat", "rt");
    	fw = fopen("02.out", "wt");
    	fscanf(fr, "%d", &n);
    	findMinValue(n, zo, &tail);
    	for (i = 0; i <= tail; i++) 
    		fprintf(fw, "%d", zo[i]);
    	fclose(fr);
    	fclose(fw);
    	return 0;
    }
    I just want to use module operator. But it didn't work.
    Code:
    mathsniper:~# gcc -O2 -o 02 02.c -lm
    02.c: In function ‘findMinValue’:
    02.c:24: error: invalid operands to binary %
    02.c:28: error: invalid operands to binary %

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The % operator does not work with doubles, which pow returns.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,206
    One example of your problem is this line;
    Code:
    d = pow(10, (int)log(n)+1) % n;
    as pow() returns a double. The % operator is only defined for integral types (int, long, unsigned, etc) and has no defined meaning for floating point types.

    To get a sort-of equivalent functionality for arguments of type double, look up the function fmod() which is declared in <math.h>

  4. #4
    Registered User
    Join Date
    Apr 2005
    Posts
    77
    The problem was solved. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with this tutorial program: invalid operands to binary( - )
    By convenientstore in forum C Programming
    Replies: 8
    Last Post: 06-08-2009, 12:53 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Need help with C program
    By ChrisH in forum C Programming
    Replies: 38
    Last Post: 11-13-2004, 12:11 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21