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; }
I think you forgot to attach the flowchart.
Mmm its showing as attached for me, but just in case there are problems here is a imageshack link:
ImageShack - Image Hosting :: 456iqq.jpg
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");
Thanks brownhead. Im trying to learn proper spacing at the moment >.<
What are you using to write your code? Many IDEs come with code formatters.
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.
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");
With this code it still spams the part of the code in red:
I dont really understand why this is doing this. Do i need a break command or something?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; }
Because 0 = 0x0 = NULL = '\0'
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks