1.

Write definitions for two versions of an overloaded function. This function’s 1st version sum() takes an argument, int array, and returns the sum of all the elements of the passed array. The 2nd version of sum() takes two arguments, an int array and a character (‘E’ or ‘O’). If the passed character is ‘E’, it returns the sum of even elements of the passed array and is the passed character is ‘O’, it returns the sum of odd elements. In case of any other character, it returns 0 (zero). 

Answer»

int sum(int a[ ])

{

int n,sum=0;

cout<<"Enter n:";

cin>>n;

for(int i=0;i<n;i++)

{

sum=sum+a[i];

}

return sum;

}

//

int sum(int a[ ],char c)

{

int even=0,odd=0;

switch(c)

{

case 'E':

for(int i=0;i<5;i++)

{

if(a[i]%2==0)

{

even = even+a[i];

}

}

 return even;

case 'O':

for(int j=0;j<5;j++)

{

if(a[j]%2!=0)

{

odd=odd+a[j];

}

}

return odd;

}

}



Discussion

No Comment Found

Related InterviewSolutions