assignment 1 pro vipul
assignment 1 pro vipul
Assignments - 01
1. Write a C program to find the Sum of Elements in an Array
Using Pointers
Input the Number of Elements: The first thing that the program
does is ask the user to enter the number of elements he intends to
place in the array.
Declare the Array: The program declares an array of integers, based
on the user's input.
Input Elements: The program now asks the user to input elements of
the array.
Pointer Initialization: A pointer (ptr) is initialized to point to the first
element of the array.
Sum Calculation: The program uses a loop to iterate through the
array with pointer arithmetic. The sum of the elements is computed by
dereferencing the pointer and adding the values to the sum variable.
Output the Result: Finally, the program prints the sum of the
elements in the array.
Pseudocode:-
START
DECLARE n, i, sum
INITIALIZE sum = 0
DECLARE arr[n]
DECLARE ptr
PRINT "Enter the number of elements in the array: "
READ n
PRINT "Enter n elements: "
FOR i FROM 0 TO n-1 DO
o PRINT "Element ", i + 1, ": "
o READ arr[i]
END FOR
SET ptr = arr
FOR i FROM 0 TO n-1 DO
o sum = sum + *(ptr + i)
END FOR
PRINT "The sum of the elements in the array is: ", sum
END
Flow Chart:-