Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Flowchart - While loop

  1. #1
    spadez is offline Learning Programmer
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Flowchart - While loop

    Hi,

    This is a mock exam question im working on. I have to make the code for the flowchart attached. My first solution involved 2 if loops but from help I got on another forum I realised it wasnt correct to do it that way as it didnt loop.

    Below is the code that I currently have. Can someone please tell me if this is a correct solution for the attached flowchart?:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	int i=0;
    	char c=0;
    	char line[10]="Fantastic";
    
    while(i < 10) 
    	{
            	if (c == '\0')
            	{
    		               printf("\n");
      	}
    		else
    		{
    		              printf("%c",c);
    		              c=line[i++];
    	         }
                             printf("\n");
     
    return 0;
    }
    Attached Thumbnails Attached Thumbnails Flowchart - While loop-456.jpg  

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    brownhead's Avatar
    brownhead is offline Programmer
    Join Date
    Apr 2009
    Posts
    172
    Rep Power
    12

    Re: Flowchart - While loop

    I think you forgot to attach the flowchart.

  4. #3
    spadez is offline Learning Programmer
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Re: Flowchart - While loop

    Mmm its showing as attached for me, but just in case there are problems here is a imageshack link:

    ImageShack - Image Hosting :: 456iqq.jpg

  5. #4
    brownhead's Avatar
    brownhead is offline Programmer
    Join Date
    Apr 2009
    Posts
    172
    Rep Power
    12

    Re: Flowchart - While loop

    Nvm, I see it now. Dunno if you edited or I'm just blind. But you just made a typo, its correct.. Make sure to space your lines correctly though so you can catch these things easier.
    Code:
    int i;
    char c = 0;
    char line[10] = "Fantastic";
    while(i < 10)
    {
        if (c == '\0')
        {
            printf("\n");
        }
        else
        {
            printf("%c",c);
            c = line[i++]
        }
    } // You were missing this curly
    printf("\n");

  6. #5
    spadez is offline Learning Programmer
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Re: Flowchart - While loop

    Thanks brownhead. Im trying to learn proper spacing at the moment >.<

  7. #6
    brownhead's Avatar
    brownhead is offline Programmer
    Join Date
    Apr 2009
    Posts
    172
    Rep Power
    12

    Re: Flowchart - While loop

    What are you using to write your code? Many IDEs come with code formatters.

  8. #7
    spadez is offline Learning Programmer
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Re: Flowchart - While loop

    Sorry but ive just tried running this code. It seems to spam the "\n" character. I cant spot why this is doing this, is it meant to from the information in the flowchart?

    Edit: I tried writting this one from scratch in notepad.

  9. #8
    brownhead's Avatar
    brownhead is offline Programmer
    Join Date
    Apr 2009
    Posts
    172
    Rep Power
    12

    Re: Flowchart - While loop

    Sorry, that one was my mistake, forgot to initialize i.
    Code:
    int i = 0; // <<< Whoops
    char c = 0;
    char line[10] = "Fantastic";
    while(i < 10)
    {
        if (c == '\0')
        {
            printf("\n");
        }
        else
        {
            printf("%c",c);
            c = line[i++]
        }
    }
    printf("\n");

  10. #9
    spadez is offline Learning Programmer
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Re: Flowchart - While loop

    With this code it still spams the part of the code in red:

    Code:
    // cfghcfh.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    int main()
    {
    int i = 0; 
    char c = 0;
    char line[10] = "Fantastic";
    while(i < 10)
    {
        if (c == '\0')
        {
            printf("\n");
        }
        else
        {
            printf("%c",c);
            c = line[i++];
        }
    }
    printf("\n");
    	return 0;
    }
    I dont really understand why this is doing this. Do i need a break command or something?

  11. #10
    Sysop_fb is offline Programmer
    Join Date
    Apr 2009
    Location
    Missouri
    Posts
    150
    Blog Entries
    2
    Rep Power
    12

    Re: Flowchart - While loop

    Because 0 = 0x0 = NULL = '\0'

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help with a flowchart
    By moey187 in forum C and C++
    Replies: 1
    Last Post: 04-14-2011, 06:47 AM
  2. flowchart with loop needed.Help me please
    By deme in forum General Programming
    Replies: 1
    Last Post: 10-18-2010, 05:18 AM
  3. Flowchart help.
    By kevotinh in forum General Programming
    Replies: 0
    Last Post: 06-26-2010, 04:38 PM
  4. Flowchart problem
    By gabz456 in forum General Programming
    Replies: 4
    Last Post: 09-23-2009, 10:24 AM
  5. flowchart help
    By j0k3r5an in forum Python
    Replies: 3
    Last Post: 08-15-2009, 04:35 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts