I have been confused on how to write algorithms from day one! I am currently taking an online class and am so lost with this topic. We have an assignment due in a week where we have to write an algorithm for a certain problem and then create a program for it. Here are my problems:
1. Write a program to calculate the change in quarters, dimes, nickels, and pennies when any amount from 1 to 99 is entered. The program should return the minimum coins that represent the amount entered. For example 37 cents should yield one quarter, 1 dime, and two pennies.
2. Write a program to find the smallest number, the largest number, and the average of any five numbers entered by the user. For example, if the five numbers entered are 1, 2, 3, 4, and 5, the answers should be smallest =1, largest=2, and average=3. (Your program should handle any numbers from 1 to 1,000)
So far what I have for the first one is:
cin>>inputCoins
while(inputCoins > 0)
{
if(inputCoins >= 25)
Quarters ++
inputCoins -= 25
elseif(inputCoins >= 10)
Dimes ++
inputCoins -= 10
elseif(inputCoins >=5)
Nickles ++
inputCoins -= 5
elseif(inputCoins >=1)
Pennies ++
inputCoins -= 1
}
cout>> //print your coin totals out here.
Right now, if the amount is higher than 25, the number of quarters goes up by one, and the amount is decreased by 25, the value of a quarter. So far, so good.
Now you move on to dimes. You say that if it's higher than or equal to 10, you up the number of dimes, and then decrease the amount by 10. Here's where I think an issue may be. Would you not need to say where the amount is greater than 10, but less than 25?
For instance: you enter 26.
For this, Quarters++, but ALSO, Dimes++ because it is greater than 10 as well as greater than 26.
For problem #2, I'm rusty on my C++ (it's been a few years). I believe what you would do is to assign each user-entered value to a variable as it is entered. You then need to check each variable to make sure it fits the parameters (i.e. has the person entered all five, is it between 1-1000). Now, just work bit by bit.
To find out which number is the smallest, create a variable called "Smallest". If variable 1 < variable 2 AND variable 1 < variable 3, etc, then Smallest = Variable 1. The opposite works for the largest.
As for the average, variable 1 + variable 2 + variable 3 + variable 4 + variable 5 divided by 5 will give you the solution.
Attention: NOTHING on this site may be reproduced in any fashion whatsoever without explicit consent (in writing) of the owner of said material, unless otherwise stated on the page where the content originated. Search engines are free to index and cache our content. Users who post their account names or personal information in their questions have no expectation of privacy beyond that point for anything they disclose. Questions are otherwise considered anonymous to the general public.