Thread: post vs pre increment

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Question post vs pre increment

    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

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I've used post and pre increament in Linux without any problems. Most likely its how you are using it.

  3. #3
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    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

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    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.

  5. #5
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    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

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    And is this in the incrememt part of the for loop?
    Code:
    for(r=0;r<primecountcp;r++) // And other loops like this one
    Or are we talking about your use of increment in a more complex expression as in
    Code:
    primecp[++*primecountptr]=w;
    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.
    My best code is written with the delete key.

  7. #7
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    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

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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]
    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.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    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.
    Originally posted by xviddivxoggmp3
    Attachment: project1c.c
    This has been downloaded 4 time(s).
    I think he realises that they are not C statements given the code he posted (project1c.c).

  10. #10
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    output only should look like the following.

    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
    I have my loops testing for every occurance of the problem.
    "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.
    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*/
          }
        }
      }
    }
    my code below prints 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;
            }
        }
      }
    }
    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.
    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

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    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.

  12. #12
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    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

  13. #13
    Registered User
    Join Date
    Feb 2004
    Posts
    72
    Originally posted by xviddivxoggmp3
    isn't 1 devisable by 1 and itself(1)?
    is that not the rule of prime?
    AND greater than 1

  14. #14
    The C-er
    Join Date
    Mar 2004
    Posts
    192
    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).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help with HTTP POST
    By Anddos in forum Networking/Device Communication
    Replies: 5
    Last Post: 03-22-2009, 08:41 AM
  2. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  3. Post your Best Text Adventure
    By Joe100 in forum Game Programming
    Replies: 3
    Last Post: 08-15-2003, 05:47 PM
  4. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM
  5. Auto POST
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 06-07-2003, 10:42 AM