Results 1 to 25 of 25
Thread: C++ problem help please
-
2017-10-14, 07:53 PM (ISO 8601)
- Join Date
- Feb 2009
- Location
- A nice, sparkly place.
- Gender
C++ problem help please
I'm trying to figure out doing a problem in C++. Problem is, I haven't done C++ in over a year and I'm blanking on terminology and such. I have a general idea how to do the questions, but the specifics are blanking on me. Anyone want to help me out?
The question I'm trying to answer is:
In C++ - write code to print the following: The daily fine for an overdue library book is 10 cents for the first day and increases by 1 cent for each succeeding day – that is, 11 cents for the second day, 12 cents for the 3rd day and so on. The total fine is the sum of the daily fines. Given the user input of n days late, print out the number of days late along with the average daily fine.
-
2017-10-14, 08:15 PM (ISO 8601)
- Join Date
- Nov 2016
- Location
- SoCal
- Gender
Re: C++ problem help please
Off the top of my head and being extremely tired after recovering from surgery, I would say:
10N + ( (N * ( N - 1)) / 2)
-
2017-10-14, 08:27 PM (ISO 8601)
- Join Date
- Apr 2010
- Location
- Night Vale
- Gender
Re: C++ problem help please
Avatar by TheGiant
Long-form Sig
-
2017-10-14, 08:41 PM (ISO 8601)
- Join Date
- Feb 2009
- Location
- A nice, sparkly place.
- Gender
Re: C++ problem help please
Well, so far I have this:
#include<iostream>
using namespace std;
void main()
{
int n;
cout << "Number of days late :";
cin>>n;
for (
{
cout << "Your fine is " << (n + 1) << "cents."
}
Last edited by Silverraptor; 2017-10-14 at 08:42 PM.
-
2017-10-14, 08:43 PM (ISO 8601)
- Join Date
- Nov 2016
- Location
- SoCal
- Gender
Re: C++ problem help please
You don't need a for loop.
-
2017-10-14, 08:49 PM (ISO 8601)
- Join Date
- Apr 2010
- Location
- Night Vale
- Gender
Re: C++ problem help please
Code:cout << "Your fine is " << (9+n) << "cents."
Biggest correction I would make would be add a data scrubber, to check that n isn't 0 or negative. Simple if statement should be enough for that.
Secondary thing I would add is a check so that if the fine >= 100 cents it writes it as x dollars and y cents (substitute local currency as necessary).Last edited by Astral Avenger; 2017-10-14 at 08:52 PM.
Avatar by TheGiant
Long-form Sig
-
2017-10-14, 09:14 PM (ISO 8601)
- Join Date
- Feb 2009
- Location
- A nice, sparkly place.
- Gender
Re: C++ problem help please
So, if I make it instead:
Code:#include<iostream> using namespace std; void main() { int n; cout << "Number of days late :"; cin>>n; cout << "Your fine is " << (n + 9) << "cents." return 0; }
Having the first be "if (n + 9) < 100, << cout << "Your fine is " << (n + 9) << "cents."
Then the else would be along the lines of "if (n+9) > 100, ..." or something like that. Will I need to add another value, (like k)? Something for stored value for dollars where 1 "k" = 100 "n".Last edited by Silverraptor; 2017-10-14 at 09:15 PM.
-
2017-10-14, 09:27 PM (ISO 8601)
- Join Date
- Apr 2010
- Location
- Night Vale
- Gender
Re: C++ problem help please
That should do it for the first part of the problem unless i'm misreading it. Doubles are your friend with money (until you can write a class definition of your own and make it do what you want). I tend to program in functions, so I'd probably do something along the lines of:
Code:#include<iostream> using namespace std; void main() { int n; double fee; double fine(int n){ if(n<1){ return 0; } double fee=0.09; fee += .01*n; return fee; } cout << "Number of days late :"; cin>>n; fee = fine(n); cout << "Your fine is $" << fee <<"."; return 0; }
Avatar by TheGiant
Long-form Sig
-
2017-10-14, 10:17 PM (ISO 8601)
- Join Date
- Nov 2010
- Location
- California
- Gender
Re: C++ problem help please
I think you are misreading the problem. The daily fine increases by one cent a day, but the total fine is the sum of the daily fines.
So I think the fine calculator is more like:
Code:// Fine in cents. int fine(int days) { int total = 0; for (int day = 1; day <= days; ++day) { int daily_fine = 9 + day; total += daily_fine; } return total; }
Last edited by Sermil; 2017-10-14 at 10:18 PM.
-
2017-10-14, 10:51 PM (ISO 8601)
- Join Date
- Apr 2010
- Location
- Night Vale
- Gender
Re: C++ problem help please
I do believe you are correct, one of those things that seem super obvious in retrospect. I think you could work out the formula for the fine and not need a for loop, but given how trivial this loop is, you'd probably loose out on efficiency in taking the time to do that rather than let the processor run in O(n) rather than O(1).
Avatar by TheGiant
Long-form Sig
-
2017-10-14, 11:04 PM (ISO 8601)
- Join Date
- Feb 2009
- Location
- A nice, sparkly place.
- Gender
Re: C++ problem help please
What exactly does doubles do again? I'm also trying to refresh my memory on terminology in C++ as well.
Let me see if I completely understand this code. Under the function of 'fine' with the integer titled 'days', set the in value to 0 then with the for loop set with day starting at 1 and day less-than or equal to days, increasing the value of day (I think that's what ++day means). Then of course the integer 'daily_fine' is equal to 9 plus the value of day (or n) with the total equaling daily_fine? Did I say that correct? Sorry for asking trivial questions, but I am really rusty at C++ and trying to get back into the swing of it. (Also, why is total '+=' to the int 'daily_fine'?)
-
2017-10-14, 11:28 PM (ISO 8601)
- Join Date
- Apr 2010
- Location
- Night Vale
- Gender
Re: C++ problem help please
Doubles are one of the data types built into c++, they store numeric values but are not limited to integers. The name comes from using double the memory of an int.
The basic classes in c/c++ off the top of my head are int, double, bool (true/false), char (character), str (strings of characters, can reference parts of strings as a char array). Int also has unsigned int, which makes it able to handle bigger numbers than a normal int, but can't go negative, and long int, which doubles the maximum values int can store, but also doubles the memory it uses (distinct from doubles by still being limited to integer values).
I'll leave sermil to comment on their code, i think you got it right, but I'm reading it on my phone, so mistakes may have been missed.Avatar by TheGiant
Long-form Sig
-
2017-10-15, 01:12 AM (ISO 8601)
- Join Date
- Feb 2007
- Location
- Manchester, UK
- Gender
Re: C++ problem help please
No, the name comes from them being double precision as compared to "float", which is the other type of floating-point variable C and C++ support.
Have to say, I probably wouldn't use them for this problem, though, because floating-point precision errors are a thing and you add those errors with every addition you make--so if you enter a really large number of days you might end up with oddities like a fine of 1110.0000000034 cents*, or something like that. Less likely to happen with doubles as opposed to floats, admittedly, but I'd just store the cents value of the fine as an integer and then process that for display appropriately.
* Note: I haven't calculated to see if 1110 is an actual value you could get as a fine, I just used it as an example of a big number.
-
2017-10-15, 03:23 AM (ISO 8601)
- Join Date
- Aug 2005
- Location
- Mountain View, CA
- Gender
Re: C++ problem help please
Like 4X (aka Civilization-like) gaming? Know programming? Interested in game development? Take a look.
Avatar by Ceika.
Archives:
SpoilerSaberhagen's Twelve Swords, some homebrew artifacts for 3.5 (please comment)
Isstinen Tonche for ECL 74 playtesting.
Team Solars: Powergaming beyond your wildest imagining, without infinite loops or epic. Yes, the DM asked for it.
Arcane Swordsage: Making it actually work (homebrew)
-
2017-10-15, 03:56 AM (ISO 8601)
- Join Date
- Aug 2007
- Location
- Imagination Land
- Gender
Re: C++ problem help please
I don't have a lot to add to this thread since it's been pretty well covered by others. However, I just wanted to point out that you're missing an "if (n+9) == 100" case here in this cents to dollars conversion logic. You are only checking "(n+9) < 100" and "(n+9) > 100", but have nothing for when the fine is exactly one dollar.
I know we've already determined that the total fine isn't simply "n+9 cents", but you get the idea....
-
2017-10-15, 09:56 AM (ISO 8601)
- Join Date
- Feb 2007
- Location
- Manchester, UK
- Gender
-
2017-10-15, 11:30 AM (ISO 8601)
- Join Date
- Nov 2016
- Location
- SoCal
- Gender
Re: C++ problem help please
You are correct.
Day: 0, total fine: 0, AvgDailyFine: 0
Day: 1, total fine: 10, AvgDailyFine: 10
Day: 2, total fine: 21, AvgDailyFine: 10.5
Day: 3, total fine: 33, AvgDailyFine: 11
Day: 4, total fine: 46, AvgDailyFine: 11.5
Day: 5, total fine: 60, AvgDailyFine: 12
Day: 6, total fine: 75, AvgDailyFine: 12.5
Day: 7, total fine: 91, AvgDailyFine: 13
Day: 8, total fine: 108, AvgDailyFine: 13.5
Day: 9, total fine: 126, AvgDailyFine: 14
Day: 10, total fine: 145, AvgDailyFine: 14.5
Day: 11, total fine: 165, AvgDailyFine: 15
Day: 12, total fine: 186, AvgDailyFine: 15.5
Day: 13, total fine: 208, AvgDailyFine: 16
Day: 14, total fine: 231, AvgDailyFine: 16.5
Day: 15, total fine: 255, AvgDailyFine: 17
Day: 16, total fine: 280, AvgDailyFine: 17.5
Day: 17, total fine: 306, AvgDailyFine: 18
Day: 18, total fine: 333, AvgDailyFine: 18.5
Day: 19, total fine: 361, AvgDailyFine: 19
-
2017-10-15, 02:37 PM (ISO 8601)
- Join Date
- Feb 2009
- Location
- A nice, sparkly place.
- Gender
Re: C++ problem help please
I just want to thank everyone so much for there help. Given how rusty I am with coding after not doing it for a while, every bit of information helped me out immensely.
-
2017-10-15, 04:29 PM (ISO 8601)
- Join Date
- Oct 2009
- Location
- In my library
Re: C++ problem help please
I know you've got your answer now, but here's how I'd do it. I'd start with your code.
#include<iostream>
using namespace std;
void main()
{
int n;
cout << "Number of days late :";
cin>>n;
for (
{
cout << "Your fine is " << (n + 1) << "cents."
}
Code:#include<iostream> using namespace std; void main() { while (true) { int dayslate; cout << "Number of days late :\n"; cin>>dayslate; float fine = 0.1; int count = 1; float totalfine = 0; while (count <= dayslate){ totalfine = totalfine + fine; count++ fine = fine + 0.01; } cout << "Total fine: $" << totalfine << "\n"; cout << "Please input data for next fine.\n"; } return 0; }
As a side note, I've come to hate it when single character variable names are used. I know some are traditional, especially i for loop durations and n for integers, but if you make your variable names meaningful then they're easier to track and you have to comment your code less.
Also, I used the while loop so you can get fine after fine, if I was actually writing this code as a program I'd personally have made this a function called by inputting the correct keyword (likely "fine" in this case), and made the main function a while loop holding 'cout << "please enter function.\n"; cin >> keyword;' and then a switch or if-elseif statement that takes the keyword and calls the correct function (or just says 'return 0;' if told to exit). But that's outside the scope of this problem.
Oh, and always end your couts with "\n" unless you're going to append something to the same line. It's just nicer while testing.
-
2017-10-19, 05:03 PM (ISO 8601)
- Join Date
- Aug 2007
- Location
- Imagination Land
- Gender
Re: C++ problem help please
You should still be checking for that case, because your "display more than 99 cents as x dollars and y cents" function should be agnostic about what kind of input it will be receiving. It's just a best practices thing.
Similarly, you should also check for when the leftover cents are less than 10 so that you can add in the floating 0. Otherwise, two dollars and five cents will display as $2.5, which looks pretty confusing.Last edited by KillianHawkeye; 2017-10-19 at 05:04 PM.
-
2017-10-24, 11:09 AM (ISO 8601)
- Join Date
- Aug 2010
-
2017-10-24, 04:42 PM (ISO 8601)
- Join Date
- Aug 2008
Re: C++ problem help please
It's not a particularly difficult formula, and it's one calculation instead of one per loop. There's no reason to use a for loop here, it's a simple arithmetic series. The easiest depiction of the formula (plus the average) would be
Code:$tot=n(19+n)/200 $avg=.095+n/200
Also, technically speaking it looks like there's no need to ever calculate the total fine in the problem statement.I would really like to see a game made by Obryn, Kurald Galain, and Knaight from these forums.
I'm not joking one bit. I would buy the hell out of that. -- ChubbyRain
Current Design Project: Legacy, a game of masters and apprentices for two players and a GM.
-
2017-10-25, 11:11 AM (ISO 8601)
- Join Date
- Feb 2010
- Location
- UK
- Gender
Re: C++ problem help please
Don't do this! std::endl; also does a flush, which is expensive (six times more expensive than '\n') and unnecessary. This was the subject of a lightning talk (3:13) at this year's CppCon, but represents the recent wisdom in C++ for some time now: https://stackoverflow.com/questions/...always-be-used
-
2017-10-29, 12:37 PM (ISO 8601)
- Join Date
- Oct 2009
- Location
- In my library
Re: C++ problem help please
Eh, if given the choice I still go for good old printf because it's what I learnt first and I find it more powerful (although I need to brush up on the formatting notation again). I'll also default to scanf because I believe that you should always have to have your data type in mind when accepting input, and I won't 90% if I'm using cin.
I once used stdio.h in my university C++ course exam because I'm more comfortable with it.
-
2017-11-01, 07:20 PM (ISO 8601)
- Join Date
- Jul 2016
Re: C++ problem help please
check out my D&D-inspired video game, not done yet but you can listen to the soundtrack if you're bored: https://www.facebook.com/TheCityofScales/
my game's soundcloud: https://soundcloud.com/user-77807407...les-soundtrack
my website with homebrew and stuff on it: http://garm230.wixsite.com/scales