Help with arrays

This is a discussion on Help with arrays within the C Programming forums, part of the General Programming Boards category; Hello I try to write this code it takes in 12 numbers from the user and puts it into an ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Help with arrays

    Hello I try to write this code it takes in 12 numbers from the user and puts it into an array then the array is passed on the another function then the average of those 12 numbers are taken. But I can'nt seem to get it working I will be very greatful if anyone can fix my code.

    Code:
    #include <stdio.h>
    
    #define size 12
    
    double ave (int a[size]);
    
    int main()
    {
    	int a[size];
    	int i;
    	
    for(i=0; i<=12; i++)
    {
    	printf("Please enter the 12 numbers needed: ");
    	scanf("%d",&i);
    }
    
    ave (&a[size]);
    
    return 0;
    }
    
    double ave (int a[size])
    {
    	double value;
    	int i;
    	
    for(a[i]=1; a[i]<13; i++)
    {
    	value= i/12;
    }
    
    printf(" the average value is %d\n",i);
    
    return 0;
    }

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    proper working code

    Code:
    #include <stdio.h>
    #define size 12
    
    float ave(int []);
    
    int main()
    {
    	int a[size];
    	int i,ch;
    	
    	printf("Please enter the 12 numbers needed: ");
    for(i=0; i<12; i++)
    	scanf("%d",&a[i]);
    
    printf("The average of the array is %f",ave(a));
    
    while((ch=getchar())!='\n' && ch!=EOF);
    getchar();
    return 0;
    }
    
    float ave(int a[])
    {
    	float total=0.0;
    	int i;
    	
    for(i=0;i<size;i++)
        total+=a[i];
        
    return total/12.0;
    }
    
    /*my output
    Please enter the 12 numbers needed: 12
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    The average of the array is 8.333333
    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Building B-Tree from Arrays
    By 0rion in forum C Programming
    Replies: 1
    Last Post: 04-09-2005, 03:34 AM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21