convert pseudocode into C code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • haarsh
    New Member
    • Sep 2010
    • 2

    convert pseudocode into C code

    hi... can anyone tell me how to convert the below pseudocode segment to a C program

    Code:
    [B]while [/B]true
              [B]do repeat[/B] j--
                     [B]until[/B] A[j] <= x
                 [B]repeat[/B] i++
                     [B]until[/B] A[i] >= x
    this is from quicksort partition algo. also pls tell me how to write true and repeat until in C
    thnxx
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    It seems odd that you are asking us to write a piece of code for you when you have the algorithm.

    Your questions suggest you don't know C at all, so if learning why start with something so tricky?

    Comment

    • Nepomuk
      Recognized Expert Specialist
      • Aug 2007
      • 3111

      #3
      Hi!
      This code is very c-like, so you don't have to change much. What you'll be using is called "loops" and there's a tutorial here, that should point you in the right direction. One tip from me: You're going to need 2 of the 3 types of loops that you'll find in that article.

      Oh, and in C you don't have predefined values true and false, but you can either define them yourself by putting the following code in the head of your program:
      Code:
      #define TRUE   (1)
      #define FALSE  (0)
      or you can just use 1 as true and 0 as false or you can use
      Code:
      #include <stdbool.h>
      to define the datatype bool which can have the values true and false... Or I'm sure there are other ways to do it too.

      Greetings,
      Nepomuk

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Code:
        #define TRUE   (1) 
        #define FALSE  (0)
        You might want to #define TRUE as !FALSE. All values are TRUE that are not FALSE.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          You might want to remove the definition for TRUE. I suggest that you only work with the FALSE macro.
          Code:
          for(done=FALSE; !done; )
             done = doSomething();
          
          int doSomething(void) {
             return !FALSE;
             }
          Whatever value you may choose for your TRUE macro, there will be many values that differ from TRUE, but that are still logically true.

          However, if you're using C99 then you should include stdbool.h and use the facilities it provides.

          Comment

          • haarsh
            New Member
            • Sep 2010
            • 2

            #6
            thnxx guys...:D

            Comment

            Working...