Hello marialavr,
To more directly answer your question.
"<cstring>" should be "string", but even then it is not needed for this program.
"<algorithm>" is not needed at all or is not needed yet.
For line 6:
Chances are that this is what you were taught and told to use, but it will become a problem some day. An alternative that is better is:
1 2 3 4
|
using std::cin;
using std::cout;
using std::endl;
using std::vector;
|
You will find this is a more narrow approach for what you need.
Lines 9 and 10 as a global variables should be avoided. The only place they are used is the function, so I would put them there.
For line 9 I would take the hint from
Furry Guy and add at least 25 to the vector.
For line 10 understand that the ".size()" and ".length()" functions return a number of "size_t", AKA "unsigned int", and that storing this value type into an "int" there could be possible data loss because an "unsigned int" can store a number larger than what an "int" can. This should generate at least a warning from the compiles, but will not stop the program from running.
Also the ".size()" and ".length()" functions return the total number of elements that are in, in this case, the vector. In C++ and some other languages things like arrays and vecctors along with other containers are (0) zero based, so the elements go from (0) zero to 2. This will be better understood shortly.
The variable name of "n" may be easy to type, but only have meaning to you. For some people it becomes a problem trying to keep track of what it is for. A suggestion call the variable "valuesSize". At least you have a better idea of what it is for.
In the function the for loop would start to look better as:
for (size_t i = valuesSize; i >= 0; i--)
. Now you can understand better that "valuesSize" has a value of (3) then when you look at
while (sum >= values[i])
you can realize that the vector does not have an element numbered (3) since it goes from (0) zero to (2). In the end the for loop should be
for (size_t i = valuesSize - 1; i >= 0; i--)
or take the suggestion from
againtry int n = values.size() - 1;
or more proper
size_t valuesSize = values.size() - 1;
.
After that a couple of "\n"s will improve the output on the screen.
In main I will start with a few blank lines makes the code easier to read and follow.
1 2 3 4 5 6 7 8 9 10 11 12
|
int main()
{
int sum;
cin >> sum;
cout << " The minimum number of coins noted that sum up to " << sum << "\t";
findMin(sum);
return 0;
}
|
Your use of the {}s is totally your choice and what you are comfortable with. With whatever you choose just be consistent in its use. You should take a look at
https://en.wikipedia.org/wiki/Indentation_style This will show you several different styles of using the {}s that you can compare.
The biggest point is to make you code easy to read and understand. Personally I like the "Allman" style, but it is your choice.
The next problems has to do with line 5 in the above code. When the program runs you are staring at a blank screen wondering what to do. The "cin" statement needs a prompt to tell the user what to enter.
Look at
againtry's first or second code line 31 or
Furry Guy's code line 17 for a suggestion.
Other than the blank lines the rest of "main" works.
Hope that helps,
Andy