Jump to content

Flowchart - While loop

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
17 replies to this topic

#1
spadez

spadez

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
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?:


#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 Files

  • Attached File  456.JPG   17.4K   10958 downloads


#2
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
I think you forgot to attach the flowchart.

#3
spadez

spadez

    Learning Programmer

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

ImageShack - Image Hosting :: 456iqq.jpg

#4
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
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.
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");


#5
spadez

spadez

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
Thanks brownhead. Im trying to learn proper spacing at the moment >.<

#6
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
What are you using to write your code? Many IDEs come with code formatters.

#7
spadez

spadez

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
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.

#8
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
Sorry, that one was my mistake, forgot to initialize i.
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");


#9
spadez

spadez

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
With this code it still spams the part of the code in red:


// 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')

    {

        [COLOR="Red"]printf("\n");[/COLOR]

    }

    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?

#10
Sysop_fb

Sysop_fb

    Programmer

  • Members
  • PipPipPipPip
  • 160 posts
Because 0 = 0x0 = NULL = '\0'

#11
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
Not my day is it, alrighty
int i = 0;
char c = 0;
char line[10] = "Fantastic";
while(i < 10)
{
    c = line[i++];
    if (c == '\0')
    {
        printf("\n");
    }
    else
    {
        printf("%c",c);
    }
}
printf("\n");
return 0;


#12
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
You cannot print the '\n' character inside the loop. If you look, c=='\0' means you have to break out of the loop.
int i = 0;
char c = 0;
char line[10] = "Fantastic";
while(i < 10 && c!='\0')
{
    c = line[i++];
    printf("%c",c);
}
printf("\n");
return 0;
Note, when running the code nothing should be printed. (c is not reassigned to a non-zero value before testing it).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog