PDA

View Full Version : Online C course exercise question




jruizisenberg
Feb 10, 2008, 04:26 PM
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....



toddburch
Feb 10, 2008, 05:04 PM
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

jruizisenberg
Feb 10, 2008, 05:17 PM
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...

toddburch
Feb 10, 2008, 05:18 PM
try

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

jruizisenberg
Feb 10, 2008, 05:24 PM
try

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



no go... :(

toddburch
Feb 10, 2008, 06:07 PM
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

toddburch
Feb 10, 2008, 06:08 PM
And this is not correct:

col1='#';

col1 is an integer.

toddburch
Feb 10, 2008, 06:10 PM
And... no how, no way will this work:


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

jruizisenberg
Feb 10, 2008, 06:20 PM
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

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?

toddburch
Feb 10, 2008, 06:22 PM
And what did you learn about FOR loop syntax?

jruizisenberg
Feb 10, 2008, 06:26 PM
And what did you learn about FOR loop syntax?

what do you mean?

Eraserhead
Feb 10, 2008, 06:48 PM
Stop defining your ints to '#' it isn't an integer.

toddburch
Feb 10, 2008, 07:13 PM
what do you mean?

I was referring to the comment I made in post #8.

See here: http://pbpl.physics.ucla.edu/Computing/C_Tutorial/Section_3_(Program_Flow)/

and this sentence in particular:

The body of the loop follows in the statement block, marked by curly brackets.

Todd

jruizisenberg
Feb 10, 2008, 07:23 PM
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

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

toddburch
Feb 10, 2008, 07:46 PM
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.

jsw
Feb 10, 2008, 07:52 PM
The syntax would be:

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

so, for you, it'd be


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

...maybe another loop here, perhaps?...
}

jruizisenberg
Feb 10, 2008, 07:53 PM
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.

can you give me an example of what you mean?

toddburch
Feb 10, 2008, 07:57 PM
See jsw's post above.

You code, properly indented, looks like this:

/*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


for (initializer ; conditional_test ; increment ) single_statement ;


However, you don't want a single statement inside the FOR loop, therefore use jsw's example with the curly braces.

toddburch
Feb 10, 2008, 07:59 PM
P.S. Please use code tags to wrap your code, like this:



#include ....



and it will look like this:

#include ....

jsw
Feb 10, 2008, 08:03 PM
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.

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


for (initializer ; conditional_test ; increment ) single_statement ;


However, you don't want a single statement inside the FOR loop, therefore use jsw's example with the curly braces.
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

for (x = 0; x < 10; x++)
printf("a");
printf("b");

which prints "aaaaaaaaaab", not "abababababababababab" as many beginners might think.

jruizisenberg
Feb 10, 2008, 08:32 PM
See jsw's post above.

You code, properly indented, looks like this:

/*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


for (initializer ; conditional_test ; increment ) single_statement ;


However, you don't want a single statement inside the FOR loop, therefore use jsw's example with the curly braces.


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?

kainjow
Feb 10, 2008, 08:35 PM
now how do i limit to a maximum input of 40?

Would probably be easiest to use the do-while loop. Read up on it if you haven't already. Maybe something like...

do
{
... read input into variable X ...
} while (X <= 40);

toddburch
Feb 10, 2008, 08:37 PM
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.

jsw
Feb 10, 2008, 09:01 PM
got it working perfectly with:
Agreed, that's not perfect... you actually can, I think, delete one line and move a couple around and make it work, though.
now how do i limit to a maximum input of 40?
Something like:

if (whatever < 100)
{
whatever = 100;
}

but, you know, a bit different.

notjustjay
Feb 10, 2008, 09:02 PM
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!

kainjow
Feb 10, 2008, 09:05 PM
I'll use the second example... the first is left as an exercise to you :) kainjow is on the right track, but his example is wrong ;)

Oops :o.. I'll let the OP figure out what's wrong with it :D

GirthP
Feb 18, 2008, 08:00 PM
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!


Thank you! That was very helpful for me!

Cromulent
Feb 19, 2008, 03:34 AM
A couple of things.

Your application should return 0 on success, not 1. Returning 1 is used to indicate a failure in your program, generally when using error checking.

Edit : Should clarify that it is actually -1 normally on error but that some functions just return a non-zero value on error.

Also it has been mentioned before but you really should use tags to enclose your code. It makes it incredibly difficult to read your code if you do not use them.

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?

You have some problems with your for loops in the code you posted above. I think you should invest in a good book on C. I'd recommend A Book On C, it is short and concise while still being very informative.