|
So long as you're not doing anything with source control, everything is in the project folder.
|
|
|
|
|
Hallo there
i want to get the Aveg Temperature of August which has 31 day but sometime there is a data of temp just for 29 or 27 however how could i get the Avag based on how many days has been read and saved as data that could be (29,27,26 or 10 days) ?
here is the code :
public static TheWeather GetAverageForMonth(List<TheWeather> entries, int year, int month)
{
if (entries == null || entries.Count < 1)
return null;
int days = 0;
int index = entries.Count();
double total = 0.0;
double Aveg = 0.0;
for (index = 0; index < entries.Count; index++)
{
if (entries[index].dateTime.Year == year && entries[index].dateTime.Month == month)
{
total += entries[index].Actual_mean_temp;
days = entries[index].dateTime.Day;
}
}
Aveg = total / days;
return entries[index];
|
|
|
|
|
That's not how you count "days"
days = entries[index].dateTime.Day;
days++;
The return doesn't make much sense ... and you throw away the "average".
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
i have already tried but the problem here if there is only temperature for 29 day it will get the aveg for 31 day and thats wrong . i need the aveg based on how many days alread have it could be in August 29 or 23 days has been read
|
|
|
|
|
You're making a rod for your own back here: an average is always the same:
average = sum of samples / number of samples
So what your code needs to do is sum the samples and count how many items you added to the sum.
That shouldn't have anything to do with the number of days in a month at all - because you have no guarantee that a sample has been taken every day during the month, regardless of whether the month is "complete yet".
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
could you show me some code to be more clear what do you mean
|
|
|
|
|
You are kidding, right?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Aveg = total / days;
to understand what i meant : (days) are the days of August which they 31 day right .
total is the all temperature in August
but i have Temperature for just 29 day in August then how i can divided on 31 ?
|
|
|
|
|
No, read what I said:
average = sum of samples / number of samples
That's not the same thing. If you have 29 samples, then dividing by 31 will give you the wrong average.
So each time you add a sample to the sum, you count the sample, and divde teh total by teh count, not by a "fixed value".
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Message Closed
modified 8-May-19 7:47am.
|
|
|
|
|
ohh its fine man i got it ... i thank you for your nice explantation
|
|
|
|
|
I refer to the reply which is written from OriginalGriff :
Average means : you build a sum of all entries in your List and divide this sum by the number (count) of the entries in your List.
For me that was very good pointed out from OG.
For this it is absolutely irrelevant how much entries are in the List - also it has nothing to do with the number of days in the month.
|
|
|
|
|
oh ja Ralf es geht jetzt .. danke für deine Message
|
|
|
|
|
I was going to start out by asking if this was a LINQ or non-LINQ question, but thought the better of it.
You're a saint.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Quote: days = entries[index].dateTime.Day;
Should have been
++days;
|
|
|
|
|
Not related to your problem, but to the code;
int index = entries.Count(); Why call the count-method, when you're using the .Count-property elsewhere?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
does make that difference ?
|
|
|
|
|
Abdalla Ben Omran wrote: does make that difference ? Calling a property is usually preferred; after assigning that value to it, it gets set to 0 in the loop, overwriting your initial initialization.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
public static TheWeather GetAverageForMonth(List<TheWeather> entries, int year, int month)
{
if (entries == null || entries.Count < 1)
{
return null;
}
int days = 0;
int index = entries.Count();
double total = 0.0;
double Aveg = 0.0;
for (index = 0; index < entries.Count; index++)
{
if (entries[index].dateTime.Year == year
&& entries[index].dateTime.Month == month
&& entries[index].Actual_mean_temp > 0)
{
total += entries[index].Actual_mean_temp;
days += 1;
}
}
Aveg = total / days;
return entries[index];
}
EDIT ==========
Fixed again. Everything was in the first if block, and it shouldn't have been.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
modified 9-May-19 7:29am.
|
|
|
|
|
Surely that should be:
days += 1;
|
|
|
|
|
Or even
days++;
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
or even
days = days + 1;
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Yes it should be, and don't call me Shirley.
Fixed.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- When you pry the gun from my cold dead hands, be careful - the barrel will be very hot. - JSOP, 2013
|
|
|
|
|
Now we wait to see if we got the group-excercise right
..but still, why is there a Linq-call to do a count, if that value is thrown away shortly after?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Shirley the rest of the method shouldn't be grouped within the first if block?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|