Computer Science Canada

I NEED HELP WITH 4.2 Practice Question 11!

Author:  master-awesome [ Fri Nov 14, 2014 4:26 pm ]
Post subject:  I NEED HELP WITH 4.2 Practice Question 11!

The question is "Write a program that accepts 3 numbers from the user and then shows them in ascending or descending order (based on the user?s choice)."

I've spend hours and hours trying to figure this out and I'm at the point where I even want to ignore this question, but my teacher isn't much help and the students in my class are struggling equally hard, but considering all this, the teacher still refuses to help.

I don't know what to do and I'm beginning to hate this course because I'm left so confused and everybody thinks I should keep going because I get good marks in it. This really sucks! HELP ME PLEASE! Evil or Very Mad Evil or Very Mad

Here's my program:

Quote:
var A1 : real
var A2 : real
var A3 : real
var D1 : real
var D2 : real
var D3 : real
var answer : string
var result1 : string
var result2 : string
result1 := "Good! You will have to arrange your number from smallest to biggest for ascending!"
result2 := "Good! You will have to arrange your numbers from biggest to smallest for descending!"

put "Would you like to put your number in ascending or descending order? "
..put " Write \"A\" for ascending and \"D\" for descending."
get answer

if answer = "D" then
else
put ""
end if

if answer = "A" then
put result1
get A1,A2,A3
else
put ""
end if

if answer = "D" then
put result2
get A1,A2,A3
else
put ""
end if

if A1 > A2 and A1 > A3 then
colour (2)
put "The number ",A1," is the highest!"
else
colour (12)
put ""
end if

if A2 > A3 and A2 < A1 then
colour (2)
put "The number ",A2," is the medium!"
else
colour (12)
put ""
end if

if A3 < A2 and A3 < A1 then
colour (2)
put "The number ",A3," is the smallest!"
else
colour (12)
put ""
end if

if A1 < A2 and A1 < A3 then
colour (2)
put "The number ",A1," is the smallest!"
else
colour (12)
put ""
end if

if A2 > A1 and A2 < A3 then
colour (2)
put "The number ",A2," is the medium!"
else
colour (12)
put ""
end if

if A3 > A1 and A3 > A2 then
put "The number ",A3," is the highest!"
else
colour (12)
put ""
end if

Author:  Tony [ Fri Nov 14, 2014 4:56 pm ]
Post subject:  RE:I NEED HELP WITH 4.2 Practice Question 11!

I've moved this topic to Turing Help from General Discussion.

When you say
Quote:

the teacher still refuses to help.

what exact question are you asking your teacher about this problem?

The approaches to this problem are also wildly different depending on the usage of arrays -- if you've already covered those in class, it's probably a good idea to use them. If not, that's okay too, this is totally solvable.

For some inspiration, it might help to find a deck of cards, pick out 3 numbered cards, and arrange them in order. It's a simple task, the difficult part is noticing exactly what you are doing -- what "variables" are you remembering? What kind of comparisons are you doing between cards? If you write down the steps you took, and try it again with a different set of 3 cards, would that also work?

Author:  master-awesome [ Fri Nov 14, 2014 5:57 pm ]
Post subject:  Re: I NEED HELP WITH 4.2 Practice Question 11!

This is how it is supposed to work.
Quote:
I type:
10
5
8


And then the user has a choice to ascend like so:

Quote:
5
8
10


or descend like so

Quote:
10
8
5

Author:  Tony [ Sat Nov 15, 2014 12:56 am ]
Post subject:  RE:I NEED HELP WITH 4.2 Practice Question 11!

Using a specific example is a good start.

Author:  master-awesome [ Sat Nov 15, 2014 11:11 am ]
Post subject:  Re: I NEED HELP WITH 4.2 Practice Question 11!

Okay so what happens is the user types in three numbers. For example:
Quote:
Please type in your numbers:
5
8
2


Now what happens is the user has a choice of deciding whether he/she wants it descending or ascending.

Quote:
So would you like your number ascending or descending. Type in D for descending or A for ascending!

If the user punches A then this will happen!
2
5
8


Quote:
If the user punches D then this will happen!

8
5
2

Author:  Insectoid [ Sat Nov 15, 2014 11:19 am ]
Post subject:  RE:I NEED HELP WITH 4.2 Practice Question 11!

We know what the assignment is asking for. Most of us did the same one when we were in high school. Tony is asking that *you* play around with a deck of cards. Take 3 cards in random order and sort them. The hard part is, like Tony said, figuring out exactly what you did to sort them in a way that a computer can understand. This can be done in as little as two if statements.

Author:  master-awesome [ Sat Nov 15, 2014 11:29 am ]
Post subject:  Re: I NEED HELP WITH 4.2 Practice Question 11!

I'm kind of lost here! I don't know how the deck of cards work. Could you show me an example here? Confused

Author:  Tony [ Sat Nov 15, 2014 3:34 pm ]
Post subject:  Re: I NEED HELP WITH 4.2 Practice Question 11!

master-awesome @ Sat Nov 15, 2014 11:29 am wrote:
I don't know how the deck of cards work.

It's just some pieces of paper with numbers on them. It could be helpful connection to "the real world", as you talk your way through the problem.

Posted Image, might have been reduced in size. Click Image to view fullscreen.

You keep on giving examples like going from 5,8,2 to 2,5,8 (ascending), but what were the steps in the middle? How did you know that the first number is a 2, instead of a 5 or an 8? This is the kind of very specific steps that you have to explain to a computer in code.

Author:  master-awesome [ Sat Nov 15, 2014 5:24 pm ]
Post subject:  Re: I NEED HELP WITH 4.2 Practice Question 11!

But how do you explain to the computer the code?

Do you need randint or is this if statements? Sad

Author:  Panphobia [ Sat Nov 15, 2014 11:31 pm ]
Post subject:  RE:I NEED HELP WITH 4.2 Practice Question 11!

If it is only 3 cards, if statements are enough but not the optimal solution. Imagine having 5 different cards in front of you in a random order. How would you sort them in descending order? What would be your first step? By the way, have you learned loops yet?

Author:  master-awesome [ Sun Nov 16, 2014 11:30 am ]
Post subject:  Re: I NEED HELP WITH 4.2 Practice Question 11!

How would loops apply here? Could you show me the code or something so I understand better?

Author:  Insectoid [ Sun Nov 16, 2014 4:20 pm ]
Post subject:  RE:I NEED HELP WITH 4.2 Practice Question 11!

You have to understand at all before you can understand better. I mean, you asked you need to use randint. Is there anything random in this program? This really shows that you haven't put any real thought into this at all.

Author:  master-awesome [ Mon Nov 17, 2014 12:55 pm ]
Post subject:  Re: I NEED HELP WITH 4.2 Practice Question 11!

Sad Well the way you guys talk about a deck of cards, it sounds confusing. Okay, I thought about it and how does this sound:

Quote:
var num1 : real
var num2 : real
var num3 : real
var answer : real

put "Type down your number: "..
get num1,num2,num3

if num1 < num2 and num2 < num3 then
put num1 and num2
else
put num2 and num3
end if


Something like that?

Author:  Insectoid [ Mon Nov 17, 2014 1:13 pm ]
Post subject:  RE:I NEED HELP WITH 4.2 Practice Question 11!

That's very close, you're just messing up the syntax and not covering all cases.

Let's do this step by step.

Let's say you have 3 numbers. Can you find and print the biggest number?

Author:  Tony [ Mon Nov 17, 2014 1:16 pm ]
Post subject:  Re: I NEED HELP WITH 4.2 Practice Question 11!

Try it out with some numbers you've suggested before.
master-awesome @ Sat Nov 15, 2014 11:11 am wrote:

Quote:
Please type in your numbers:
5
8
2


First, read through the program yourself and decide on what the program will output as a result. To be very clear -- think about what the code, as is, will produce, not necessarily what the assignment says it should (it's okay for this answer to be different, we just want to understand the current code really well). Once you have this answer written down, run the program with the same input, and note the difference in output, if any. If you and the computer disagree on the output, then some detail has been missed.

Finding such details move you closer to better understanding exactly what is going on, and makes it easier to move forward.

It could also help to start with smaller problems. E.g., output only 1 number (instead of all 3) -- the smallest number.

In fact, to get us started, lets try this exercise:
Quote:

Given 3 numbers -- 5, 8, 2 -- what is the smallest number? Why is it the smallest?


Edit: to keep consistent with Insectoid, I will also accept "what is the biggest number" with the same followup question of why is it the biggest?

And because OP hasn't been answering the leading questions before, I propose not giving out any further help until the the above why? question is answered. It might seem very silly, but it's very important to be able to actually put that answer in words.

Author:  master-awesome [ Sat Nov 22, 2014 7:27 pm ]
Post subject:  Re: I NEED HELP WITH 4.2 Practice Question 11!

I'll work on, but most important is you know my Rock, Paper, Scissors game! Is it possible if you could delete three of the first comments because what happen is I was publishing my work and then, all my data went missing so I was re-editing it to fix that problem, but then, somebody responded to my missing data message.

Author:  master-awesome [ Sun Nov 23, 2014 2:21 pm ]
Post subject:  Re: I NEED HELP WITH 4.2 Practice Question 11!

I think I finally figured it out, but could you double check just to be sure!

Very Happy Your method actually helped! I was getting really pissed when you brought up a deck of cards because it wasn't making sense. It would have helped though, if you showed this!

Quote:
if num1 < num2 and num2 < num3 and answer = "A" then
cls
put "Numbers Ascending!"
put num1
put num2
put num3


Then, I would have gotten it. I would have said more along the lines that "Oh so it is mathematical! Okay let me try!" Instead I was getting really confused and asked a classmate to show me an example of the code. After that, I just wrote it down here the example on this site to triple check that this is the method I use and after it was verified. I spent a lot of time today, trying to make the code agree with the output of the computer using your method of working 1 number at a time.

WARNING: Next time, show an example or something instead of saying, put a deck of cards. Maybe, it helps some people, but when somebody doesn't get it, use another method like showing an example.

Author:  Tony [ Sun Nov 23, 2014 7:16 pm ]
Post subject:  RE:I NEED HELP WITH 4.2 Practice Question 11!

Your solution probably works. At the very least it has all the possible orders. It might have problems if some of the numbers are the same (e.g., the input is "5,5,5")

re: "show me an example of the code" -- in this case an example is effectively a solution. It would certainly be easier to just copy/paste your way to an answer, but then you would have a similarly difficult time with the next new question.

I think a challenge with your code is in understanding some of the more complicated cases. E.g.
code:

if num1 > num2 and num1 < num3 and num2 < num1 and num3 > num1 and num2 < num3 and answer = "A" then

That is a lot of conditions to keep in mind at the same time. This is very easy for computers, but difficult for humans. A good strategy might be nested if statements (if statements inside of other if statements).

Now that you have some working code, it's appropriate to share examples Smile
code:

if num1 <= num2 and num1 <= num3 then
   % we know that num1 is the smallest value
   put num1

   % now we only have num2 and num3 left
   if num2 <= num3 then
      % num2 is smaller
      put num2
      put num3
   else
     % otherwise it must be the case that num3 is smaller
     put num3
     put num2
   end if

% num1 was not the smallest, see if it's num2
elsif num2 <= ...


Here we use IFs inside IFs to build out this decision tree. If you do much gaming, you can think of tech tree or skill tree as reference. We make simple decisions at first (what is the smallest number of the 3? (note that this was the hint question we've asked you to solve before)) and that leaves us with smaller problems (given that we already know what the smallest number is, how do we arrange the other 2?).

Author:  master-awesome [ Sun Nov 23, 2014 7:54 pm ]
Post subject:  Re: RE:I NEED HELP WITH 4.2 Practice Question 11!

Quote:
[quote="Tony @ Sun Nov 23, 2014 7:16 pm"]Your solution probably works. At the very least it has all the possible orders. It might have problems if some of the numbers are the same (e.g., the input is "5,5,5")


You are completely right about this!

I'm actually struggling with getting the 5,5,5 thing and all other same number and I'm getting confused! The teacher actually wants it with no elsif statement in the program, so I don't have a choice here! Shocked

Author:  Tony [ Sun Nov 23, 2014 9:32 pm ]
Post subject:  RE:I NEED HELP WITH 4.2 Practice Question 11!

Consider the example above with the use of <= (less then or equals). Basically if you are sorting two 5s, then it doesn't really matter which which of the 5s is "smaller", so you are free to take whichever is more convenient.

elsif requirement is rather arbitrary, but it's not needed to make the same approach works. E.g.
code:

if num = 1 then
   ...
elsif num = 2 then
  ...
end

is exactly the same* as
code:

if num1 = 1 then
  ...
end if

if num1 = 2 then
   ...
end if

* as long as the value of num1 doesn't change in between.

The advantage of the if-elsif-else (especially else) is that you'll end up in some decision. Any program can be implemented just the same without the elsif, but you have to make sure that your checks cover every possible scenario.


: