Chapter 4 : Making Decisions About…and Planning Program Flow

This forum contains all the assignments and questions readers may have for each chapter.

Moderator: gary.bennett

Chapter 4 : Making Decisions About…and Planning Program Flow

Postby gary.bennett » Sat Aug 14, 2010 3:31 pm

Making Decisions About…and Planning Program Flow

    Boolean Logic
    Truth Tables
    Comparison Operators
    Designing Apps
    Pseudo-code.
    Design Requirements.
    Flowcharting
    Designing and Flowcharting an Example App
    The App’s Design
    Using Loops to Repeat Program Statements
    Coding the Example App in Alice
    Coding the Example App in Objective-C
    Nested If Statements and Else-If Statements.



    Improving the Code Through Refactoring
    Moving Forward Without Alice.


Weekly Q&A for Objective-C for Absolute Beginners Book

Purpose: Answer questions about Objective-C and OOP from book or other related info.
This Webinar is held every week on Wednesday, from:
6:30 PM - 7:30 PM PST
Register Now at:
https://www2.gotomeeting.com/register/763610930
Once registered you will receive an email confirming your registration with information you need to join the Webinar.
System Requirements
PC-based attendees
Required: Windows® 7, Vista, XP or 2003 Server
Macintosh®-based attendees
Required: Mac OS® X 10.4.11 (Tiger®) or newer
Attachments
Ch04 RandomNumber.zip
Chapter 4 Source Code
(55.15 KiB) Downloaded 1028 times
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby trxc » Tue Dec 27, 2011 12:45 pm

Can someone post the code to the exercises at the end of this chapter? I have tried doing them, but am not sure how to complete them. I can't figure it out.

Thanks
trxc
 
Posts: 0
Joined: Sun Dec 25, 2011 12:20 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby AndieF » Sun Jan 29, 2012 5:29 pm

Hello~

I'm trying to run through the exercise in Alice for Chapter 4 and am at a loss on the first "if / else". I can't figure out how to get to IF ( userGuess == randomNumber is TRUE). When I drag down the "If" and put it in place, it doesn't give me an option to change it to "userGuess". I can only choose continueGuessing or keepPlaying. I tried to drag down userGuess too but that didn't work either. Did I miss something here? Feel like I've been raking my brains over a cheese grater for the past hour!
Thanks

AndieF
AndieF
 
Posts: 7
Joined: Sun Jan 29, 2012 9:12 am
Location: Utah

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby blue4dolphin » Mon Feb 06, 2012 12:43 pm

hi

I am at the end of chapter 4 in the book on the first excersise about extending the random number app to tell the user the number of turns they have had. But I am really struggling to get it to do this. I have tried things in previous chapters like using do in order with a sum and total. but the best I can get is every time it just saying "the number of times you have guessed is: 1" I can't get it to addon after each loop. Though I can get it to add on the number of times it sames it says the same thing after each loop! so after three loops it will say the earlier thing 3 times. This is now frustrating me as I do not want to move on without resolving it.

Charlotte
blue4dolphin
 
Posts: 0
Joined: Mon Feb 06, 2012 8:31 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby admin » Tue Feb 07, 2012 1:23 pm

blue4dolphin wrote:hi

I am at the end of chapter 4 in the book on the first excersise about extending the random number app to tell the user the number of turns they have had. But I am really struggling to get it to do this. I have tried things in previous chapters like using do in order with a sum and total. but the best I can get is every time it just saying "the number of times you have guessed is: 1" I can't get it to addon after each loop. Though I can get it to add on the number of times it sames it says the same thing after each loop! so after three loops it will say the earlier thing 3 times. This is now frustrating me as I do not want to move on without resolving it.

Charlotte


Inside you guessing loop keep a counter variable and increment it. At the of the loop print out the variable.
pseudo code

Code: Select all
(while !correctGuess)
{
    guessNumber = guessNumber + 1;
..
..


}

Print guessNumber
...
..
admin
Site Admin
 
Posts: 12
Joined: Wed Feb 18, 2009 2:32 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby Ebenezer » Sat Feb 11, 2012 9:50 am

Thanks for providing the chapter 4 source code. It will be really helpful for me.
User avatar
Ebenezer
 
Posts: 0
Joined: Fri Dec 16, 2011 8:53 am
Location: USA

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby blue4dolphin » Mon Feb 13, 2012 10:36 am

I am getting nowhere quickly here , it will not keep adding on

@autoreleasepool {

// insert code here...
int randomNumber = 1;
int userGuess = 1;
BOOL continueGuessing = TRUE;
BOOL keepPlaying = TRUE;
char yesNo =' ';
int guessNumber = 0;

while (keepPlaying)
{
for(guessNumber = 0; guessNumber++;
guessNumber = guessNumber + 1
)randomNumber = (arc4random()% 101);
NSLog(@"e random number to guess is: %d",randomNumber);
while (continueGuessing)
{
NSLog (@"Pick a number between 0 and 100. ");
scanf ("%i", &userGuess);
fgetc(stdin);//remove CR/LF i.e extra character
if (userGuess == randomNumber)
{
continueGuessing = FALSE;
NSLog(@"Correct number!");
}
//nested if statement
else if (userGuess > randomNumber)
{
//user guessed too high
NSLog(@"Your guess is too high");
}
else
{
// no reason to check if userGuess < randomNumber. It has to be.
NSLog(@"Your guess is too low");
}
//refactored from our Alice app. This way we only have to code once.
NSLog(@"The user guessed %d",userGuess);
}

NSLog (@"The number of times the user guessed is %d",guessNumber);

NSLog (@"Play Again? Y or N");
blue4dolphin
 
Posts: 0
Joined: Mon Feb 06, 2012 8:31 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby JulianHuff » Mon Feb 13, 2012 9:20 pm

blue4dolphin wrote:I am getting nowhere quickly here , it will not keep adding on

@autoreleasepool {

// insert code here...
int randomNumber = 1;
int userGuess = 1;
BOOL continueGuessing = TRUE;
BOOL keepPlaying = TRUE;
char yesNo =' ';
int guessNumber = 0;

while (keepPlaying)
{
for(guessNumber = 0; guessNumber++;
guessNumber = guessNumber + 1
)randomNumber = (arc4random()% 101);
NSLog(@"e random number to guess is: %d",randomNumber);
while (continueGuessing)
{
NSLog (@"Pick a number between 0 and 100. ");
scanf ("%i", &userGuess);
fgetc(stdin);//remove CR/LF i.e extra character
if (userGuess == randomNumber)
{
continueGuessing = FALSE;
NSLog(@"Correct number!");
}
//nested if statement
else if (userGuess > randomNumber)
{
//user guessed too high
NSLog(@"Your guess is too high");
}
else
{
// no reason to check if userGuess < randomNumber. It has to be.
NSLog(@"Your guess is too low");
}
//refactored from our Alice app. This way we only have to code once.
NSLog(@"The user guessed %d",userGuess);
}

NSLog (@"The number of times the user guessed is %d",guessNumber);

NSLog (@"Play Again? Y or N");

Your "for" statement is a little off, I think. So it's not tracking the number of guesses right. Here's the code I came up with if it helps.

------------
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{

@autoreleasepool {

// insert code here...
int randomNumber =1;
int userGuess = 1;
BOOL continueGuessing = TRUE;
BOOL keepPlaying = TRUE;
char yesNo = ' ';
int guessCount =0;
int gameCount = 1;

while (keepPlaying)
{
randomNumber = (arc4random() % 101); //generate random number
NSLog(@"The random number to guess is: %d", randomNumber);
while (continueGuessing)
{
NSLog (@"Pick a number between 0 and 100. ");
scanf ("%i", &userGuess);
fgetc(stdin);//remove CR/LF i.e extra character
guessCount= guessCount+1;//increment number of guesses
if (userGuess ==randomNumber)
{
continueGuessing = FALSE;//to show number of guesses in game
NSLog(@"Correct number!");
NSLog(@"You guessed %d times!",guessCount);

}
else if (userGuess > randomNumber)
{
NSLog(@"Your guess is too high, genius!");
}
else
{
NSLog(@"Your guess is too low!");
}
//Print the guess the user made
NSLog(@"The user guessed %d", userGuess);
}
NSLog (@"Play Again? Y or N");
yesNo=fgetc(stdin);
if (yesNo == 'N' || yesNo == 'n')
{
keepPlaying = FALSE;
NSLog(@"You played %d times!", gameCount);
}
continueGuessing = TRUE;// code to increment and count number of games played
gameCount=gameCount+1;
guessCount=0;
}
}
return 0;
}
JulianHuff
 
Posts: 4
Joined: Sun Sep 26, 2010 10:08 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby blue4dolphin » Tue Feb 14, 2012 6:12 am

thank you very much for this I will have another go. I found it hard because I could not find the right type of count loop in the book. Have you managed it in ALice?
blue4dolphin
 
Posts: 0
Joined: Mon Feb 06, 2012 8:31 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby blue4dolphin » Tue Feb 14, 2012 7:05 am

It worked. thank you now to see if I can do it in Alice and do the next exercise.
yours was different to what was posted before and I could not see a for statement in yours?

I have never done any computer programming before.

Thank you for your help.
blue4dolphin
 
Posts: 0
Joined: Mon Feb 06, 2012 8:31 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby blue4dolphin » Tue Feb 14, 2012 12:21 pm

not so easy in Alice I can't find a code or make one the same as was used in Xcode
blue4dolphin
 
Posts: 0
Joined: Mon Feb 06, 2012 8:31 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby blue4dolphin » Thu Feb 16, 2012 8:38 am

I can't get it to add on 1 every time the user guesses
blue4dolphin
 
Posts: 0
Joined: Mon Feb 06, 2012 8:31 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby blue4dolphin » Sat Feb 18, 2012 7:24 am

ok I can do both exercises now in Xcode but neither in Alice as I still can't workout how to make it count on?

the other bug I have now come across in Xcode with it I can't resolve is when I answer Y to play again it continues to add-on to the number of goes from the previous total. I don't know how to make it start again from 0?
blue4dolphin
 
Posts: 0
Joined: Mon Feb 06, 2012 8:31 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby blue4dolphin » Sat Feb 18, 2012 7:29 am

Forget that just had a brainwave!

Resolved! it now starts at 0 again each time.

still can't do in Alice though :-(
blue4dolphin
 
Posts: 0
Joined: Mon Feb 06, 2012 8:31 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby moundmaggit » Thu Feb 23, 2012 10:48 am

Many thanks for posting the answer to this exercise!! I was pulling my hair out!
moundmaggit
 
Posts: 0
Joined: Wed Jan 18, 2012 7:41 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby blue4dolphin » Fri Feb 24, 2012 4:35 am

I have yet to manage it in Alice though so when I get time as I have done it in Xcode will move onto chapter 5.

would be interested to know how to do it in Alice though as I was also pulling my hair out :o
blue4dolphin
 
Posts: 0
Joined: Mon Feb 06, 2012 8:31 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby Kockmaster » Tue Mar 06, 2012 7:07 am

Hi!
First off, Great book and videos!

I'm currently in chapter 4, doing the random number generator app (listing 4-9) a few times to get a hang of what I'm doing. I see below the listing that "The source code for this Objective-C project is available for download forum.xcelme.com.
Additionally there is a short video explaining the source code and the project."

This short video explaining the source code, is this the video linked in the source code-part of the forum, explaining the code in Alice. Or is there also one explaining the source code in Objective-C? I realize they're mostly the same, but it would be nice to have it explained anyway, if there is a video. Mostly I think it's the fgetc(stdin) that I don't really know what it is in relation to the Alice code.

Well, thanks!
Kockmaster
 
Posts: 0
Joined: Sat Mar 03, 2012 7:37 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby JosRuggiero » Fri Mar 16, 2012 5:58 pm

OK. So I've been able to get this to work on Alice on a Windows XP but can't seem to get it to work on a Mac mini with wireless mouse and wireless keyboard.

It generates a random number and asks me to pick a number but I can't get anything to happen after that. Either I'm overlooking something or the keyboard won't function properly to enter a value.

I feel like I'm standing it the right forrest but looking at the wrong tree.
Any help to point me in the right direction would be great!
JosRuggiero
 
Posts: 0
Joined: Sun Mar 04, 2012 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby blue4dolphin » Sat Mar 17, 2012 4:27 am

can you post the code you used to get it to work in Alice on a PC? Then at some point I can try it on my desk top mac.
blue4dolphin
 
Posts: 0
Joined: Mon Feb 06, 2012 8:31 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby gary.bennett » Sat Mar 17, 2012 5:33 pm

blue4dolphin wrote:can you post the code you used to get it to work in Alice on a PC? Then at some point I can try it on my desk top mac.


Just go to Alice.org and you download the PC version of Alice.
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby blue4dolphin » Sun Mar 18, 2012 4:19 am

I meant the programming code the above poster had used to get the number app to work in Alice on PC so I can try it on the Mac version as I have been unable to work this out for the exercises at the end of chapter 4.
blue4dolphin
 
Posts: 0
Joined: Mon Feb 06, 2012 8:31 am

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby JosRuggiero » Sun Mar 18, 2012 10:04 am

I posted the other night about being lost in the Forrest. I'm happy to report that I have resolved my stupidity and found the mistakes. This has been fun even if frustrating. Forget a Value and you can spend hours not seeing it. Good luck to all. Believe in yourself. Stay calm and think clearly. You will get it!
JosRuggiero
 
Posts: 0
Joined: Sun Mar 04, 2012 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby gary.bennett » Sun Mar 18, 2012 10:27 am

JosRuggiero wrote:I posted the other night about being lost in the Forrest. I'm happy to report that I have resolved my stupidity and found the mistakes. This has been fun even if frustrating. Forget a Value and you can spend hours not seeing it. Good luck to all. Believe in yourself. Stay calm and think clearly. You will get it!


Great post, I couldn't say it better myself!
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby biged » Fri Apr 20, 2012 7:08 am

[code][/code]
I have tried a different algorithm (code below) and everything works fine except that I cannot get it to accept the "0" to exit and print out "Bye Bye." I also do not know how to end the program at that point before the "Hooray" is printed.

//
// main.m
// RandomNumber
//
// Created by bigednc on 4/19/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {

int randomNumber = 0;
int guess = 0;

randomNumber = (arc4random() %101);
NSLog(@"%i", randomNumber);

while (guess != randomNumber){

NSLog(@"Enter any number between 1 and 100 or '0' to quit: ");

scanf ("%i", &guess);

fgetc(stdin);

if (guess == randomNumber || guess == 0)

continue;

else NSLog (@"Oh Shit!");
}
if(guess == 0)

{ NSLog (@"Bye Bye");
}
else NSLog (@"Hooray!");

}



}
biged
 
Posts: 0
Joined: Thu Apr 19, 2012 3:41 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby biged » Fri Apr 20, 2012 7:17 am

2 questions about the RandomNumber program printed in the book:

1. line 30: scans ("%i", &userGuess) - what is the & symbol for?

2. in debugging I tried the following: NSLog(@"%i%i" , randomNumber, guess) and it would not accept that. How do I print two items in a single statement?
biged
 
Posts: 0
Joined: Thu Apr 19, 2012 3:41 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby gary.bennett » Fri Apr 20, 2012 10:18 am

biged wrote:2 questions about the RandomNumber program printed in the book:

1. line 30: scans ("%i", &userGuess) - what is the & symbol for?

2. in debugging I tried the following: NSLog(@"%i%i" , randomNumber, guess) and it would not accept that. How do I print two items in a single statement?


1) & is the memory address where the value is located

2) I just tried NSLog(@"%i%i" , randomNumber, guess) and it works just fine. What is the error message?
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby gary.bennett » Fri Apr 20, 2012 10:20 am

biged wrote:[code][/code]
I have tried a different algorithm (code below) and everything works fine except that I cannot get it to accept the "0" to exit and print out "Bye Bye." I also do not know how to end the program at that point before the "Hooray" is printed.

//
// main.m
// RandomNumber
//
// Created by bigednc on 4/19/12.
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {

int randomNumber = 0;
int guess = 0;

randomNumber = (arc4random() %101);
NSLog(@"%i", randomNumber);

while (guess != randomNumber){

NSLog(@"Enter any number between 1 and 100 or '0' to quit: ");

scanf ("%i", &guess);

fgetc(stdin);

if (guess == randomNumber || guess == 0)

continue;

else NSLog (@"Oh Shit!");
}
if(guess == 0)

{ NSLog (@"Bye Bye");
}
else NSLog (@"Hooray!");

}



}



You are not matching up your { } after your if statements so you logic is getting out of whack. Review the if statement section of the book again. i.e.e
if (guess == randomNumber || guess == 0)
{
this is for true
}
else
{this is for false
..
..}

continue
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby angelo.destefano » Thu Aug 02, 2012 10:23 am

I copied and pasted the source code in xcode and it does not work. I am using xcode 4.4, could this be the problem?
angelo.destefano
 
Posts: 4
Joined: Tue Jul 31, 2012 6:42 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby gary.bennett » Thu Aug 02, 2012 10:58 am

angelo.destefano wrote:I copied and pasted the source code in xcode and it does not work. I am using xcode 4.4, could this be the problem?


Hi Angelo,
Please define...."Does not work". thanks
User avatar
gary.bennett
 
Posts: 564
Joined: Thu Feb 26, 2009 3:12 pm

Re: Chapter 4 : Making Decisions About…and Planning Program

Postby angelo.destefano » Fri Aug 03, 2012 12:29 pm

Hi Gary, never mind! I just commented out the [pool drain] and the program ran successfully. I also completed the exercises with ease. I am enjoying this course very much and will probably finish within a week. I hope to start the next course in the sequence on August 6th so I can participate in a live class. Take care.
angelo.destefano
 
Posts: 4
Joined: Tue Jul 31, 2012 6:42 pm

Next

Return to Book -> Objective-C for Absolute Beginners: (2nd Edition) iPhone and Mac Programming Made Easy

cron