How to add array Elements in a special region

Rumman Ansari   2017-11-13   Student   Data structure > array   13253 Share

In this lesson we are trying to manipulate the elements of the array. In the data structure, you might have to take the summation of the elements of the array or do some kind of a mathematical operation on them. In this program we will to add array Elements in a special region.


// How to add array Elements in a special region

#include<stdio.h>
void main()
{
int value; 
int array[] ={1,2,3,4,5,66,7,8,9,10,11,12};
value = add(array,0,6);
printf("%d \n",value);
}

int add(int array[], int start, int finish)
{

int i = start;
int sum = 0;

for(; i<= finish; i++)
  {
  sum+=array[i];  
  }
  
  return sum;

}

Output

88
Press any key to continue . . .

Explanation

add function:

This function will add all the elements of the array and will return the sum to the calling method. This function will take the array name and start and finish indices as argements. Here is the c code.


// How to add array Elements in a special region
 
int add(int array[], int start, int finish)
{

int i = start;
int sum = 0;

for(; i<= finish; i++)
  {
  sum+=array[i];  
  }
  
  return sum;

}

This method will add all the array elements whose locations fall between the start and finish index inclusive of the two, Say, there is an array like

Array Initialization while declaration the array

int array[] ={1,2,3,4,5,66,7,8,9,10,11,12};

And we want to add 1 to 5; then we will call the above method as add(array,0,6)

This function can be used to find out the summation of all the array elements if the start index is 0 and the last index is the number of array elements. That means for the above array , if we want to find the summation of entire array elements then we should write add(array, 0,11) .

Add all array elements

int array[] ={1,2,3,4,5,66,7,8,9,10,11,12};

// How to add array Elements in a special region

#include<stdio.h>
void main()
{
int value; 
int array[] ={1,2,3,4,5,66,7,8,9,10,11,12};
value = add(array,0,11);
printf("%d \n",value);
}

int add(int array[], int start, int finish)
{

int i = start;
int sum = 0;

for(; i<= finish; i++)
  {
  sum+=array[i];  
  }
  
  return sum;

}

Output


138
Press any key to continue . . .

How to add array Elements in a special region (Take region from user)


// How to add array Elements in a special region

#include<stdio.h>
void main()
{
int start,finish,value; 
int array[] ={1,2,3,4,5,66,7,8,9,10,11,12};
printf("Enter start position and Finish position: ");
scanf("%d%d",&start,&finish);

value = add(array,start,finish);
printf("Sum is: %d \n",value);
}

int add(int array[], int start, int finish)
{

int i = start;
int sum = 0;

for(; i<= finish; i++)
  {
  sum+=array[i];  
  }
  
  return sum;

}

Output 1:

Enter start position and Finish position: 0 11
Sum is: 138
Press any key to continue . .  

Output 2:

Enter start position and Finish position: 0 5
Sum is: 81
Press any key to continue . . .