# Algorithm problem ()

jackdamack10
Hi there. I have two algorithm problems.
I was wondering if you can check my answers for the first one and help me get a start with the second one:

1)Write an algorithm to compute the average of 5 numbers

Name: Ave5
Given: X1, X2, X3, X4, X5
Change: none
Intermediate: Total
Result: Average
Definition: Average:= Ave5 (X1, X2,X3,X4,X5)

METHOD

Get X1
Get X2
Get X3
Get X4
Get X5

Total = X1 + X2 + X3 + X4 + X5
Average = Total/5

Give Average

2) Write an algorithm to get the names and ages of two people. Return the name of the person who is older (in format "x is older than y", where x and y are the names of the two people), unless the two people are the same age, in which case, return the message "x is the same age as y.

I'm having trouble writing an algorithm for this particular case. It's more complicated than what I am used to. Can someone give me an idea on how to start a problem like this one ? I have made a couple of assumptions:

Name: PerAge
Givens: X, Y
Change: X,Y
Intermediate: Age
Result: none
Definition: Age =: PerAge (X,Y)

METHOD

Give X
Give Y

Well... I'm pretty lost at this point

## Answers and Replies

Staff Emeritus
IF Jack is 5 and Jill is 4 THEN how do you decide which one is older?

I gave you a big hint.

jackdamack10
Name: AgName
Given: X,Y
Change: X,Y
Result: Oldest
Intermediate: OldestSoFar
Definition: Age=agName(X,Y)

Method

Give X
Give Y

If (X>Y)
OldestsoFar = X

Else
OldestsoFar=Y

Oldest =O ldestSoFar

Give Oldest

What I don’t understand is how to put messages up (like “x is the oldest)

Staff Emeritus
How about "Display" or "Print"

jackdamack10
Oh ok.. I remember now. Thank you. Is the method good though?

Staff Emeritus
Why don't you just do:

If (X>Y)
Display "X is older than Y"
Else if(X<Y)
Display "Y is older than X"
Else
Display "X is the same age as Y"

jackdamack10
Thank you. I guess this was more simple than I thought.

jackdamack10
One last question...
A Teller enters his opening balance and then a number of transactions. Deposits are entered as a positive numbers and withdrawals are entered as a negative numbers. He also needs to calculate the number o deposits and the number of withdrawals. The teller indicates that all transactions have been entered by entering a transaction with a value of 0.
Develop an algorithm to determine the closing balance for a teller.

This is what I found:

Given: Withdrawals, Deposits
Result : Balance
Intermediate: Value, Count
METHOD:
Count = 0
Value = 0
IF (Transaction >0)
Value = Transaction
Value = Deposits
Else
Value = (-1) (Transaction)
Value= Withdrawals
Finnish Loop
Balance = Deposits + Transactions
Give Balance

Does the method look ok?

Last edited:
I don't see a beginning to your loop.
You should use a while loop, maybe:
Code:
nOfDeposits = 0
nOfWithdrawals = 0
balance = 0
WHILE transactions.getNext() != 0 DO
IF curTransaction > 0 THEN
nOfDeposits++
ELSE
nOfWithdrawals++
END IF
balance += curTransaction
LOOP
OUTPUT("Number of deposits: "+nOfDeposits)
OUTPUT("Number of withdrawals "+nOfWithDrawals)
OUTPUT("Your balance is: "+balance)
Also if you don't mind my asking, what class is this for?

Last edited:
Staff Emeritus
jackdamack10, your sudo code doesn't make any sense.

1) You should have only two givens: number of transaction and some type of deposit or withdrawl.
2) The only intermediate value you need is the total
3) The result should be the total
4) Where does your loop begin?
5) The loop will ofcourse loop for as many transactions you have.

There is more wrong with code and I suggest you think about the problem some more.

EvaBugs
Hi.

Since "Jackdamack" already started a thread about algorithm, I'm going to put my question here too.

Develop an algorithm to covert a decimal number into binary, octal or hexadecimal. The algorithm should ask the user to enter a decimal number. The algorithm then should ask the user what number system he/she wants to convert to (3 options; binary, octal or hexadecimal). If the user enters an illegal option, the algorithm should repeat the question over until the user enters a legal option. The algorithm then should process the request and give the result.

Given: N
change: none
Intermediate: none

I really do not know how to start this particular problem. I read through this thread and through my notes, but I'm not sure how to do a "conversion". I'm not asking for the entire solution (because I know you can't do that here), but can someone give me a hint on how to start this algorithm
Thank you

Staff Emeritus
Take any number NUMBER and any base BASE. In order to convert bases you would do this:

FLOOR(NUMBER/BASE) = N1 , NUMBER%BASE = R1
FLOOR(N1/BASE) = N2 , N1%BASE = R2
FLOOR(N2/BASE) = N3 , N2%BASE = R3
...
Continue going until N* is 0. Then the number your looking for is the reverse order of the R* values. ie. R4 R3 R2 R1

The FLOOR operator is just a way to get the integer portion. ie. 5/2 = 2.5 , FLOOR(5/2) = 2. The INT operator is also acceptable.
The % operator or modulus is the way to get the remainder.

Here is an example of converting 5 into binary:

INT(5/2) = 2 = N1 , 5%2 = 1 = R1
INT(2/2) = 1 = N2 , 2%2 = 0 = R2
INT(1/2) = 0 = N3 , 1%2 = 1 = R3

You answer is R3 R2 R1 = 1 0 1

To confirm this answer use you can do this: 1*2^2 + 0*2^1 + 1*2^1 = 5

Last edited: