Sum of Array Using Pointer

Problem Statement
Read N integers and store them in an array A,
and find the sum of all these elements using pointer
#include<stdio.h>
#include<stdlib.h>
int main()
{
int *y,n,sum,i;
printf("enter the dimension of array \n");
scanf("%d",&n);
y=(int*)malloc(n*sizeof(int));
printf("enter the array \n");
for(i=0;i<n;i++)
{
scanf("%d",&y[i]);
}
for(i=0;i<n;i++)
{
sum=sum+ *y;
y++;
}
printf("the sum is %d \n",sum);
return 0;
}


No comments:

Post a Comment