0
#include <stdio.h>

int main()
{
int n, i;
double num[100], sum = 0.0, average;

printf("Enter the numbers of elements: ");
scanf("%d", &n);

for(i = 0; i < n; i++)
{
    printf("Enter number %d: ", i+1);
    scanf("%f", &num[i]);
    sum += num[i];
}

average = sum / n;
printf("Average = %f", average);

return 0;
}

Hi, I have a problem with displaying the average, displaying numbers from space, how to fix it?

Poncjusz
  • 11
  • 1
  • 6
  • You should read the compiler warning, any decent compiler will tell you where is wrong and how to fix. – llllllllll Feb 03 '18 at 18:15
  • https://stackoverflow.com/questions/13730188/reading-in-double-values-with-scanf-in-c – yano Feb 03 '18 at 18:16
  • Possible duplicate of [Reading in double values with scanf in c](https://stackoverflow.com/questions/13730188/reading-in-double-values-with-scanf-in-c) – Santosh A Feb 03 '18 at 18:23

2 Answers2

1

Correct format specifier for double would be (you will have to use this)

scanf("%lf", &num[i]);

For printf both %f or %lf will do. Note that when using scanf check the return value of scanf - in case of failure you will take necessary action. (Wrong input given or some other error).

user2736738
  • 30,591
  • 5
  • 42
  • 56
0

Using the correct format specifier for double as stated by @coderredoc resolves it. Use %lf instead of %f.

techenthu
  • 158
  • 11