296,759 views
38 votes
38 votes
Coding problem! please check my work!

test information:

Validate the input as follows:

Make sure the numbers of fat grams and calories aren’t less than 0.
Ensure that the number of calories entered isn’t greater than fat grams x 9.
Once correct data has been entered, the program should calculate and display the percentage of calories that come from fat. Use the following formula:

Percentage of calories from fat = (Fat grams X 9) / calories

My coding:

// Start

Module Main ()

Declare Real fatcalories

//Get the number of fat grams
Display "Enter the number of fat grams."
Input fat grams

While fatgrams > 0
Display "Error: the number of fat grams cannot be less than 0"
Display "Please enter the correct number of fat grams"
Input fat grams
End while

//Get the number of calories
Display "Enter calories"
Input calories

While calories < fatgrams*9
Display "Error: the number cannot be more than 9 times the fat grams"
Display "Please enter the correct number of calories"
Input calories
End while

Call calculatedSum
End Module

//Get the percentage
Module calculatedSum
Set fatCalories=(fat grams*9) / calories
If fatcalories < 0.3 Then
Display "This food is low in fat"
Else
Set fatCalories = False
End if

End Module

User Brown Limie
by
2.7k points

1 Answer

12 votes
12 votes

Answer:

There are a few issues with your code.

In the declaration of fatcalories, you have written Declare Real fatcalories. However, Declare is not a valid keyword in any programming language that I'm aware of. You can simply declare a variable by writing its type and name, like Real fatcalories.

In the While loop that checks for a negative number of fat grams, you are using the variable fatgrams instead of fatGrams. Variable names are case-sensitive, so fatGrams and fatgrams are treated as two different variables. Make sure to use the correct variable name throughout your code.

In the calculatedSum module, you are using fat grams instead of fatGrams. Again, make sure to use the correct variable name.

In the calculatedSum module, you are setting the value of fatCalories to a boolean value (True or False) depending on whether the percentage of calories from fat is less than 0.3. However, fatCalories is a Real type variable, so you cannot assign a boolean value to it. Instead, you can use an If statement to display a message based on the value of fatCalories.

Here's how you can fix these issues and improve your code:

Module Main ()

Real fatGrams

Real calories

Real fatCalories

//Get the number of fat grams

Display "Enter the number of fat grams."

Input fatGrams

While fatGrams < 0

Display "Error: the number of fat grams cannot be less than 0"

Display "Please enter the correct number of fat grams"

Input fatGrams

End while

//Get the number of calories

Display "Enter calories"

Input calories

While calories > fatGrams * 9

Display "Error: the number of calories cannot be more than 9 times the fat grams"

Display "Please enter the correct number of calories"

Input calories

End while

Call calculatedSum

End Module

//Get the percentage

Module calculatedSum

Set fatCalories = (fatGrams * 9) / calories

If fatCalories < 0.3 Then

Display "This food is low in fat"

Else

Display "This food is not low in fat"

End if

End Module

Step-by-step explanation:

User Paul Bruno
by
3.4k points