PDA

View Full Version : C++ problem help please



Silverraptor
2017-10-14, 07:53 PM
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.

Thanks in advance.:smallsmile:

FreddyNoNose
2017-10-14, 08:15 PM
Off the top of my head and being extremely tired after recovering from surgery, I would say:

10N + ( (N * ( N - 1)) / 2)

Astral Avenger
2017-10-14, 08:27 PM
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.
Thanks in advance.:smallsmile:

What specifically are you having trouble with? User input, algorithm design, function definition, printing? It's hard to help without feeling like I'm doing homework for you with that broad of a question.

Silverraptor
2017-10-14, 08:41 PM
What specifically are you having trouble with? User input, algorithm design, function definition, printing? It's hard to help without feeling like I'm doing homework for you with that broad of a question.

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."
}



I know it's not much, and probably a step in the wrong direction somewhere, but that's as much as I remember. I'm trying to figure out how to get the value of input 'n' output 10+1n. And I'm drawing a blank on it.

FreddyNoNose
2017-10-14, 08:43 PM
You don't need a for loop.

Astral Avenger
2017-10-14, 08:49 PM
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."
}
I know it's not much, and probably a step in the wrong direction somewhere, but that's as much as I remember. I'm trying to figure out how to get the value of input 'n' output 10+1n. And I'm drawing a blank on it.


cout << "Your fine is " << (9+n) << "cents."

My reading of the problem is $0.10 on day 1 and 1 additional cent every subsequent day, so 9+n = 10c on first day, 11c on second, 12c on third, etc.
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).

Silverraptor
2017-10-14, 09:14 PM
So, if I make it instead:


#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;
}


Would that do it? And if so, I'm trying to think about how to properly make the check you mentioned. You're right, I never considered the dollar conversion for 100 cents. Probably will need to have an if/else statement.

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".

Astral Avenger
2017-10-14, 09:27 PM
So, if I make it instead:
*snip*
Would that do it? And if so, I'm trying to think about how to properly make the check you mentioned. You're right, I never considered the dollar conversion for 100 cents. Probably will need to have an if/else statement.

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".
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:


#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;
}

Sermil
2017-10-14, 10:17 PM
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:



// 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;
}


And the average daily file is total fine / days.

Astral Avenger
2017-10-14, 10:51 PM
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:



// 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;
}


And the average daily file is total fine / days.

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).

Silverraptor
2017-10-14, 11:04 PM
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:


#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;
}


What exactly does doubles do again? I'm also trying to refresh my memory on terminology in C++ as well.


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:



// 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;
}


And the average daily file is total fine / days.

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'?)

Astral Avenger
2017-10-14, 11:28 PM
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'?)
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.

factotum
2017-10-15, 01:12 AM
The name comes from using double the memory of an int.


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.

Douglas
2017-10-15, 03:23 AM
(Also, why is total '+=' to the int 'daily_fine'?)
"+=" means "increase by". It's (almost) equivalent to "total = total + daily_fine". The difference is a technical detail that's rarely relevant.

KillianHawkeye
2017-10-15, 03:56 AM
You're right, I never considered the dollar conversion for 100 cents. Probably will need to have an if/else statement.

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.

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....

factotum
2017-10-15, 09:56 AM
You are only checking "(n+9) < 100" and "(n+9) > 100", but have nothing for when the fine is exactly one dollar.


I don't think it's actually possible for the fine to be exactly a dollar? According to my calculations it jumps straight from 91 cents to $1.08.

FreddyNoNose
2017-10-15, 11:30 AM
I don't think it's actually possible for the fine to be exactly a dollar? According to my calculations it jumps straight from 91 cents to $1.08.

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

Silverraptor
2017-10-15, 02:37 PM
I just want to thank everyone so much for there help.:smallsmile: Given how rusty I am with coding after not doing it for a while, every bit of information helped me out immensely.

Anonymouswizard
2017-10-15, 04:29 PM
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."
}

Up to there, and change it so it looks like this.


#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;
}


I know I probably should set the length of the decimal part of the output, but I'm tired.

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.

KillianHawkeye
2017-10-19, 05:03 PM
I don't think it's actually possible for the fine to be exactly a dollar? According to my calculations it jumps straight from 91 cents to $1.08.

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.

kyoryu
2017-10-24, 11:09 AM
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.

Nitpick: Use std::endl rather than '\n' (presuming you're using STL, which I am considering you said for cout).

Knaight
2017-10-24, 04:42 PM
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).

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

$tot=n(19+n)/200
$avg=.095+n/200
although directly using the dollar sign as a variable is a bit unorthodox and really shouldn't make its way into a program. If you want a display in cents for low values that's another few lines, but this is about a five line program in something like MATLAB.

Also, technically speaking it looks like there's no need to ever calculate the total fine in the problem statement.

Capt Spanner
2017-10-25, 11:11 AM
Nitpick: Use std::endl rather than '\n' (presuming you're using STL, which I am considering you said for cout).

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/35580919/should-stdendl-always-be-used


https://www.youtube.com/watch?v=6WeEMlmrfOI

Anonymouswizard
2017-10-29, 12:37 PM
Nitpick: Use std::endl rather than '\n' (presuming you're using STL, which I am considering you said for cout).

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.

Goodkill
2017-11-01, 07:20 PM
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/35580919/should-stdendl-always-be-used


wow, i've been coding in c++ for awhile and i had no idea about that! just been using endl like it's synonymous with \n. this mad science forum has some interesting threads and posts!