Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
August 17, 2017, 04:46:48 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1] 2   Go Down
  Print  
Author Topic: Exercise 8.8 using MIN and MAX Functions  (Read 5932 times)
adamgonzalez1984
Jr. Member
**
Posts: 89


« on: February 12, 2009, 08:50:37 PM »

Here's what I got for Chapter 8E8....

I have a question though...  Can I make this work using NSLog(@"")Huh?  I had to use printf to have the dashes print out on the same line.  NSLog makes it like this....
-
-
-
-
-
|  |
|  |
|  |
-
-
-
-
-





#import "Rectangle.h"



@implementation Rectangle  

@synthesize width, height;


-(void) setWidth: (float) w andHeight: (float) h
{
   width = w;
   height = h;
}

//----------------------------------------------------


-(void) draw
{

int w, h;




for (w=1; w<= (width/2); ++w)
printf("-");
printf("\n");
for (h=1; h<=height; ++h)
printf("|   |\n");
for (w=1; w<= (width/2); ++w)
printf("-");


}
@end
« Last Edit: August 19, 2011, 10:27:19 AM by fujilla » Logged
esc
Global Moderator
Full Member
*****
Posts: 230


« Reply #1 on: February 13, 2009, 12:45:39 AM »

I used printf also.  There's mention in the book to use printf when we need to print on the same line.
« Last Edit: August 19, 2011, 10:31:32 AM by fujilla » Logged
adamgonzalez1984
Jr. Member
**
Posts: 89


« Reply #2 on: February 13, 2009, 09:44:46 AM »

Gotcha!! I must have skipped that part!  Thanks
« Last Edit: August 19, 2011, 10:31:45 AM by fujilla » Logged
rSammond
Newbie
*
Posts: 2


« Reply #3 on: March 09, 2009, 08:56:51 AM »

A more flexible solution to drawing the vertical lines would be:

   int i, j;
   for(i = 0; i < height; i++)
   {
      printf("|");
      for(j = 0; j < width/2 - 2; j++)
         printf(" ");
      printf("|\n");
   }
This allows the width to be changed and still produce a correct result. The solution

for (h=1; h<=height; ++h)
printf("|   |\n");

assumes the width. If it was changed (i.e. 20) the drawing would be incorrect.
« Last Edit: August 19, 2011, 10:31:56 AM by fujilla » Logged
m.schick
Newbie
*
Posts: 3


Email
« Reply #4 on: March 21, 2009, 09:45:03 PM »

This is how I implemented the the "draw" method using NSLog....

-(void) draw
{
   NSMutableString * horizontal = [[NSMutableString alloc] init];
   NSMutableString * vertical = [[NSMutableString alloc] init];
   
   for(int countWidth = 0; countWidth < width; countWidth++)
   {
      [horizontal appendFormat:@"-"];
   }
   
   NSLog(@"%@", horizontal);
   
   for(int countHeight = 0; countHeight < height; countHeight++)
   {
      [vertical appendFormat:@"|"];
      
      for(int countWidth = 2; countWidth < width; countWidth++)
      {
         [vertical appendFormat:@" "];
      }
      
      [vertical appendFormat:@"|"];
      
      NSLog(@"%@", vertical);
      [vertical setString:@""];
   }
   
   [horizontal setString:@""];
   
   for(int countWidth = 0; countWidth < width; countWidth++)
   {
      [horizontal appendFormat:@"-"];
   }
   
   NSLog(@"%@", horizontal);
   horizontal.release;
   vertical.release;
}



I figured it was ok to use the NSMutableString class since the chapter was about inheritence....and plus it works with all widths and heights that you set the rectangle too.
« Last Edit: August 19, 2011, 10:34:23 AM by fujilla » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #5 on: March 21, 2009, 10:33:33 PM »

haha! Ok, good solution, but of course way ahead of what's
been taught by this point in the book!

Cheers,

Steve Kochan
« Last Edit: August 19, 2011, 10:34:45 AM by fujilla » Logged
rock
Newbie
*
Posts: 10


« Reply #6 on: April 15, 2009, 08:51:58 PM »

Yeah, mine was basically nested loops as well.  I do have a question, though.

Is there a way to repeat characters to create a string?

For example:

top = '-'*6;

« Last Edit: August 19, 2011, 10:34:57 AM by fujilla » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #7 on: April 15, 2009, 09:16:30 PM »

No way to do that!

Steve
« Last Edit: August 19, 2011, 10:35:09 AM by fujilla » Logged
rock
Newbie
*
Posts: 10


« Reply #8 on: April 16, 2009, 05:32:02 PM »

dang.  :-)
« Last Edit: August 19, 2011, 10:35:20 AM by fujilla » Logged
mg
Newbie
*
Posts: 2


« Reply #9 on: May 12, 2009, 03:29:05 PM »

Here's the method I came up with, which seems to work.

Note that the drawn example shown in the book seems technically correct but logically not representative of the rectangle you want to draw. With a width of 10 and height of 3, the expected result would be a rectangle that is wider than it is tall. But the example rectangle in the text is taller than it is wide.  

The method below seems to give me a shape that more closely resembles the desired rectangle, although with the unequal character sizes I doubt it's possible to get the proportions exactly right.

-(void) draw
{
   // put a lid on it
   for (int i = 1; i <= width; i++)
      printf("-");
   
   // draw some sides
   for (int n = 1; n <= height; n++) {
      printf("\n|"); // print the left side on a new line
      for (int i = 1; i <= width-2; i++)  // spaces between the sides
         printf(" ");
      printf("|"); // right side
   }
   
   printf("\n"); // move on down to the bottom
   
   // draw the bottom, same as the top
   for (int i = 1; i <= width; i++)
      printf("-");
}
« Last Edit: August 19, 2011, 10:35:32 AM by fujilla » Logged
Neil
Newbie
*
Posts: 5


Email
« Reply #10 on: May 16, 2009, 07:39:25 AM »

Hi

Am I missing something somewhere?  I couldn't find any reference to printf anywhere before Chapter 8 (I have the second edition), or in the index.  

Thanks

Neil
« Last Edit: August 19, 2011, 10:35:47 AM by fujilla » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #11 on: May 16, 2009, 08:32:40 AM »

No, you didn't miss anything.  printf was used in the first edition for the first part of the book.  C programmers are very familiar with using printf, as it's the usual way to write messages to a console or terminal window.,

Cheers,

Steve Kochan
« Last Edit: August 19, 2011, 10:35:57 AM by fujilla » Logged
airhead3
Newbie
*
Posts: 9


« Reply #12 on: May 27, 2009, 10:27:05 AM »

A more flexible solution to drawing the vertical lines would be:

   int i, j;
   for(i = 0; i < height; i++)
   {
      printf("|");
      for(j = 0; j < width/2 - 2; j++)
         printf(" ");
      printf("|\n");
   }
This allows the width to be changed and still produce a correct result. The solution

for (h=1; h<=height; ++h)
printf("|   |\n");

assumes the width. If it was changed (i.e. 20) the drawing would be incorrect.


Hi rSammond - I agree that the spaces need to be printed using a nested for loop to ensure it would support changing values, but why are you dividing the width by two in the for evaluation? I agree with the subtracting two (to account for the character spacing for the two "|", but dividing by two just shortens the right vertical line to be down the center of the rectangle rather than on the right side. Unless I'm missing something??  Wink

-Ryan
« Last Edit: August 19, 2011, 10:36:16 AM by fujilla » Logged
chrisherbert
Newbie
*
Posts: 8


« Reply #13 on: September 16, 2009, 12:03:24 PM »

I'm not sure why my "if" nested inside the "for" doesn't work.  As it stands, it just runs infinitely.

If I put a "break" in after printf, it works once starts the next iteration of the for loop.

If I eliminate the "if" and just put "printf ("%i", iw) I get what I expect - 10987654321.

Code: (Objective-C)
-(void) draw
{
//draw top of rectangle
iw = width;
while (iw > 0) {
printf("-");
iw--;
}

printf("\n");

//draw sides

for (ih = height; ih >0; ih--) { //loop as many times as the rectangle is high

for (iw = width; iw >0; iw--){ //loop as many times as the rectangle is wide

if ((iw=width) || (iw=1)) //if its the first or last
printf("|");
else
printf (" ");
}

printf("\n"); //new line
}

//draw bottom
iw = width;
while (iw > 0) {
printf("-");
iw--;
}
« Last Edit: August 19, 2011, 10:36:30 AM by fujilla » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #14 on: September 16, 2009, 12:36:01 PM »

Remember, = is for assignment, == is the equality test operator.

Cheers,

Steve
« Last Edit: August 19, 2011, 10:36:42 AM by fujilla » Logged
Pages: [1] 2   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.