Jump to content



Photo

Repetition Structures


  • Please log in to reply
2 replies to this topic

#1 bs819

bs819

    Neowinian

  • Joined: 16-September 13

Posted 30 September 2013 - 12:12

Hi, I'm learning just general pseudocode in my class this semester. I'm having trouble understanding some of the repetition structures in the book. 

 

The instructions for one particular problem tell me to calculate how much money is earned if the salary is one penny the first day and it doubles each day after that. I'm having trouble writing the statements that will calculate the salary. It also tells me to display the dollar amount, not the number of pennies. I'm not even asking for the answer, just some kind of push in the right direction at least. 

 

 

Declare Real days, counter, salary, pennies

//Get upper limit

Display "Calculate earnings over how many days?"

Input days

 

//Display table headers

Display "Day", Tab, "Salary"

Display "------------------"

 

//Calculate & display salary

For counter = 1 to days

    Having trouble with the calculation!!!

    Display counter, Tab, salary

End For

 

I know I need a formula that doubles the amount. Would the amount start with 0.01?

 

Set pennies = 0.01

 

//Display and calculate

For counter = 1 to days

    Set salary = pennies

    Set pennies = pennies * 2

    Display counter, Tab, salary

End For

 

Is that formula close?




#2 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 8
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 30 September 2013 - 16:37

The way you describe it, it sounds like you're on the right track. Couple of things that might be useful to you:

If you change the order of the statements inside the loop ("repetition structure"), you don't need the pennies variable:
 
// Day 1 salary is 1 penny.
Set salary = 1

// Display the salary, and then double the salary.
For counter = 1 to days
    Display counter, tab, salary
    Set salary = salary * 2
End
 

For calculating the salary in dollars, instead of pennies, you can simply divide the salary by 100 to get the result in dollars.

199 pennies / 100 = 1.99 dollars!

 

Looking at the way you describe the problem, it sounds like you're also being asked to keep a track of the TOTAL amount of money earned in the specified number of days too, so you're probably going to need another variable to keep a running total each time you go round the loop :).

#3 OP bs819

bs819

    Neowinian

  • Joined: 16-September 13

Posted 30 September 2013 - 19:13

The way you describe it, it sounds like you're on the right track. Couple of things that might be useful to you:

If you change the order of the statements inside the loop ("repetition structure"), you don't need the pennies variable:
 

// Day 1 salary is 1 penny.
Set salary = 1

// Display the salary, and then double the salary.
For counter = 1 to days
    Display counter, tab, salary
    Set salary = salary * 2
End
 

For calculating the salary in dollars, instead of pennies, you can simply divide the salary by 100 to get the result in dollars.

199 pennies / 100 = 1.99 dollars!

 

Looking at the way you describe the problem, it sounds like you're also being asked to keep a track of the TOTAL amount of money earned in the specified number of days too, so you're probably going to need another variable to keep a running total each time you go round the loop :).

 

Thank you! You're right, I don't have to use the pennies variable! And thanks for the tip about the running total  :)