#include <stdio.h>
int sumofelements(int a[],int size)
{
int i,sum=0;
for (i=1;i<=size;i++)
{
sum+=a[i];
}
return sum;
}
int main()
{
int a[]={1,2,3,4,5};
int size=sizeof(a)/sizeof(a[0]);
int soe=sumofelements(a,size);
printf("sum of the elements=%d\n",soe);
}
The error in the sumofelements
where the for
loop if it starts with 1 it produces garbage value.