Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 26th, 2005, 1:19 AM   #1
Nano
Newbie
 
Nano's Avatar
 
Join Date: Aug 2004
Location: Hacienda Heights, CA
Posts: 8
Rep Power: 0 Nano is on a distinguished road
Send a message via AIM to Nano Send a message via MSN to Nano Send a message via Yahoo to Nano
Strategy to Solve these 2 questions

Hi everyone! AP tests are coming (Start next week) and I am taking Computer Science AB's test. While I was reviewing (doing last year's ap test), I found myself have most trouble on these 2 types of question:
The first type is
public static int indonesia(int p, int q)
{
     if (p/q == 0)
          return q;
     else
          return indonesia (q, p/q);
}

public static int thailand (int p, int q)
{
     int temp = -1;
     int value = 0;
     while (temp != 0)
     {
          temp = p/q;
          if (temp == 0)
          {
                 value = q;
          }
          else
          {
                 p = q;
                 q = temp;
          }
      }
       return value;
}

For which values of p and q do indonesia (p,q) and thailand (p, q) return the same result?
(A) For all values of p and q
(B) For all values, such that p > q
(C) For all values, such that p <= q
(D) For all values, such that p > 0 and q >0
(E) For no values of p and q
I was wondering how to solve the above problem during the test. I think there's 2 approaches, first is plug values respect to the answer in, or recognize the program pattern. Plug the value and test takes a long time, and sometimes I can't recognize the program pattern. Can anybody share their strategy to solve this kind of problem?

Second is Boolean Algebra:
a && (a || b)
a || (a && b)
For which values of a and b are the two expressions equivalent?
(A) For all values of a and b
(B) For no values of a and b
(C) Only whenever a is true
(D) Only whenever b is true
(E) Only whenever both a is true and b is true
Do I plug True or False to test the expressions? I have no idea how to do this kind.

I hope I can get some help because I couldn't get any help from school.......
Thanks in Advance!
__________________
why....why u.s. citizen.............
Nano is offline   Reply With Quote
Old Apr 26th, 2005, 1:45 AM   #2
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 6 bl00dninja is on a distinguished road
i thin kthe second one you plug in true. i think only when a is true. the other one i'm not totally sure, little too much beer to think recursively, but just think about testing for p == 0 rather than p/q == 0...makes it easier.
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
Old Apr 28th, 2005, 12:41 AM   #3
You Like Java?
Newbie
 
You Like Java?'s Avatar
 
Join Date: Mar 2005
Location: aboho.com (sn:eyvind)
Posts: 20
Rep Power: 0 You Like Java? is on a distinguished road
Send a message via AIM to You Like Java? Send a message via MSN to You Like Java? Send a message via Yahoo to You Like Java?
(I took AP COmputer Science A last year and recieved a 5, Iw as going to take AB this year, but too few students signed up for it so it was concelled. I know the stuff though.)

For the first problem:
You will want to begin by analyzing indonesia().
Let's say p = 0, then p/q would also be zero, so indonesia would return q.
If q was zero, it would through a DivideByZeroException.
If p was not equal to zero, then it would go through again with p = q and q = p/q.
Let's take two arbitrary numbers, p = 4 and q = 2. p/q = 2, so it calls indonesia(2, 2), 2/2 (p/q) = 1, so it calls indonesia(2, 1). p/q (2/1) is still 2, so it calls indonesia(1, 2). Now, 1/2 is 0.5, BUT since indonesia returns an int, it is truncated to zero, therefore indonesia(4, 2) returns 2.
Now we should try with p<q, so let's say p=2 and q=4:
step 1: p/q = 2/4 = 0.5, truncated to 0, returns 4.
so there is only one step. We see that the method behaves differently for the two situations.
So this is what we have found (looking at teh possible answers):
- If p>q, the method returns some number (I don't think we need to delve further than that)
- If p<=q... if p<q the method returns q, if p = q, the method returns p.
- If both p and q are >0, it returns some number.
Now for the second method thailand().
...

So, yeah, the only way is to go throgh it logically and possibly plugging in numbers.

~~~~~~~~~~~~~~~~~~~~~~~~

For the first one, if a is true, it is always true since a is true and a or b is true (since a is true) it is false in any other case. For the second statement, if a is false teh whole thing is always gfalse since a is false and a && b is false (since a is false it doesn't amytter what b is). if a is true: true || something else, it is always true.

Statement 1: if a = true then true, if a = false then false
Statement 2: if a = true then true, if a = false then false

Therefore you can eliminate E (since they are equivalent for the above cases), D (since it doesn't matter what b is), C (they are equivalent when a is false), B, (they are equivalent for the cases above). SO I guesswe found teh answer.

On the test, some questions will take longer time, some will take one or two seconds, as long as you know exactly what to do and you start doing it immediately you should be fine. And remember to write all over your test, it really helps, especially when working out recursion (you will need to trace the stack which can get really confusing).

Hope that helped.
__________________
If you need help with Java, you can go to javaforum.tk and get answers quickly.

Programmer YouLikeJava = new JavaProgrammer("Eyvind");
YouLikeJava.printInfo();
YouLikeJava.setWeb("You Like Java?");
You Like Java? is offline   Reply With Quote
Old May 6th, 2005, 8:35 AM   #4
HeX
Programmer
 
HeX's Avatar
 
Join Date: May 2005
Location: Kosova
Posts: 94
Rep Power: 4 HeX is on a distinguished road
Send a message via MSN to HeX
Well the second one is very easy to solve mathematically:

let's say 1 = "true", 0 = "false". Therefore, for the first statement we have

| a | b | (a||b) | a && (a||b)|
|----------------------------|
| 1 | 1 |___1___|____1_____|
| 1 | 0 |___1___|____1_____|
| 0 | 1 |___1___|____0_____|
| 0 | 0 |___0___|____0_____|

and for the second statement we have:

| a | b | (a&&b) | a || (a&&b)|
|----------------------------|
| 1 | 1 |___1___|____1_____|
| 1 | 0 |___0___|____1_____|
| 0 | 1 |___0___|____0_____|
| 0 | 0 |___0___|____0_____|

So, if you compare the two tables then you will see that the result is the same for every a and b.
__________________
countdown++;

Last edited by HeX; May 6th, 2005 at 8:51 AM.
HeX is offline   Reply With Quote
Old May 6th, 2005, 9:08 AM   #5
HeX
Programmer
 
HeX's Avatar
 
Join Date: May 2005
Location: Kosova
Posts: 94
Rep Power: 4 HeX is on a distinguished road
Send a message via MSN to HeX
Well, for the first one you just need to check the methods for the given options.
A. For all values of p and q, the methods return a number, but we aren't sure what. So, this option fails.
B. For all values, such that p > q the methods return a number, but we aren't sure what. So, this option fails.
C. For all values, such that p <= q both methods return q. Therefore this is the right answer.
__________________
countdown++;
HeX is offline   Reply With Quote
Old May 16th, 2005, 7:45 PM   #6
You Like Java?
Newbie
 
You Like Java?'s Avatar
 
Join Date: Mar 2005
Location: aboho.com (sn:eyvind)
Posts: 20
Rep Power: 0 You Like Java? is on a distinguished road
Send a message via AIM to You Like Java? Send a message via MSN to You Like Java? Send a message via Yahoo to You Like Java?
Isn't this what I said? w/e
__________________
If you need help with Java, you can go to javaforum.tk and get answers quickly.

Programmer YouLikeJava = new JavaProgrammer("Eyvind");
YouLikeJava.printInfo();
YouLikeJava.setWeb("You Like Java?");
You Like Java? is offline   Reply With Quote
Old May 16th, 2005, 7:52 PM   #7
HeX
Programmer
 
HeX's Avatar
 
Join Date: May 2005
Location: Kosova
Posts: 94
Rep Power: 4 HeX is on a distinguished road
Send a message via MSN to HeX
No. you said that B would be the right answer. And it is not.
__________________
countdown++;
HeX is offline   Reply With Quote
Old May 22nd, 2005, 8:58 PM   #8
You Like Java?
Newbie
 
You Like Java?'s Avatar
 
Join Date: Mar 2005
Location: aboho.com (sn:eyvind)
Posts: 20
Rep Power: 0 You Like Java? is on a distinguished road
Send a message via AIM to You Like Java? Send a message via MSN to You Like Java? Send a message via Yahoo to You Like Java?
No, I eliminated E, D, C, and B, leaving the answer as A. I jsut never explicitly stated teh answer to be A, I said, after eliminating everything but A, "SO I guesswe found teh answer."

I'm sorry for not being clear enough, I see how you can think I said that B was the answer, Glad we cleared it up anyhow.
__________________
If you need help with Java, you can go to javaforum.tk and get answers quickly.

Programmer YouLikeJava = new JavaProgrammer("Eyvind");
YouLikeJava.printInfo();
YouLikeJava.setWeb("You Like Java?");
You Like Java? is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:19 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC