Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jruizisenberg

macrumors newbie
Original poster
Feb 10, 2008
8
0
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

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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

macrumors newbie
Original poster
Feb 10, 2008
8
0
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

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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
 

jruizisenberg

macrumors newbie
Original poster
Feb 10, 2008
8
0
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?
 

jruizisenberg

macrumors newbie
Original poster
Feb 10, 2008
8
0
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

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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

Moderator emeritus
Mar 16, 2004
22,910
44
Andover, MA
The syntax would be:

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

so, for you, it'd be

Code:
for ()
{
  ...do something...

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

jruizisenberg

macrumors newbie
Original poster
Feb 10, 2008
8
0
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

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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.
 

jsw

Moderator emeritus
Mar 16, 2004
22,910
44
Andover, MA
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

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

jruizisenberg

macrumors newbie
Original poster
Feb 10, 2008
8
0
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.


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?
 

notjustjay

macrumors 603
Sep 19, 2003
6,056
167
Canada, eh?
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!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.