CREATE OWN LIBRARY

Sum of the elements of an Array is calculated using a pointer

Back to Programming

Description

The elements of the array are taken as input. The sum of the elements is calculated using a pointer. A pointer should be the same data type as the data type of the array. The pointer points to the first element of the array at the beginning and then traverses the whole array and calculates the sum.

 

Algorithm

INPUT: Array elements
OUTPUT: the sum of the elements
PROCESS:
Step 1: [taking the input]
	Read n [number of elements]
	For i=0 to n-1 repeat
		Read a[i]
	[end of ‘for’ loop]
Step 2: [calculating the sum of the array]
	Declare p as a pointer
	Set p<-a [the base address of the array]
	Set s<-0
	For i=0 to n-1 repeat
		Set s<-s+*p
		Set p<-p+1
	[End of ‘for’ loop]
Step 3: Stop.

Code

Complexity: