c - For loop runs an extra time than what is specified? -


so code takes in list of numbers , outputs consecutive rolling average of numbers. scanf function runs time put in zero. how can fix this? here have:

#include <stdio.h> #include <stdlib.h> #include <string.h>  int main(){     int i,n;     double numbers[100]; double previous[100]; double x[100];     double mean[100]; double old_average[100]; double new_average[100];     printf("total amount of numbers: ");     scanf("%d\n", &n);     (i=1; i<n+1; i++){         scanf("%lf\n", &numbers[i]);         }         old_average[1] = numbers[1] / 1;     (i=1; i<n+1; i++){         new_average[i] = (((old_average[i] * (i)) + numbers[i+1]) / (i+1));         old_average[i+1]=new_average[i];         }     printf("%lf\n", old_average[1]);     (i=1; i<n+1; i++){         printf("%lf\n", new_average[i]);     }           //new_average[i] = (((old_average[i] * i-1) + numbers[i]) / i);         //mean[j] = numbers[i] + mean      //printf("%f\n", old_average[1]);     //for (i=2; i<n+2; i++){       //  printf("%f\n", new_average[i]);     //}     return 0; } 

here input , output like

input:   total amount of numbers: 10 10 8 9 15 12 2 3 8 7 11 0(this loop) output:  10.0 9.0 9.0 10.5 10.8 9.3 8.4 8.4 8.2 8.5 -1.#qnan0 (this loop) 

how can fix issue?

remove \n, means "read until end-of-file or next non white-space character", in each format specifier passed scanf().


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -