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

In summary, To convert a decimal number into binary, octal or hexadecimal, you can use the FLOOR and % operators in a while loop to repeatedly divide the number by the desired base and get the remainder. Then, the remainder values can be combined in reverse order to get the converted number.
  • #1
jackdamack10
17
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
  • #2
IF Jack is 5 and Jill is 4 THEN how do you decide which one is older?

I gave you a big hint.
 
  • #3
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)
 
  • #4
How about "Display" or "Print"
 
  • #5
Oh ok.. I remember now. Thank you. Is the method good though?
 
  • #6
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"
 
  • #7
Thank you. I guess this was more simple than I thought.
 
  • #8
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:
  • #9
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:

1. How do I approach solving a complex algorithm problem?

To solve a complex algorithm problem, it is important to break down the problem into smaller, more manageable tasks. Start by understanding the problem and its constraints, then brainstorm and plan your approach before diving into coding. It can also be helpful to discuss the problem with others or seek guidance from online resources.

2. What are some strategies for determining a person's age?

One strategy for determining a person's age is to ask for their date of birth. Another is to use an official document, such as a birth certificate or driver's license. In some cases, physical characteristics or milestones can also give an estimate of a person's age.

3. How can I compare two ages to determine who is older?

To compare two ages, you can use mathematical operations such as subtraction or greater than/less than comparisons. It may also be helpful to convert ages into a standardized unit, such as days or months, for easier comparison.

4. What are some potential challenges when solving a complex algorithm problem involving ages?

Some potential challenges when solving a complex algorithm problem involving ages may include handling outliers or invalid inputs, dealing with different date formats or time zones, and accounting for leap years. It is important to thoroughly test and debug your code to ensure it is accurate and robust.

5. Can you provide an example of an algorithm for determining who is older?

One possible algorithm for determining who is older could involve taking two ages as inputs, checking for invalid inputs or outliers, converting the ages into a standardized unit (such as days or months), and then comparing the two ages using mathematical operations or conditional statements. The algorithm could also include error handling and outputting the result in a clear format.

Similar threads

  • Programming and Computer Science
Replies
30
Views
4K
  • Precalculus Mathematics Homework Help
Replies
7
Views
866
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • Programming and Computer Science
Replies
23
Views
6K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top