This shows you the differences between two versions of the page.
— |
cs-142:gofundme [2015/05/14 11:57] (current) cs142ta created |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | =GoFundMe= | ||
+ | ==Problem== | ||
+ | * You have a friend with a cause trying to raise $100K | ||
+ | * (S)he needs a program to track donations and stop accepting donations when the goal is reached | ||
+ | * (S)he would like the following information at the end: | ||
+ | ** Total funds raised | ||
+ | ** Total number of donations | ||
+ | ** Average donation amount | ||
+ | ** Maximum donation amount | ||
+ | ** Minimum donation amount | ||
+ | * Follow the problem solving steps to design a solution | ||
+ | * Try simulating donations with a random number generator with values between $5 and $5000. | ||
+ | |||
+ | ==Solution== | ||
+ | <code cpp> | ||
+ | /* When your program doesn't have inputs, you should still find ways to test your code, perhaps by fixing certain values | ||
+ | Input: goal=100000, donations=-150, 150000 (negative donation) | ||
+ | Expected Output: total 149850 | ||
+ | Actual: | ||
+ | Input: goal=150, donations=155 (example of single donation exceeding goal) | ||
+ | Expected Output: total, avg, min, max all equal 150, 1 donation | ||
+ | Actual: | ||
+ | |||
+ | Input: goal=150, donations = 75, 75 (two donations that equal exactly my goal) | ||
+ | Expected Output: total = 150, avg = 75, min, max = 75, 2 donations | ||
+ | Actual: | ||
+ | |||
+ | Input: goal=150, donations=150 (example of single donation exactly meeting my goal) | ||
+ | Expected Output: total, avg, min, max all equal 150, 1 donation | ||
+ | Actual: | ||
+ | |||
+ | Input: goal=0 | ||
+ | Expected Output: program crashes | ||
+ | Actual: | ||
+ | |||
+ | */ | ||
+ | |||
+ | #include <iostream> | ||
+ | #include <iomanip> | ||
+ | #include <ctime> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | const double GOAL = 100000; | ||
+ | const double MAX_VAL = 5000; | ||
+ | const double MIN_VAL = 5; | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | cout << fixed << setprecision(2); | ||
+ | |||
+ | double total_donations = 0; | ||
+ | int count_of_donations = 0; | ||
+ | double max_donation_so_far = 0; // Pick a small enough value that you know it will be immediately replaced as the max value | ||
+ | double min_donation_so_far = 1000000000; // Pick a big enough value that you know it will be immediately replaced as the min value (MAX_VAL?) | ||
+ | |||
+ | srand(time(0)); | ||
+ | while (total_donations < GOAL) | ||
+ | { | ||
+ | // Record each donation (here it is generated as a random value) | ||
+ | double donation_amount = (rand() * 1.0 / RAND_MAX) * (MAX_VAL - MIN_VAL) + MIN_VAL; | ||
+ | cout << "Donation please: $" << donation_amount << endl; | ||
+ | |||
+ | // Add up donation as you go | ||
+ | total_donations = total_donations + donation_amount; | ||
+ | count_of_donations++; | ||
+ | |||
+ | // check if this is the max_donation_so_far | ||
+ | if (donation_amount > max_donation_so_far) | ||
+ | { | ||
+ | max_donation_so_far = donation_amount; | ||
+ | } | ||
+ | |||
+ | if (donation_amount < min_donation_so_far) | ||
+ | { | ||
+ | min_donation_so_far = donation_amount; | ||
+ | } | ||
+ | // Repeat until sum is above 100K | ||
+ | } | ||
+ | |||
+ | // Output the summary from the program | ||
+ | cout << "Congrats! You raised $" << total_donations << endl; | ||
+ | cout << "There were " << count_of_donations << " donations" << endl; | ||
+ | cout << "Average donation was $" << (total_donations / count_of_donations) << endl; | ||
+ | cout << "Max donation was $" << max_donation_so_far << endl; | ||
+ | cout << "Min donation was $" << min_donation_so_far << endl; | ||
+ | |||
+ | |||
+ | system("pause"); | ||
+ | return 0; | ||
+ | } | ||
+ | </code> |