+ 1
How can i make my program as short and as efficient as possible?
My homework is to make a program, in which you enter a sum and it assigns 5$, 2$ and 1$ bills using the least ammount of bills as possible. https://code.sololearn.com/c7nBW3pfCsF3/?ref=app
4 ответов
+ 4
-The IF statements :
You have written 2 IF statements in your program but haven't specified the braces or the code that will be executed if the IF comes TRUE.
Ex :
Original - if(c > 2);
int d = (c - c % 2) % 2;
cout ..... endl;
Modified - if(c > 2) {
int d = (c - c % 2) / 2;
cout ...... endl;
}
- The printing pattern :
Having the values printed correctly works as a debugger and solves bugs.
Ex : You didn't print the value of A, maybe not your fault as this is necessary in SOLOLEARN as it doesn't work like a normal console.
- Writing an understandable code :
You should always add comments, recognized variable names, spaces both horizontally and vertically to help you and your co-developers easily understand the program working.
Ex :
Original - int a;
if(e==1) {}
Modified - int amount; or int amt;
if (e == 1) {}
- Besides these small bugs, the code is perfect
+ 3
The calculation b=(a-a%5)/5 can be simplified. It is the same result if you simply write b = a/5. Because this is integer division, it truncates the part that would be (a%5)/5 anyway. Likewise, d=(c-c%2)/2 could be simplified to d=c/2.
Notice that the task repeats similar logic for each $ denomination. If you are familiar with writing functions in C++, you could make a function that generalizes the logic for any demomination. That would simplify maintenance on the program and reduce its size.
0
Hi, your program seems perfect & efficient, you just need to work on :
- The IF statements
- The printing pattern
- Writing an understandable code
0
What do you mean by working on them?
Can you give me some examples?