Like Tree2Likes
  • 1 Post By itCbitC
  • 1 Post By quzah

Converiting a flowchart into code

This is a discussion on Converiting a flowchart into code within the C Programming forums, part of the General Programming Boards category; I found this flowchart on the internet and was wondering what would be the most efficient way to program this ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    3

    Question Converiting a flowchart into code

    I found this flowchart on the internet and was wondering what would be the most efficient way to program this on C.

    I was thinking along the lines of either switch, do-while, or if-else functions but what's the best way and could someone give me a sample I can go by just to start me off? I'm also curious about how you're supposed to make the program loop at a certain point (i.e. The Begging part) until it gets a yes.

    Name:  435flowchartdog.jpg
Views: 6495
Size:  39.1 KB

    -Thanks in advanced

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by Defgrav View Post
    I found this flowchart on the internet and was wondering what would be the most efficient way to program this on C.
    For starters convert pictures into pseudocode, then code it in C.
    Quote Originally Posted by Defgrav View Post
    ...I'm also curious about how you're supposed to make the program loop at a certain point (i.e. The Begging part) until it gets a yes.
    In that case pickup a good book on C and read it cover-to-cover.
    There are plenty of good books suggested in the sticky on this site.
    Adak likes this.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Also let us not forget Cprogramming.com's very own C Tutorials.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,832
    Something like
    Code:
    int got;
    
    do {
        got = beg(); /* function returns non-zero if it has gotten what was begged for */
        } while (got == 0);
    Last edited by nonoob; 07-27-2011 at 01:05 PM.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    After the inital "Can I eat it?", I would break it up into a left hand side of the flowchart function, and a right hand side of the flow chart, function., just to limit the number of prompts and answers required in any one function.

    A clever way to loop back to a starting place in logic, is simply to put the code before the loop back, into a called function. When that function is done, it will automatically cause the program to "loop back", to the function that preceeded it.

    Mixing function calls with the if else statements, is one answer.

    You'll definitely want to start with pseudo code, imo.
    Last edited by Adak; 07-27-2011 at 01:16 PM.

  6. #6
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    Thanks to all who replied, I did go through a bit of the stickies and the C tutorials and I coincidentally did what Adak said exactly . I divided it into a YES and NO branch and encased it all in a do-while loop to allow repetition if i !=0 . I dunno if it'll work though, but from what I read the program reads it line per line so i think the order of the If-else's is right but then again I'm new to this so any constructive criticism is welcome and well appreciated !

    Here's my pseudo-code,
    P.S. To all nazi-coders out there please forgive me >.<

    Code:
    /* Include libraries and declarations of variables and other pre-processor directives */
          
          
    /*Process starts off from here*/      
          printf("Can I eat it?: ");
          scanf("%c", &answer);
    
    switch (answer)   /* a switch to seperate the inital YES and NO branches-*/
           {          /*- which will be cut short to accomodate the YES and NO branch connections */
           case '1':    /* will be the YES branch */
             printf("Eat it");  /* this part is separated because in the flowchart the NO branch connects with the YES branch and will loop AFTER the EAT IT statement */
             do {      /* something to make the program loop from the beggining given a certain condition*/
                printf("...Did it taste good?: ");
                scanf("%c", &answer);
    /* 1 */     if(char answer = yes ){
                    printf("\nAm I still hungry?: ");
                    scanf("%c", &answer1);
                    }
    /* 2 */         if(char answer1 = yes){
                            printf("\nIs there anymore?: ");
                            scanf("%c", &answer2);
                            }
    /* 3 */                 if(char answer2 = yes){
                                    printf("\nCan I get it without begging?: ");
                                    scanf("%c", &answer3);
    /* 4 */                         if(char answer3 = yes){
                                            i++;
                                            }
                            }
    /* 1 */                         else if(char answer = no){
                                         printf("\nStop eating...Take a nap.);
                                         exit;
                                 }
    /* 2 */                 else if(char answer1 = no){
                                  printf("\nYes I am!);
                                  i++;
                                  }
    /* 3 */          else if(char answer2 = no){
                          printf("\nTake a nap.");
                           exit;
                            }
    /* 4 */      else if(char answer3 = no){
    /* B */           do{
                         printf("\nBEG...Did he/she give it to me?: ");
                         scanf("%c", &answer4);
                      }
    /* 4.1 */         if(answer4 = yes){
                           i++;      /* allows the program to exit do-while loop B and enter into do-while loop A and since here i = 1 the condition of do-while loop A-*/
                           }         /*- will make it repeat from the beginning of do-while loop A which is what we want */
    /* 4.1 */         else if(char answer4 = no){
                            i--;    /* if this condition is fullfilled it will repeat do-while loop B until it a YES is given in which case it exits the loop.*/ 
                            }
    /* B */           }while(i != 1) /* this do while loop will only occur if the condition of the 4th else-if before it is fulfilled and will loop so long as i is not(!)-*/
                                     /*-equal to 1 ( i is always initially 0 )*/
           
           case '2'  /*Will serve as the NO branch*/
                printf("\nIs it safe to try eating?: ");
                scanf("%c", &answer);
    /* 5 */     if(char answer = yes){
                    printf("Eat it");   /* because we separated the first EAT IT statement because I have no idea how to make it skip a part rather I'll just resort to inserting which is easier*/
                    i++; /* This will evidently make the program loop back INTO the YES branch seeing as there is a connection */
                }
    /* 5 */     else if(char answer = no){
                     printf("\nShould I try eating it anyway?");
                     scanf("%c", &answer);
                     }
    /* 6 */          if(char answer = yes){
                             printf("\nEat it...Am I still okay?: ");
                             scanf("%c", &answer);
                             }
    /* 7 */                  if(char answer = yes){
                                     i++;
                             }
    /* 6 */                  else if(char answer = no){
                                  printf("Take a nap.");
                             }
    /* 7 */           else if(char answer = no){
                             printf("RUN TO KHOI.");
                      }
    }while (i != 0);   /* when i is not(!) equal to 0 the do while will keep repeating*/
    /* take note that from here i MUST be equal to 0 or else it would have repeated do-while loop A*/
          
          return 0;  /* they said it's always good to return to main, also a force of habit thanks to my professors */
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you only have 2 cases, you may as well just use an if-else. Switches are really meant for cases (hah) where you have many choices.


    Quzah.
    AndrewHunter likes this.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You've really done a lot of work - congrats!

    In C, two equal sign characters: == mean "equals", when you test for equality. One equal sign char is for assignment:

    Code:
    int i = 0; //assign i the value 0
    
    if(i == 2) //is i equal to two - the equality test
    Also, strings can't be compared directly in C. You can never code up:

    Code:
    if(word1 == word2) //no
    or in your case,

    Code:
    if(answer == yes) //nope!
    The C way to do this is:

    Code:
    if(strcmp(answer, "yes") == 0) //note the  two equal signs
    And you have to #include <string.h> (right next to the other include files is a good place.)

    I like the way you've numbered the logic out, in the program. I'd prefer you used more functions to make the code more readable and easier to write, but I know using functions is perhaps more difficult for you, just now.

    Gotta say - you have impressed me here. Code is rough, but shaping up. Well done, so far.

    P.S. char answer, will need to be
    char answer[4]; //big enough for 3 char's and the end of string char: '\0', that scanf() will add with %s
    Code:
    scanf("%s*c", answer);
    takes a string of char's, and scans but does not store, the newline. Also adds the end of string char to the answer string.
    In order to hold the "yes". char answer would hold only one letter.
    Last edited by Adak; 07-28-2011 at 05:38 AM.

  9. #9
    Registered User
    Join Date
    Jul 2011
    Posts
    3
    @quzah Oh I thought switches could be used for even just 2 cases, oh wait do you mean it naturally has 2 cases and that default thing it mentioned is the 3rd one ? Or is it really just impractical to use a switch for 2 cases only ?

    @Adak

    Thanks! It was a little sloppy since I started late and ended at 1 am haha, I even missed a label or two and I'm not even sure if all the brackets are balanced. But good to know it's a viable code .
    Oh and thanks a lot for the corrections on my condition part and my scanf's, I would never have figured those out if I actually tested O.O

    I like the way you've numbered the logic out, in the program. I'd prefer you used more functions to make the code more readable and easier to write, but I know using functions is perhaps more difficult for you, just now.
    May I ask what functions your referring to? Just out of curiosity

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    2,693
    @quzah Oh I thought switches could be used for even just 2 cases, oh wait do you mean it naturally has 2 cases and that default thing it mentioned is the 3rd one ? Or is it really just impractical to use a switch for 2 cases only ?
    This answer. Switch can be used for two cases, but as you mentioned, is impractical in practice. Switch has no "natural" limit, and default is used in the situation where none of the case criteria were met.
    Last edited by Matticus; 07-28-2011 at 11:46 AM.

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by Defgrav View Post
    @Adak
    May I ask what functions your referring to? Just out of curiosity
    The functions Adak is referring to are functions you create yourself:

    Quote Originally Posted by Adak
    A clever way to loop back to a starting place in logic, is simply to put the code before the loop back, into a called function. When that function is done, it will automatically cause the program to "loop back", to the function that preceeded it.
    A good starter for understanding that programming idea is CProgramming Tutorial Functions. Additionally, for more information on the subject you can read Functions and Program Structure. The last link is part of an entire lecture series by Steve Summit and is a tutorial for C in general.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is what I meant by using functions, in your code. This code has not been tested, btw.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void beg(void);
    void eatIt(int);
    char isSafeToTry(void);
    void stopEating(void);
    void takeNap(void);
    
    int main () {
      int no2yes=0, cry=0;
       char answer;
       printf("\nCan I Eat It? [y/n] ");
       scanf("%c", &answer); getchar();
       if(answer=='y') {
          no2yes=0;
          eatIt(no2yes);
       }else {
          answer = isSafeToTry();
    
          if(answer=='y')
             eatIt(no2yes);
          printf("\nShould I eat it anyway? [y/n] ");
          scanf("%c", &answer); getchar();
    
          if(answer=='n') 
             takeNap();
          printf("I'll try eating it!\n");
          printf("\nAm I still OK? [y/n] ");
          scanf("%c", &answer); getchar();
    
          if(answer=='y') {
             no2yes=1;
             eatIt(no2yes);
          }else {
             printf("\nRun crying to Mom!\n");
             cry=1;
          }
       }
    
       if(!cry)
          takeNap();
    	return 0;
    }
    void beg(void) {
       char begAnswer='n';
       while(begAnswer=='n') {
          printf("\nPlease can I have some?, I'm hungry! [y/n]");
          scanf("%c", &begAnswer); getchar();
       }
    }
    void eatIt(int no2yes) {
       char answer; 
       int begWorked = 0;
       if(no2yes==0)
          printf("\nEat It.\n");
       printf("\nDid it taste good [y/n]?");
       scanf("%c", &answer); getchar();
       if(answer=='y') {
          answer='n';
          while(answer!='y') {
             printf("\nAm I still hungry [y/n]?");
             scanf("%c", &answer); getchar();
             if(answer == 'y') printf("Yes I am!\n");     
          }
          if(answer=='y') {
             printf("\nIs there any more [y/n]?");
             scanf("%c", &answer); getchar();
             if(answer=='y') {
                printf("\nCan I get it without begging? [y/n]:");
                scanf("%c", &answer); getchar();
                while(answer=='n') {
                   beg();
                   printf("Did he/she give it to me? [y/n]: ");
                   scanf("%c", &answer); getchar();
                   begWorked = 1;
                }
                if(!begWorked) {
                  printf("Did he/she give it to me? [y/n]: ");
                  scanf("%c", &answer); getchar();
                  begWorked = 0;
                }
                eatIt(no2yes);
             }else 
                takeNap();
          }
       }else {
          stopEating();
          takeNap();
       
       }
    }
    char isSafeToTry(void) {
       char answer;
       printf("\nIs it safe to try eating it? [y/n] ");
       scanf("%c", &answer); getchar();
       return answer;
    }
    void stopEating(void) {
       printf("\nStop eating\n");
    }
    void takeNap(void) {
       printf("\nTime to take a nap, goodbye. \n");
       exit(0);
    }
    I'm not good at working from flowcharts. We used pseudo code in my class.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Matticus View Post
    This answer. Switch can be used for two cases, but as you mentioned, is impractical in practice. Switch has no "natural" limit, and default is used in the situation where none of the case criteria were met.
    There can be a a limit on the number of case statements for a switch implementation, but it will at a minimum support 1023.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with flowchart
    By moey187 in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2011, 05:25 AM
  2. flowchart with if else statements
    By nishar in forum Tech Board
    Replies: 2
    Last Post: 10-01-2010, 05:20 PM
  3. Flowchart Software?
    By Cell in forum Tech Board
    Replies: 2
    Last Post: 02-09-2009, 08:20 AM
  4. flowchart. Help!!!
    By chema124 in forum C Programming
    Replies: 3
    Last Post: 12-10-2002, 10:00 AM
  5. Flowchart
    By ob1cnobe in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2002, 01:03 PM

Tags for this Thread


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