inefficient code C++ problem

Hi guys this is my first post, i just got done programming this code for my class and when i showed it to my professor his only comment was that "it could be MUCH more efficient"


i do apologize for posting the entire code, but i want to see why it is not efficient.

thank you!

Harvey

Last edited on
The following code is pointless:
1
2
3
double foodave;
foodave = 0;
foodave = foodtot/7;

the obvious solution would be:
double foodave = foodtot/7.0;

If you want anyone to read the rest of the code, you need to edit your post to use code tags.
Harvey, You're right that it runs & that's 90 ++ percent of the solution.
However your instructor is quite correct that it is not well thought out. I would venture to say that it was typed-in as you thought through the problem.
I see your design is (1) headers, (2) Main(), (3) Input , & (4) Averages.

INPUT() & AVERAGE() can both be shortened into just a few lines by re-working them with loops and more general-type questions/input.

I'm not even going to ask what that int I var is all about, but it is clearly NOT usually the way that is best to use a var such as that.
In fact, the entire INPUT() subrt'n should only be about 3-5 lines.
One way to start is to combine all of those user prompts into a single line with only the key word changing at each iteration.

Please enter the amount you spend on ????????? for each day this week:
Then change ????? for "school", "food", clothes" etc each time within the loop.

The same is also trru for the AVERAGE() subrt'n
" you spent $" << clothesave << " on average this week for clothes! |"

" you spent $" << ???????? << " on average this week for ?????????! |"
One line, a few well placed and well defined VARs and this subrt'n too can be written in a handful of lines rather than a page of lines.

Remember, too, that you can TYPE & INIT a VAR on the same line.
double d_FoodAve =0, d_ClothesAve=0, d_SchoolAve=0;

Also remember that you CAN place the TOTALS into a single 1-D array
of doubles to be passed to AVERAGE() rather than passing a full list of vars.
If you change the order of ARGs in AVERAGE() then you will need to correct the line-up and re-compile, but by placing those ARGs into an array of doubles then any future changes to the TOTALS or their position will be nominal.



In short Harvey, go back to your DESIGN, and look at what each subrt'n is suppose to do and how each subrt'n can be made to be concise & compact.


Last edited on
thank you guys, that helps a lot. yes, i will go back and look it all over and see how much i can simplify. incis B you are right i did this as i was going which is why it might have come out very inefficient.

thanks again for the helps,

Harvey
Topic archived. No new replies allowed.