Online C course exercise question

Discussion in 'Mac Programming' started by jruizisenberg, Feb 10, 2008.

  1. macrumors newbie

    Joined:
    Feb 10, 2008
    #1
    Hi, i have an objective to accomplish and is giving me a hard time, the objective is:


    Write a program that has the user input a height and width. Then it draws a rectangle made of #'s. (Limit the inputs to 40 or so as to not get too big.)
    For example:

    Enter a height: 3
    Enter a width: 5

    #####
    #####
    #####
    For a challenge, see if you can make a hollow rectangle:

    #####
    # #
    #####

    the closest i think i have been to it is:

    /*lesson10objective1*/
    #include <stdio.h>

    int main(){
    int row,row1,col,col1='#';

    printf("Please enter number of rows followed by a space and number of colums:\n");
    scanf("%i %i",&row1,&col1);

    for (row=1;row<="%i";row++)

    for (col=1;col<="%i";col++)

    return 1;

    }


    can someone help me out with this please....
     
  2. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #2
    You needed the formatting string ("%i") when to used scanf(), but you don't need it in your FOR loop. In the FOR loop, just reference the variables that you scanf()'ed into.

    Todd
     
  3. thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #3
    i tried:

    /*lesson10objective1*/
    #include <stdio.h>

    int main(){
    int row,col;

    row='#';
    col='#';

    printf("Please enter number of rows followed by a space and number of colums:\n");
    scanf("%i %i",&row,&col);

    for (row=1;row<=row;row++)

    for (col=1;col<=col;col++)

    return 1;

    }


    still no output... HELP!!! it compiled without errors but no output, no #'s quare...
     
  4. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
  5. thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #5

    no go... :(
     
  6. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #6
    Sorry, my bad.

    In your first program, you had row1 and col1 as the vars you were scanf()ing into. I didn't realize you messed that part up in your second post.

    Go back to your first example, using the FOR loop I just suggested.

    Todd
     
  7. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #7
    And this is not correct:

    col1='#';

    col1 is an integer.
     
  8. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #8
    And... no how, no way will this work:

    Code:
    for (row=1;row<="%i";row++)
    
    for (col=1;col<="%i";col++)	
    
    return 1;
    
    You started two FOR loops, but never finished either one of them. Read up on FOR loop syntax.

    If you post again, mention where you are, like "it won't compile, these are the errors...." or, "it runs, but I get incorrect output, here's why...."

    Todd
     
  9. thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #9
    tried:

    /*lesson10objective1*/
    #include <stdio.h>

    int main(){
    int row,col,row1,col1;

    row='#';
    col='#';
    row1='#';
    col1='#';

    printf("Please enter number of rows followed by a space and number of colums:\n");
    scanf("%i %i",&row1,&col1);

    for (row=1;row<=row1;row++)

    for (col=1;col<=col1;col++)
    break;
    return 1;

    }


    still no go... it compiles without errors but its not printing thee result, im not getting the square constructed with #'s, it asks for the input, i type it n execute but no output on screen, if i cant do the :

    row='#';
    col='#';
    row1='#';
    col1='#';

    how do i get the #'s for the output?
     
  10. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #10
    And what did you learn about FOR loop syntax?
     
  11. thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #11
    what do you mean?
     
  12. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #12
    Stop defining your ints to '#' it isn't an integer.
     
  13. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #13
  14. thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #14
    ok using this code:


    /*lesson10objective1*/
    #include <stdio.h>

    int main(){
    int row1,col1,row,col;

    printf("Please enter number of rows followed by a space and number of columns:\n");
    scanf("%i %i",&row1,&col1);

    for (row=0;row<=row1;row++)
    printf("#");
    {
    for (col=0;col<=col1;col++)
    printf("#\n");

    }
    return 1;

    }


    i got it to print but lets say i used 6 for rows and 6 for columns it prints 7 and 7 but instead of printing the square

    #######
    #
    #
    #
    #
    #
    #

    instead of

    #######
    #######
    #######
    #######
    #######
    #######
    #######
     
  15. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #15
    Very good!

    Ok, so, now, you need to move the newline character out of the inner loop. After you print each row of cols, THEN print the newline.

    Todd

    EDIT - well, actually, not very good. But definitely BETTER.

    Now, fix your indentation to see where else you might not have your logic correct. For clarity, and learning, make sure each FOR loop uses curly braces to surround it's logic.
     
  16. jsw
    Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #16
    The syntax would be:

    for ()
    {
    ...do something...
    }

    so, for you, it'd be

    Code:
    for ()
    {
      ...do something...
    
      ...maybe another loop here, perhaps?...
    }
    
     
  17. thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #17
    can you give me an example of what you mean?
     
  18. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #18
    See jsw's post above.

    You code, properly indented, looks like this:
    Code:
    /*lesson10objective1*/
    #include <stdio.h>
    
    int main() 
    	{
    	int row1,col1,row,col;
    
    	printf("Please enter number of rows followed by a space and number of columns:\n");
    	scanf("%i %i",&row1,&col1);
    
    	for (row=0;row<=row1;row++)
    		printf("#");
    	
    	{
    	for (col=0;col<=col1;col++)
    		printf("#\n");
    
    	} 
    	return 1;
    }
    There are two ways to write a FOR loop. jsw shows what I'm suggesting you use.

    The other form, that you are using, is

    Code:
    for (initializer ; conditional_test ; increment ) single_statement [b];[/b]
    
    However, you don't want a single statement inside the FOR loop, therefore use jsw's example with the curly braces.
     
  19. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #19
    P.S. Please use code tags to wrap your code, like this:


    [code]
    #include ....
    [/code]


    and it will look like this:
    Code:
    #include ....
    
     
  20. jsw
    Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #20
    jruizisenberg, you're going to need to try a bit more without expecting us to write it out word for word. :)

    You have all that you need. Just keep in mind the format of the for loop, and be sure that you put all you need inside the curly brackets for each loop, then step through the logic in your head.

    Absolutely the second form is often used in coding.

    However, for beginners, I always recommend the first form, as it's a lot harder to spot errors if you leave out the bracket but include indents, like
    Code:
    for (x = 0; x < 10; x++)
      printf("a");
      printf("b");
    
    which prints "aaaaaaaaaab", not "abababababababababab" as many beginners might think.
     
  21. thread starter macrumors newbie

    Joined:
    Feb 10, 2008
    #21

    got it working perfectly with:

    /*lesson10objective1*/
    #include <stdio.h>

    int main(){
    int row1,col1,row,col;

    printf("Please enter number of rows followed by a space and number of colums:\n");
    scanf("%i %i",&row1,&col1);

    for (row=0;row<row1;row++)
    {
    printf("#");
    {
    for (col=1;col<col1;col++)
    printf("#");
    printf("\n");
    }
    }


    return 1;

    }
    now how do i limit to a maximum input of 40?
     
  22. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #22
    Would probably be easiest to use the do-while loop. Read up on it if you haven't already. Maybe something like...

    Code:
    do
    {
        ... read input into variable X ...
    } while (X <= 40);
     
  23. macrumors 6502a

    Joined:
    Dec 4, 2006
    Location:
    Katy, Texas
    #23
    It's not working properly. It might appear to be working properly if you enter 2 rows and 2 cols, but that's about it.
     
  24. jsw
    Moderator emeritus

    jsw

    Joined:
    Mar 16, 2004
    Location:
    Andover, MA
    #24
    Agreed, that's not perfect... you actually can, I think, delete one line and move a couple around and make it work, though.
    Something like:
    Code:
    if (whatever < 100)
    {
      whatever = 100;
    }
    
    but, you know, a bit different.
     
  25. macrumors 603

    notjustjay

    Joined:
    Sep 19, 2003
    Location:
    Canada, eh?
    #25
    This is an online course? So you're just going through it at your own pace, and nobody's there to talk you through things and teach you more about certain topics?

    Might I suggest that you also check out some other tutorials, books, etc. while you're at it. Sometimes you don't really get a concept until it's taught in several different ways. I know that when I was trying to learn object-oriented programming (e.g. stepping up to C++ from C), I just did not get it at all until about the fourth book I read, which wrote about it in a way that finally made sense to me.

    It may also be helpful to step back a moment and just think in terms of English. How would YOU write a grid of #'s on paper? Sit down and break it into steps as simply as you can. Write the steps down in plain english. Then you can translate your algorithm "pseudocode" into real C code.

    Programming requires a level of thinking that some people pick up right away, and for others it takes a while to get used to thinking in small steps. Writing simple steps can help you think through what you're trying to tell the computer to do. Then, turn it into code, and then YOU step through the code exactly, slowly, using pen and paper, pretending you're the computer, to see if it makes sense.

    For example, you want to limit input to a maximum of 40. How would you do this in English? You've actually got two ways you could handle it:

    Approach 1:

    Ask for a number.
    If the number is bigger than 40, then tell the user the number is too big.
    Ask again, and keep asking until you get a number that's less than or equal to 40.

    (note the words "keep asking until <something>" suggests using a loop... writing the steps in English is a good way to shed light on what your code will end up looking like.)

    Approach 2:

    Ask for a number.
    If the number is bigger than 40, then just cap it at 40 and keep going.

    I'll use the Approach 2... the first is left as an exercise to you :) kainjow is on the right track, but his example is wrong ;)

    So turn that plain english into "pseudo code" (half code, half english)

    input(number)
    if (number > 40) then set number = 40

    Next step, turn that pseudo code into real, compiling, C code.

    It sounds tedious, but remember that you are learning several different things all at the same time:
    - how programming a computer in general works
    - designing an algorithm that correctly solves the problem
    - how C syntax works (where to put the {, }, semicolons, brackets, etc)

    Breaking it down into steps helps you separate the problems. If you can't keep that straight, then you'll have a very hard time proceeding because you'll be writing valid C code that doesn't solve the problem, or you'll know how to solve the problem but write the wrong code to do it, etc.

    With time and practice, the pseudo code will start to form in your head and you'll be able to just write the code. But even the pros will write out pseudocode when implementing complex algorithms, just to make sure they know exactly what they're doing.

    Good luck!
     

Share This Page