Programming in C
Programming in C
Dept of Bio-informatics
VFSTR
Algorithm :
1. Start
Initialize array, sum, and
2. Initialize the Array
pointer
// Start by defining an array and its size.
3. Initialize the Pointer
// Declare a pointer that will traverse through the
array.
Set pointer to the start of
4. Initialize the Sum
the array
// Declare a variable to keep track of the sum.
5. Traverse the Array Using the Pointer
// Iterate through the array elements using the
pointer, adding each element to the sum
variable.
false
6. Output the Result
// Print the sum of all the array elements. i < sizeof(array)
7. Stop
true
Print sum
End
Code
#include <stdio.h>
int main()
{
int n;
Test Case:
printf("Enter the size of array : ");
scanf("%d",&n);
int i, sum = 0, arr[n]; // Example array
printf("Enter elements of array of size[%d]
:\n",n);
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
int *ptr;
ptr = arr; // Pointer to the first element of
the array
// Loop through the array using the pointer
for ( i = 0; i < n; i++)
{
sum += *(ptr + i); // Dereference the
pointer to access array elements
}
printf("\nSum of all elements: %d\n", sum);
return 0;
}
Conclusion :