How to add array Elements in odd and even places

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

To add array elements within specific region we can pass the start and finish index as shown in the c code in our previous blog. Click here to see the previous blog We can make this function even more intellegent. This intellegent function can be used to sum those integers in the even place of the array or the odd place elements, or can be made to return the sum of all the elements in the specified region or range.

A flag will be passed as the third arguments. If the argument is zero(0) then the function will return sum of all the elements in the specified region including the start and finish index. If the argument is one(1), then it will return the sum of only the even place numbers. If the argument is two (2), then his function would return the sum of all integers in the odd place within the specified range by start and finish. Here is the code in C.

Program

	 
// How to add array Elements in a special region

#include<stdio.h>
void main()
{
int value; 
int array[] ={0,1,2,3,4,5,6,7,8,9,10,11};  
value = arrayadd(array,0,11,0);
printf("All the elements from start to finish SUM=: %d \n",value);

value = arrayadd(array,0,11,1);
printf("Only the even place number SUM=: %d \n",value);
 
value = arrayadd(array,0,11,2);
printf("Only the odd place number SUM=: %d \n",value);
}




int arrayadd(int array[], int start, int finish, int flag)
{
int i;
int  sum;

 i = start ;
 sum = 0;

if( flag== 0 ) // Add all the elements from start to finish
 for(; i<=finish; i++)
 {
 sum+= array[i];
 }
 
 if(flag == 1) // Add only the even place number
  {
     if(start%2==0) // where to really start from?
        i = start;
    else
       i = start+1;
      
  
  for(; i<=finish; i+=2)
   sum+=array[i];
  }
  
  
  if( flag==2) // adding only the odd place number
  {
     if(start%2!=0) // where to really start from?
       i = start;
     else
	    i = start+1;
		    
     for(; i<=finish; i+=2)
     sum+=array[i];  
  
  }
  
  return sum;
  
}
	 
 

output

	
All the elements from start to finish SUM=: 66
Only the even place number SUM=: 30
Only the odd place number SUM=: 36
Press any key to continue . . .
	 
 

See all the elements and their additions according to odd and even position

	 
 
// How to add array Elements in a special region

#include<stdio.h>
void main()
{
int value; 
int array[] ={0,1,2,3,4,5,6,7,8,9,10,11};  
value = arrayadd(array,0,11,0);
printf("All the elements from start to finish SUM=: %d \n",value);

value = arrayadd(array,0,11,1);
printf("Only the even place number SUM=: %d \n",value);
 
value = arrayadd(array,0,11,2);
printf("Only the odd place number SUM=: %d \n",value);
}




int arrayadd(int array[], int start, int finish, int flag)
{
int i;
int  sum;

 i = start ;
 sum = 0;

if( flag== 0 ){ // Add all the elements from start to finish
 for(; i<=finish; i++)
 {
 printf("All Element array[%d] = %d \n",i, array[i]);
 sum+= array[i];
 }
 
}
 
 if(flag == 1) // Add only the even place number
  {
     if(start%2==0) // where to really start from?
        i = start;
    else
       i = start+1;
      
  
  for(; i<=finish; i+=2){
    printf("Edd Element array[%d] = %d \n",i, array[i]);
   sum+=array[i];
   }
  
  }
  
  
  if( flag==2) // adding only the odd place number
  {
     if(start%2!=0) // where to really start from?
       i = start;
     else
	    i = start+1;
		    
     for(; i<=finish; i+=2){
     printf("Odd Element array[%d] = %d \n",i, array[i]);
     sum+=array[i];  
     } 
  }
  
  return sum;
  
}
	 
 

output

	
 All Element array[0] = 0
All Element array[1] = 1
All Element array[2] = 2
All Element array[3] = 3
All Element array[4] = 4
All Element array[5] = 5
All Element array[6] = 6
All Element array[7] = 7
All Element array[8] = 8
All Element array[9] = 9
All Element array[10] = 10
All Element array[11] = 11
All the elements from start to finish SUM=: 66
Edd Element array[0] = 0
Edd Element array[2] = 2
Edd Element array[4] = 4
Edd Element array[6] = 6
Edd Element array[8] = 8
Edd Element array[10] = 10
Only the even place number SUM=: 30
Odd Element array[1] = 1
Odd Element array[3] = 3
Odd Element array[5] = 5
Odd Element array[7] = 7
Odd Element array[9] = 9
Odd Element array[11] = 11
Only the odd place number SUM=: 36
Press any key to continue . . .