0

I am working on writing the pseudocode for a calorie counting program that calculates the total weekly calories for breakfast. the program should allow the user to enter the number of calories consumed each day for 7 days, then report the number of breakfast calories consumed for the entire week.

My professor covered the topic of For Loops briefly, but didn't explain much about how to write them.

How would I write a For loop for the program above, and what would I delcare the variables of calories as?

So far I have a small piece written, but it's short and I don't know where to go with it, or if it needs anything additional.

Start

Declare BreakfastCalories[7] as an Array

For i = 1 to 7

  • Display "Enter breakfast calories dor day" i
  • Enter calories(I)

End For

Set total = day1 + day2 + day3 + day4 + day5 + day6 + day7

Display "Total breakfast calories consumed is", total

End

1 Answer 1

0

You seem to have understood well the feel of pseudocode and how it makes for a high-level description of a program's workflow. It should be noted that there's not a unique closed way of writing pseudocode, because these descriptions are mostly intended for humans to read and not for computers to interpret, so you may write your pseudocode with a certain degree of freedom or personal/team guidelines or preferences, provided it is quite readable to other developers.

I suggest this improvement to what you already worked out, so you can think about indexes and dive a little bit closer to what the actual variables and code in your program will finally look like:

initialize breakfastCalories array of size 7
initialize totalCalories to 0

for i = 1 to 7:
  prompt the user to input the calories taken for breakfast on day i
  set breakfastCalories[i] to the user's input
  add breakfastCalories[i] to totalCalories

display "The total calories taken for breakfast this week is: " + totalCalories

Be sure to also check out flowcharts and UML charts like activity diagrams to further improve your analytical skills and help you to code. I hope this helped!

Sign up to request clarification or add additional context in comments.

2 Comments

does initializing the variables at the beginning also suffice as declaring them, or must one still have "Declare real breakfastCalories" at the top?
Declaring a variable is creating a space in memory to hold a certain type of data, whereas initializing means giving it an initial value. It is common for programming languages to allow combining variable declaration and initialization in a single line of code as in my example. Readability, program logic and language specification will tell you how to work with initialization. In Java programming language, for example, every variable should be properly initialized... or Java will default-initialize them for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.