• 0

Repetition Structures


Question

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?

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

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 :).

Link to comment
Share on other sites

  • 0

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  :)

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.