why does my linux c program only like preincrement and not postincrement? is this a programmer error or a system dependent issue?
why does my linux c program only like preincrement and not postincrement? is this a programmer error or a system dependent issue?
"Hence to fight and conquer in all your battles is not supreme excellence;
supreme excellence consists in breaking the enemy's resistance without fighting."
Art of War Sun Tzu
I've used post and pre increament in Linux without any problems. Most likely its how you are using it.
i can only get post increment to work in loops.
outside of loops it has been forcing me to use the preincrement.
"Hence to fight and conquer in all your battles is not supreme excellence;
supreme excellence consists in breaking the enemy's resistance without fighting."
Art of War Sun Tzu
1) post code
2) post the actually errors and/or warnings you are getting
3) State what compiler (and version) and what distro of linux you are using.
i already have the program working with the preincrement.
It isn't causing compiler errors. It's more like when I run it switching the incrementation style, it just sits there and doesn't do a single thing. I'm assuming it might be logic error, but I do this in c++ and it work. i'm using slackware 9.1 and gcc 3.4.0
If you feel like helping me with another error I'm lost on how to fix. I'm trying to have the program print all even numbers from 2-50. in a format of 2=1+1. all even numbers must be a sum of two primes.
I have it working for every number but 2,4,6 and 10.
I hardcoded the answers, but this will not satisfy my teacher.
I just can not find why it will not work for those numbers.
I have traced this program 3 times and I still have no clue.
Last edited by xviddivxoggmp3; 03-18-2004 at 01:13 PM.
"Hence to fight and conquer in all your battles is not supreme excellence;
supreme excellence consists in breaking the enemy's resistance without fighting."
Art of War Sun Tzu
And is this in the incrememt part of the for loop?
Or are we talking about your use of increment in a more complex expression as inCode:for(r=0;r<primecountcp;r++) // And other loops like this one
If it's the latter then pre increment and post increment with a dereference mean something different. For post increment you need to use parentheses to force evaluation as you want it:Code:primecp[++*primecountptr]=w;
If it's the former then your compiler has issues. The two styles are equivalent.Code:primecp[(*primecountptr)++]=w;
My best code is written with the delete key.
both issues involve the pointer references.
that is good to know.
That fixed the number 10.
Now I'm having problems with the system recognizing the ones that would are 1+1=2 2+2=4
I can not get the program to notice 1+5=6 either.
This is making my brain melt.
thanks.
"Hence to fight and conquer in all your battles is not supreme excellence;
supreme excellence consists in breaking the enemy's resistance without fighting."
Art of War Sun Tzu
What? None of those are valid C statements. You'll have to clarify your meaning. (IE: As people have said over and over, provide a sample of how you're trying to use it.) "1+5" is not an lvalue, and if that's what you're trying to do, your compiler will be telling you as much.Originally posted by xviddivxoggmp3 Now I'm having problems with the system recognizing the ones that would are 1+1=2 2+2=4
I can not get the program to notice 1+5=6 either.
This is making my brain melt.
thanks. [/B]
In reference to the above, provide a short example of exactly what you're doing.
Quzah.
Hope is the first step on the road to disappointment.
Originally posted by quzah
What? None of those are valid C statements. You'll have to clarify your meaning. (IE: As people have said over and over, provide a sample of how you're trying to use it.) "1+5" is not an lvalue, and if that's what you're trying to do, your compiler will be telling you as much.
In reference to the above, provide a short example of exactly what you're doing.
Quzah.I think he realises that they are not C statements given the code he posted (project1c.c).Originally posted by xviddivxoggmp3
Attachment: project1c.c
This has been downloaded 4 time(s).
output only should look like the following.I have my loops testing for every occurance of the problem.
2=1+1
4=2+2
6=3+3
8=3+5
10=3+7
12=5+7
14=3+11
16=3+13
16=5+11
18=5+13
18=7+11
20=3+17
20=7+13
22=3+19
22=5+17
24=5+19
24=7+17
24=11+13
"Every even number must be represented as a sum of two primes"
I have written the code and debugged everthing except for 2,4, and 6.
My code below finds the primes and inputes it into the array.
my code below prints it.Code:void prime(int primecp[],int evencountcp,int *primecountptr) { int w,b;/*loop control variables*/ for(w=2;w<=evencountcp;w++)/*loop1 used to find the primes */ { for(b=2;b<=w;b++)/*loop2 used to find the primes*/ { if(w%b)/*modulation test for a possible prime*/ { if(b==w-1)/*final test for a possible prime*/ { primecp[(*primecountptr)++]=w;/*inserts prime into array*/ break;/*breaks loop2*/ } continue;/*continues loop2*/ } else { break;/*breaks loop2*/ } } } }I'm trying to have every print as a result of my loops and not due to the red outlined printf statements. I'm missing something and have been unable to pen point it.Code:void sum(int primecp[],int evencountcp,int primecountcp,int *countptr) { int r,h;/*loop variables for the sum test*/ if(primecountcp!=0)/*checks for an empty array*/ { for(r=0;r<primecountcp;r++)/**/ for(h=r+1;h<primecountcp;h++) { if(primecp[r]+primecp[h]==evencountcp) { if(*countptr==0) { printf("2=1+1\n"); printf("4=2+2\n"); printf("6=3+3\n"); } printf("%i=%i+%i\n",evencountcp,primecp[r],primecp[h]); (*countptr)++; if(!(*countptr%15)) { printf("press any key to see more"); getchar(); system("clear"); } } break; } else { continue; } } } }
Last edited by xviddivxoggmp3; 03-18-2004 at 03:17 PM.
"Hence to fight and conquer in all your battles is not supreme excellence;
supreme excellence consists in breaking the enemy's resistance without fighting."
Art of War Sun Tzu
1 isn't prime therefore 2 cannot be written as the sum of two primes.
Looks like 2 is not being added to your list of primes.
Last edited by major_blagger; 03-18-2004 at 03:31 PM.
isn't 1 devisable by 1 and itself(1)?
is that not the rule of prime?
"Hence to fight and conquer in all your battles is not supreme excellence;
supreme excellence consists in breaking the enemy's resistance without fighting."
Art of War Sun Tzu
AND greater than 1Originally posted by xviddivxoggmp3
isn't 1 devisable by 1 and itself(1)?
is that not the rule of prime?
major_blagger is right.
1 isn't prime. This is perhaps why your code doesn't tell you that 6=1+5.
Another definition of primeness is a number is prime only if it can't be divided by a prime smaller than itself. So if 1 was prime, then there would be no prime numbers. (except for 1 I suppose).