Solving a Complex Algorithm Problem: Get Person's Ages and Determine Who's Older

  • Thread starter Thread starter jackdamack10
  • Start date Start date
  • Tags Tags
    Algorithm Complex
AI Thread Summary
The discussion revolves around solving algorithm problems, starting with computing the average of five numbers, where a method to sum the inputs and divide by five is proposed. The second problem involves determining the older person between two individuals based on their ages, with suggestions on using conditional statements to display the result. A third algorithm focuses on calculating a teller's closing balance from a series of transactions, emphasizing the need for a loop to process inputs until a zero transaction is entered. Additionally, there is a request for guidance on converting decimal numbers into binary, octal, or hexadecimal formats, with hints provided on using division and modulus operations for base conversion. Overall, the conversation highlights various algorithmic approaches and problem-solving techniques in programming.
jackdamack10
Messages
17
Reaction score
0
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

Thank you advance
 
Technology news on Phys.org
IF Jack is 5 and Jill is 4 THEN how do you decide which one is older?

I gave you a big hint.
 
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)
 
How about "Display" or "Print"
 
Oh ok.. I remember now. Thank you. Is the method good though?
 
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"
 
Thank you. I guess this was more simple than I thought.
 
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:
  • #10
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.
 
  • #11
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
 
  • #12
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:
Back
Top