Problem 57 Smoothing the Weather

Back to Problem Solutions forum

sleepijs     2016-03-10 20:44:32
User avatar

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! :)

include <stdio.h>

include <math.h>

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;

}

Matthew Cole     2016-03-11 00:37:27

I'm going to make two assumptions, correct me if either is wrong:

  • Your code submitted here is the code you attempted to paste above before Markdown mangled it.
  • 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:

  • Your function 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.
  • You call 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:

  • Your arr[] variable is set to a fixed length of 200 floats at compile time, but the actual input set on my first loading of the page had 216 values. Thus at run time, you're going to reach the end of the array and have an issue when there's 16 more values to read into fixed storage. Of course, you could make the size of arr[] larger on the stack, say 400 (or more!) ... but think algorithmically. Do you really need to have all 216 values sitting on the stack at once, or could you use fewer values and swap/replace/print the results as you read the next value from the file? Maybe you do need all 216 values, so could you use a dynamic allocation (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:

  • In the loop 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?
  • What should be the starting and finishing values of 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!

sleepijs     2016-03-11 15:55:19
User avatar

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.

Please login and solve 5 problems to be able to post at forum