So, I'm kinda new to this and i took an extra course in my school to learn programming. Got this task and i tried to write a program to count it, but it keeps telling that the answer is 0.So the task is: on the first day it snowed s millimeters of snow, then the next day it snowed m millimeters more, count how many days (or d) does it take to reach n millimeters(all sizes are natural). if s=17, m=3, n=65, then d=4;
So i wrote this, but it keeps spewing out that d=0. Can anyone tell me where is the problem here ?
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int s, m, d=0, n;
cout <<"s: ";cin >> s;
cout <<"m:";cin >> m;
cout <<"n:";cin >> n;
while (s>=n){
d++;
s+=m;
n-=s;
}
cout<<"d:"<< d << endl;
return 0;
}
Just focusing on the code: s = 17, m = 3, n = 65.
Your while loop's condition for looping is "s >= n". 17 is not greater or equal to 65, so the while loop is never entered. Thus, d stays as 0.
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
usingnamespace std;
int main()
{
double s, m, d = 0, n, avg;
cout << "Enter the first amount (S)\n";
cin >> s;
cout << "Enter the secound amount (M)\n";
cin >> m;
cout << "Enter the amount you want to calcualte for (N)\n";
cin >> n;
//Ithink its better if u find the average in the first 2 days and then try to find how much it needs more to reach d
avg = s + m / 2.0;
d = n / avg;
cout << "d:" << d << endl;
system("pause");//theis would pause the screen after u get the output
return 0;
}
I think this is the true way to use it . doubles instead of int , and using the average .
am learning just like you.
s + m / 2.0 is not taking the average, it need parentheses. Also I interpreted the question as meaning it snows m millimeters every day after the first day, but that could be wrong (your solution makes more realistic sense if only 2 days are known). I'm not sure why OP is doing n -= s in the loop.
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
usingnamespace std;
int main()
{
double s, m, d = 0, n, avg;
cout << "Enter the first amount (S)\n";
cin >> s;
cout << "Enter the secound amount (M)\n";
cin >> m;
cout << "Enter the amount you want to calcualte for (N)\n";
cin >> n;
//Ithink its better if u find the average in the first 2 days and then try to find how much it needs more to reach d
avg = (s + m )/ 2.0;
d = n / avg;
cout << "d:" << d << endl;
system("pause");//theis would pause the screen after u get the output
return 0;
}
well, i need to use while to get the answer, and to be clearer next day it snows m more than the last, i meant, that is snows not only two days, it can snow more, thus every next day it snows m millimeters more than the last. So i need to find how many days does it snow until s is more than n.
well i fixed it up a bit, but now, the problem is that i entered that s<=n and it shows that it snowed for 3 days, but i need to make it that s>=n, but if i put that, as jonin said, it will not enter the loop. what can i do ?
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int s, m, d=0, n;
cout <<"s: ";cin >> s;
cout <<"m:";cin >> m;
cout <<"n:";cin >> n;
while(s<=n){
d++;
s+=m;
m+=s;
}
cout<<"d:"<< d << endl;
return 0;
}
In Esperanto, "kiekprisnigo" means "where it is". I have no idea, I just used Google Translate, but that hints it isn't related to howmuchitsnowed.
To the OP, maybe it's a bit early, but....
One of the coding standards tells us to be clear about names. "howmuchitsnows" isn't "comfortable", it seems to me. Among the example solutions proposed (interpreted for this context), these might be better choices:
These are different styles of shortened (slightly) versions of "howmuchitsnows". Creativity is encouraged, as is clarity, in choosing the name. Verbose, long names are discouraged, but sometimes what is too long is subjective. Separating words in camel case (howMuchItSnowed) is a common choice, but not universally accepted. Separating words with underscore (never leading) is often recommended by authoritative sources (including Stroustrup). Single word options, like precipitation, is generally preferred when they're applicable.
I find that if I do not have good names for things then I do not have a good grasp of what I am trying to do and/or of the algorithm.
I am a little amazed that we are picking on "kiekprisnigo", which seems easy enough to read to me, much like "filename" or "totalsnow" -- and it clearly indicates what it is.
I would be much more inclined to lecture about one or two letter variable names which obscure the code and make it hard to think about your algorithm.
I only use them when dealing with throwaways local to < 5 lines of code, and even then I tend to correlate names to use. n for an integer counter, or s for a string, re for a regular expression, etc.