Sign in to follow this  
Followers 0
bs819

Repetition Structures

3 posts in this topic

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?

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.