Simple problem - my solution correct?

This is a discussion on Simple problem - my solution correct? within the C Programming forums, part of the General Programming Boards category; Hi, The attached image below is from my mock exam. This is the first code i have written from scratch ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    93

    Simple problem - my solution correct?

    Hi,

    The attached image below is from my mock exam. This is the first code i have written from scratch so there may be a simple error (but im hoping not)!

    Is this the correct way of solving this problem?

    Code:
    #include <stdio.h>
    
    int i=0, c=0;
    char line[10]="Fantastic";
    
    int main(void);
    {
    
    if (i<10)
    {
    	if (c=='\0');
    	{
    	printf("\n");
    	}
    	else
    	{
    	print("%c",c);
    	c=line[i++];
    	}
    }
    else
    {
    printf("\n");
    }
    
    return 0;
    }
    Attached Images Attached Images  

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Based on the flowchart it looks correct, though i c and line[] can be local variables.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,264
    You need a loop. See where your flowchart has the line from "c=line[i++]" to "i<10"? You need to implement that in your code.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by spadez View Post
    Hi,

    The attached image below is from my mock exam. This is the first code i have written from scratch so there may be a simple error (but im hoping not)!

    Is this the correct way of solving this problem?

    Code:
    #include <stdio.h>
    
    int i=0, c=0;
    char line[10]="Fantastic";
    
    int main(void);
    {
    
    if (i<10)
    {
    	if (c=='\0');
    	{
    	printf("\n");
    	}
    	else
    	{
    	print("%c",c);
    	c=line[i++];
    	}
    }
    else
    {
    printf("\n");
    }
    
    return 0;
    }
    Your code has 4 errors:

    1) Remove the semi-colon from int main(void); line
    2) Remove the semi-colon from if(c == '0') line.
    3) printf() is missing an f in "print("%c", c);

    4) Your program does nothing. The variable c == '0' on the first test, so the program exits, having done nothing.

    I would also suggest:

    1) That your variables be made local, inside main(), rather than global, as they are now. I and c are especially used by convention, as local counters or char's, so making them global is poor.

    2) You're testing if c is a char, but c is an int. I would like to see testing if line[c] is an end of string char, instead of c, itself.

    Your indentation style is not up to par. Two to four spaces per level of indentation, and code inside a function, should be indented. All code enclosed with it's own curling braces, should be indented, again.

    You indent one if statement, but not the second one. You indent the if, but not the else part of the very same if else block of code.

    Code:
    int main(void)
    {
       int i, c;
    
       //etc.
    
       if(line[c] != '0')
       {
          //etc.
          //etc.
       }
       else   //a single line of code, needs no curling braces.
             //etc.
       
       return 0
    }
    Getting proper and consistent indentation will help you and everyone else who reads your code, to understand it much faster and better.

    I'm glad to see you didn't comment it to death. Code comments which merely restate the obvious, are a nuisance:

    Code:
    int i;     //declare an integer variable
    The above example would be of no help.

    I suggest you code much more. Code "from scratch", and code by modifying other people's code, that you add something to, or subtract something from, but code. Code a great deal, because practice may not make your code perfect, but it will improve it greatly.

    Just for kicks, how many letter do you guess are in the word, "fantastic"?

    would a simple for loop or while loop, with a counter going from 0 to < 10, print out each letter?

    Better than nested if loops, surely.
    Last edited by Adak; 04-18-2009 at 03:35 PM.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Bah i guess I get an F then. So to work on this a little more, I now know that I need local variables (i dont understand why or how yet, but im researching that), I also have no idea how to indent properly but im also looking into that.

    In answer to Adak, the first three errors are silly, i need more practice I guess. Looking back on this code i think c should be defined as a char and not an int? Also i indented the second "if" because i was trying to make it a nested if. IS that not correct?

    To address the issue of the loop, i guess an if loop isnt suitable? Would i need a while loop so that i can join c=line[i++] back to i<10?

    For the record im doing a Product Design degree not a programming degree, and my in-class tuition is lacking, hence my back questions! Thank you all for the continued help.

    EDIT: The image attached came straight from my teachers mock exam so i cant help if its written incorrectly im afraid. In answer to your question since there are 9 letters and the '\0' character [9] would be more suitable?

    EDIT2: Im going to try and create a version2 of this with a countdown script.
    Last edited by spadez; 04-18-2009 at 03:57 PM.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Hi,

    Here is version two of my code, i have tried to incorportate a counter into the existing variables, im not sure if this code is better than the last, but im hoping so. Please excuse my indentation if it is not correct, i am still reading up on it:

    Code:
    int main(void)
    {
    	int i=0;
    	char c=0;
    	char line[10]="Fantastic";
    
    	for ( ; i < 10; i++ ) 
    
       	{
            	if (c == '\0')
            	{
    		printf("\n");
    		}
            	else
            	{
    		printf("%c",c);
    		c=line[i];
    		}
    	}
    	else
    	{
    	printf("\n");
    	}
    	return 0;
    }
    For the record am i even allowed to use an "else" with a "for"?
    Last edited by spadez; 04-18-2009 at 04:22 PM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If's are compare statements, they are not loops. For, do while, while, are examples of loops. It's very correct to indent nested if loops. but each part of each loop, needs to be indented also:

    Code:
    if( i < 10) 
    {
       //this part get's indented 3 spaces (2 to 4 is OK, though)
       if( c != '0' ) 
       {
          //this part is indented, *again*. Now we're at 6 spaces indentation
       }
       else
          printf("%c", line[i]); //etc.
    
       //now we're back to 3 spaces
    }  //now back to the same column that the opening brace for the larger if is on.
    Before long, your eyes will just automatically start looking for matching braces. Having them line up on the same column of the page when they match, is a big help.

    Given your job, here, I would use either a do while, a while, or a for loop, to get it done. No if statements needed. The tests that are needed, would be done right inside the loop, itself:

    Code:
    //seeing that i < 10 is part of the "given" info, and that fantastic has 9 letters, I might
    use a for loop, like this:
    
    for(i = 0; i < 10; i++)  
       printf"%c", line[i]);
    Usually, when I don't know how long the string will be, I'd use a while loop:

    Code:
    i = 0;  //this is called "priming the pump", and is usually needed for while & do while loops
    while( line[i] )  //will exit when the end of string char is reached
       printf("%c", line[i++]);
    Actually, you have all the char's you need in line[]. You don't need the variable c, at all.

    Just line[] and a counter, like i.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Thank you for the help Adak, im trying to understand and learn everything you have written.

    Im worried that perhaps i might be straying away from the solution the teacher is looking for however, since c is defined in the flowchart i feel that i am required to use it for my solution, especially when c=line[i++] is used in one of the flowchart segments.

    Is there a way of using my nested if code in my first code, but allow it to loop back to again check if i<10?

    ---

    Im sorry i hate to sway away from a better solution but i feel that the teacher is looking for a different solution, and hes the guy im trying to keep happy
    Last edited by spadez; 04-18-2009 at 04:55 PM.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I completely understand - use c.

    As for the nested if's - yes, you could use them, I suppose. A dog can run backwards too, I guess. Is that what you want, to build "piano's", instead of clear code?

    Ignoring the tests built into a loop, so you can do your own, more complicated tests separately, (which failed last time, you'll recall), is just a bad coding habit.

    "Rube Goldberg" code can be very funny, but never in working code.

  10. #10
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Hehe i understand. I dont want to learn to be a bad programmer, but at the same time I want to learn a solution that will accomodate the different aspects of the flowchart he has given me (i.e the code c=line[i++], c=0, c == '\0'). It seems like the code i design has to fit around these aspects rather than alter the way they work.

    Is there no solution that will tick both boxes.
    Last edited by spadez; 04-18-2009 at 05:01 PM.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    while(i < 10) {
       printstuff
       i++;
    }
    That doesn't do it for you, at all?

    Your flowchart shows a loop. If statements are not loops. Pick a loop.

  12. #12
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Ok, here is my third stab at this then:

    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;
    }
    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.

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    Ok

    CODE#include <stdio.h>

    int i=0, c=0;
    char line[10]="Fantastic";

    int main(void);
    {

    if (i<10)
    {
    if (c=='\0');
    {
    printf("\n");
    }
    else
    {
    print("%c",c);
    c=line[i++];
    }
    }
    else
    {
    printf("\n");
    }

    return 0;
    }
    [/CODE]

    The above is what you wrote.

    I am not that good a programmer myself. I am really just learning. The one thing I wouls start doing if I were you is listening to what people are saying here, no offense.

    For instance, you either ignored, or did not understand the indentation comments.

    You code should look like this, with JUST the indentation corrected (there are other errors, but let's handle each one at a time).

    Code:
    #include <stdio.h>
    
    int i=0, c=0;
    char line[10]="Fantastic";
    
    int main(void);
    {
    
            if (i<10)
            {
    	if (c=='\0');
    	{
    	              printf("\n");
    	}
    	else
    	{
    	              print("%c",c);
    	              c=line[i++];
    	}
            }
            else
            {
                    printf("\n");
            }
    
    return 0;
    }
    This solves JUST part of the indentation but again, let's go step by step. Notice how the following lines are offset now (compare that to yours):

    print("%c",c);
    c=line[i++];

    You MUST get used to this. This is a relatively simple program, but you will rapidly get yourself confused if you do not properly indent. It is very avoidable.

    Next error:

    Code:
    int main(void);
    Lose the ";".

    Finally, You can initialize the variables INSIDE your for loop. That is ok. Just keep in mind that the "scope" (the part of the program in which they can be "seen" or used) is just within that loop (e.g. between the curly braces). It is prefered to do it this way. Give those corrections a try!

    You will help yourself out a great deal by listening to the people on this board. I do not mean to be offensive, I am just telling you what I have seen as I follow this thread. You are getting good advice but not following it. You need to start by doing as you are asked. People will be more willing to help that way. If you dont understand why they tell you to do something, ASK. The best thing in the world is when someone that doesnt know how to do something learns it because of what you tell them.

  14. #14
    Registered User
    Join Date
    Feb 2009
    Posts
    93
    Hi Emodius,

    I understand what you are saying about the indentation, but im sure you will also understand that it takes time to learn new things. I am new to programming, and the time between getting told that my indentation is wrong, and understanding how to properly indent my code, is not 0. Ive printed off some documentation and im working my way through it so i dont make the same mistake again, but dont assume im ignoring the advice members give, im just trying to keep up.

    Anyway, moving on, your comments here relate to my first draft of the code, i have since been told these things by members of the forum, learnt from then and corrected them in my updated code:
    Next error:


    Code:
    int main(void);
    Lose the ";".

    Finally, You can initialize the variables INSIDE your for loop. That is ok. Just keep in mind that the "scope" (the part of the program in which they can be "seen" or used) is just within that loop (e.g. between the curly braces). It is prefered to do it this way. Give those corrections a try!
    Is that not slightly ironic?...

    I have come a long way in a short time thanks to the members on this board and the tutorials. If it comes across that i am not listening then i am sorry. I read and try to understand every comment that is posted, but this all takes time to learn and implement.
    Last edited by spadez; 04-18-2009 at 07:31 PM.

  15. #15
    Registered User
    Join Date
    Apr 2009
    Posts
    5

    I see

    My spologies if that is the case. I will review it. I am new to C as well. I am sorry if I got the wrong impression. I know it takes time to learn. Believe me, I know hehehe. Well I will check back here and will do all I can to help. Thank you for your patience.

Page 1 of 2 12 LastLast
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, 04:04 PM
  2. Replies: 2
    Last Post: 04-22-2009, 03:35 AM
  3. Problem compiling simple code examples
    By Wintersun in forum Windows Programming
    Replies: 8
    Last Post: 08-14-2007, 11: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, 05:41 PM

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