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
Click For Summary

Discussion Overview

The discussion revolves around solving algorithm problems, specifically focusing on determining the ages of two individuals and converting numbers between different bases. Participants share their approaches to writing algorithms, seek clarification on methods, and provide feedback on each other's proposed solutions.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant presents an algorithm to compute the average of five numbers and seeks help with a second algorithm to determine which of two people is older based on their ages.
  • Another participant questions how to decide who is older between two individuals, hinting at the need for a comparison.
  • A different participant proposes a method for determining the older person but expresses confusion about displaying messages.
  • Suggestions are made regarding the use of "Display" or "Print" for outputting messages.
  • One participant simplifies the logic for determining who is older, suggesting a more straightforward approach with conditional statements.
  • Another participant introduces a new algorithm problem involving a teller's transactions, outlining a method to calculate the closing balance.
  • Feedback is given on the teller algorithm, with suggestions for using a loop and clarifying the structure of the code.
  • A participant raises a new question about converting a decimal number into binary, octal, or hexadecimal, expressing uncertainty about how to start the algorithm.
  • Another participant provides a detailed explanation of the conversion process, including the use of the FLOOR and modulus operators.

Areas of Agreement / Disagreement

Participants express varying levels of understanding and approaches to the algorithm problems, with some agreeing on methods while others provide alternative suggestions. The discussion remains unresolved regarding the best practices for algorithm design and implementation.

Contextual Notes

Some participants' algorithms lack clear beginnings for loops, and there are unresolved questions about the structure and logic of proposed solutions. Assumptions about inputs and outputs are not consistently defined.

Who May Find This Useful

This discussion may be useful for individuals interested in algorithm design, programming logic, and problem-solving in computational contexts.

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:

Similar threads

  • · Replies 30 ·
2
Replies
30
Views
7K
Replies
7
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 32 ·
2
Replies
32
Views
4K
Replies
5
Views
4K
  • · Replies 23 ·
Replies
23
Views
6K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K