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

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
Any idea as to how to tackle this using NSLog?

"
7:
Write a method for the Rectangle class called draw that draws a rectangle using dashes and vertical bar characters. The following code sequence


Rectangle *myRect = [[Rectangle alloc] init];
[myRect setWidth: 10 andHeight: 3];
[myRect draw];
[myRect free];
"
Would look something like this.
----------
| |
| |
| |
----------

(Cannot get the formatting correct, but I think you get the idea)

Version 1.0 used printf, but in keeping with the Authors guidelines, printf is not used. NSLog prints each character on a new line.....I think :)
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
NSLog adds a newline at the end of each invocation. Just build a NSMutableString with each line or the whole shape and print it with one NSLog. If that's beyond the current progress in the book, I'm sure there's something similar you can do with what's been covered so far.

-Lee
 

TotalLuck

macrumors newbie
Jan 3, 2009
21
0
Moreno Vallley
i couldn't figure it our either.

I had to use printf to get-r-done. If you found a way to use NSLog please help me out.

Code:
-(void) draw
{
	int count1 = width;
	int count2 = height;
	
	while (count1 > 0)
		{
			printf ("_");
			--count1;
		}
	
	printf("\n");

	while (count2 > 0)
		{
			count1 = width;
			printf("|");
			while (count1 > 1)
			{
				printf (" ");
				--count1;
			}
			printf("|");
			--count2;
			printf("\n");
		}
	
	count1 = width;
		
	while (count1 > 0)
		{
			printf ("_");
			--count1;
		}
	
}

thanks
 

skochan

macrumors regular
Apr 1, 2006
150
0
California
Any idea as to how to tackle this using NSLog?
Version 1.0 used printf, but in keeping with the Authors guidelines, printf is not used. NSLog prints each character on a new line.....I think :)

Oops! You're right! In the first edition I assumed printf would be used (NSLog isn't taught by that point yet). I hadn't thought about how to use NSLog to do it in the second edition with what's been learned to that point.

I can get the sides of the rectangle with one NSLog call like this:

Code:
NSLog (@"|%*c|\n", width - 2, ' ');

but even that uses a formatting feature I didn't teach in the text (variable-width format specified by the corresponding argument to NSLog/printf). And I don't know of a way to do the top and bottom of the rectangle with a single NSLog call, given what you've learned. [There is a way based on an assumption of the maximum width of a rectangle--not very clean at all, so I'm not going to even share it! :)]

Thanks for pointing this out and for trying to solve it! I'll make note of this on my errata page.

Cheers,

Steve Kochan
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.