229k views
4 votes
Makayla is developing software for an animal shelter. After some research, she comes up with this algorithm for calculating the number of daily calories required for a dog: Calculate the Resting Energy Requirement (RER) by raising the dog's weight to the 3/4 power and multiplying the result by 70 If the dog is neutered, multiply RER by 1.6 Otherwise, multiply RER by 1.8 After verifying the algorithm comes up with a reasonable number for a single dog, she decides to extend it to compute the total calories needed for all the dogs in the shelter each day. What structure must be added to the original algorithm so that it can compute the calories for multiple dogs?

User SOfanatic
by
8.1k points

1 Answer

1 vote

To extend the original algorithm to compute the total calories needed for multiple dogs in the shelter each day, Makayla should add a loop structure to iterate through the list of dogs.

Here are the steps to modify the algorithm:

1. Create a Data Structure for Storing Dog Information:

- Define a data structure (e.g., a list, array, or dictionary) to store information about each dog, including their weight and neuter status.

2. Initialize a Variable for Total Calories:

- Before the loop, initialize a variable (e.g., `totalCalories`) to keep track of the total calories needed for all the dogs. Set it to 0.

3. Iterate Through the List of Dogs:

- Use a loop (e.g., a `for` loop or a `while` loop) to iterate through the list of dogs one by one.

4. Within the Loop, Calculate Calories for Each Dog:

- For each dog in the list, perform the following calculations:

- Calculate the Resting Energy Requirement (RER) by raising the dog's weight to the 3/4 power and multiplying the result by 70.

- Check if the dog is neutered. If neutered, multiply RER by 1.6; otherwise, multiply it by 1.8.

- Add the calculated calories for the current dog to the `totalCalories` variable.

5. Repeat Steps 3-4 for Each Dog:

- Continue the loop until all dogs in the list have been processed.

6. After the Loop, Display or Use the Total Calories:

- Once the loop is complete, you will have the total calories needed for all the dogs in the shelter stored in the `totalCalories` variable.

- You can then display the total calories, use them for further calculations, or perform any necessary reporting.

By adding this loop structure to the original algorithm, Makayla can calculate the total calories needed for multiple dogs in the shelter each day efficiently. The loop allows her to iterate through the list of dogs and calculate calories for each dog individually before summing them up to obtain the total.

User Gbegley
by
7.8k points