Thread: Simple problem - my solution correct?

  1. #16
    Registered User
    Join Date
    Sep 2006
    Quote Originally Posted by spadez View Post
    Ok, here is my third stab at this then:

    #include <stdio.h>
    int main(void)
    	int i=0;
    	char c=0;
    	char line[10]="Fantastic";
    while(i < 10) 
            	if (c == '\0')
    return 0;
    Aside from the probably wrong indenting (im working on it) does this look better? I cant really see how to to complete the code without using one if condition.

    On understanding how to do this work, without an if statement:
    Let's give this job an analogy, and see if that helps.

    Let's say I gave you nine cards, and said "Your job is to turn over each card in the order I gave them to you, on the table".

    You wouldn't need an if statement in your thought processes. Because I gave you the cards (the letter for Fantastic), in the right order, and I told you there were 9 cards, (Fantastic has 9 letters), plus I told you where I wanted you to place them (print them out).

    If you have nine things to do (or a hundred things, no difference), you just have to do 9 (or a hundred) things - start counting!

    So no if statement is needed. It's alright to have an extra if statement, however. It is *not* alright to have a lot of unnecessary if statements in your code, however.

    The indentation looks **MUCH** better. The while word should be indented equal to the lines above it, but the whole code is 100% more readable, already.

    Now go play with this. What if I wanted every vowel of Fantastic, printed out, but not the consonants? What if I wanted Fantastic printed out backwards? What if you were asked to print out one letter from Fantastic, so each letter was on it's own line?


    What if you wanted to print out the word, in a column just three char's wide?

    Could you print out the word, picket fence style?

    F n a t c
     a t s i
    Now do it all over again, using a call to another function to actually do the printing, instead of having it done by main.

    These kinds of drills, and play and challenges, will all help build your programming "muscles".
    Last edited by Adak; 04-18-2009 at 11:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is this simple program correct? K&R 1-12
    By fsx in forum C Programming
    Replies: 14
    Last Post: 04-22-2009, 03:04 PM
  2. Replies: 2
    Last Post: 04-22-2009, 02:35 AM
  3. Problem compiling simple code examples
    By Wintersun in forum Windows Programming
    Replies: 8
    Last Post: 08-14-2007, 10:30 AM
  4. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM
  5. annoying problem, solution should be simple...
    By Captain Penguin in forum C++ Programming
    Replies: 0
    Last Post: 10-18-2002, 04:41 PM