Back to Problem Solutions forum
My answers are off by a little, but I can't figure out whats wrong?
Expected answers : 32.6 33 34.6 39.1666666667 41.4666666667 43.7 44.1 My answers: 32.600000 33.000000 35.200000 39.166667 42.055556 42.751852 44.100000
Thanks! :)
float smoothingtheWeather(int x ,double arr[200]){
int i, j;
for(i=0; i<x-1; i++){
arr[i+1] = (arr[i] + arr[i+1] + arr[i+2]) / 3;
}
for(j=0; j<x+1; j++)
printf("%f ", arr[j]);
}
int main(){
int n, i;
double arr[200];
double *ptr;
ptr = arr;
freopen("SmoothingtheWeather.txt", "r", stdin);
if(freopen("SmoothingtheWeather.txt", "r", stdin)== NULL){
fprintf(stderr, "\aCan't open file!\n");
exit(1);
}
scanf("%d", &n);
for(i=0; i<n; i++)
scanf("%lf", &arr[i]);
smoothing_the_Weather(n-1, ptr);
return 0;
}
I'm going to make two assumptions, correct me if either is wrong:
Your SmoothingtheWeather.txt
file contains the sample data from the problem specification:
7
32.6 31.2 35.2 37.4 44.9 42.1 44.1
Before we begin, let's talk about a couple of quick style points that caused non-fatal warnings on my machine:
float smoothing_the_Weather(int, double[])
reaches the end of function without a return. Either return a garbage float, or better change the return type to void if you're not going to return anything.exit(1)
in case of file opening failure. You need to include stdlib.h
if you're going to use that.And one issue that will cause a fatal error:
malloc
) at run-time instead?I've made those changes on my copy of your software and launched into my debugger at this point and I see exactly what's happening. While I won't give you an answer directly, here are some clues:
for(i=0; i<x-1; i++)
... Given that there are x
items in arr[]
, and the first (i == 0) and last (i == x) items remain unchanged (per the problem specification): how many iterations does this loop perform? How many iterations should it perform in order to smooth each point?i
, and what three indices of arr[] should you use to calculate the rolling average?Good luck. I looked at your problem and you're close!
Thank you so much! :) I fixed the styling errors, and used malloc for array allocation. And now I see why my answer was abit off.. I used substituted values for calculations when I should have used the original ones.