Close
Close

Help with printing arrays

   Ben

In the code,

#include <stdio.h>
float average(float a[], int size)
{
    int i;
    float avg, sum=0;
    for(i=0;i<size;i++)
    {
        sum+= a[i];
    }
    avg = sum/size;
    return avg;
}
int main() 
{
    int size,j; 
    printf("Enter the size of array"); 
    scanf("%d", &size); 
    float b, n[size]; 
    for(j=0; j<size; j++) 
    { 
        printf("Value of n[%d] : ", j);
        scanf("%f", &n[j]);
    } 
    b = average(n, size); 
    printf("Average of numbers=%.2f\n",b); 
    return 0;
}

 

The return is 

Value of n[0] : 12 
Value of n[1] : 234 
Value of n[2] : 12.43 
Value of n[3] : 43.43 
Average of numbers=75.46

My question is, where the heck are they getting the numbers 12, 234, 12.43, and 43.43 from?? It never declares what j is, nor does it ever ask for any values from user, so where are those random numbers coming from?


Answers

  •   

    I ran the code and it is working as expected. It is asking for the numbers and then printing the average. https://ideone.com/BqEeY6


    • Yeah I figured that one out, can you help with the one I posted most recently? That I still don't get
      - Ben
    • Yeah, that is also done.
      - Amit Kumar

Ask Yours
Post Yours
Write your answer