top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a ‘C’ Program to compute the sum of all elements stored in an array using pointers?

+3 votes
1,539 views
Write a ‘C’ Program to compute the sum of all elements stored in an array using pointers?
posted Dec 3, 2014 by Manikandan J

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+3 votes
 
Best answer
#include<stdio.h>
#include<conio.h>
void main() {
   int numArray[10];
   int i, sum = 0;
   int *ptr;

   printf("\nEnter 10 elements : ");

   for (i = 0; i < 10; i++)
      scanf("%d", &numArray[i]);

   ptr = numArray; /* a=&a[0] */

   for (i = 0; i < 10; i++) {
      sum = sum + *ptr;
      ptr++;
   }

   printf("The sum of array elements : %d", sum);
}

Output :

Enter 10 elements : 11 12 13 14 15 16 17 18 19 20
The sum of array elements is 155
answer Dec 3, 2014 by Shivaranjini
0 votes

We can use recursive method, something like

int N = array_size;
int *ptr = array;
int sum = 0;

int sum(int *ptr )
{
 if (N == 0)   // array index start with zero
   return *ptr

  N--;
  return sum + sum(ptr + N));
}

Use the method sum(ptr+N)

answer Dec 4, 2014 by sivanraj
Similar Questions
+1 vote

Write a program to print the sum of the elements of the array with the given below condition. If the array has 6 and 7 in succeeding orders, ignore 6 and 7 and the numbers between them for the calculation of sum.
Eg1) Array Elements - 10,3,6,1,2,7,9
O/P: 22
[i.e 10+3+9]
Eg2) Array Elements - 7,1,2,3,6
O/P:19
Eg3) Array Elements - 1,6,4,7,9
O/P:10

+1 vote

Write a program to remove the duplicate elements in an array and print
Eg) Array Elements - 12, 34, 12, 45, 67, 89
O/P: 12,34,45,67,89

...