#include <iostream.h>
#include <conio.h>
int sum( int a[]); // sum of all elements of an array
int sum(int a[], char ch); // sum of odd or even elements of an array, depending on choice
void main()
{
int num[10];
cout<< "\n Enter 10 integers : ";
for(int i = 0; i < 10 ; i++ )
cin >> num[i];
/* Sum of all 10 integers */
int s = sum(num);
cout<< "\n The Sum of all 10 integers = " << s;
/* Sum of all the even numbers out of all the 10 integers */
s = sum(num, 'E');
cout<< "\n The Sum of all even numbers out of all 10 integers = " << s;
/* Sum of all the odd numbers out of all the 10 integers */
s = sum(num, 'O');
cout<< "\n The Sum of all odd numbers out of all 10 integers = " << s;
/* No result...for wrong choice */
s = sum(num , 'X');
cout<< "\n No Sum for wrong choice . Hence the result = " << s;
getch();
}
// Function definition of FIRST sum function
int sum(int arr[])
{
int s = 0;
for ( int i = 0; i <10 ; i++ )
s = s+ arr[i];
return s;
}
// Function definition of SECOND sum function
int sum(int arr[], char a)
{
int se = 0, so = 0, s = 0;
switch(a)
{
case 'E':
case 'e':
for ( int i = 0; i <10 ; i++ )
{
if(arr[i]%2 == 0)
se = se+ arr[i];
}
s = se;
break;
case 'O':
case 'o':
for ( int i = 0; i <10 ; i++ )
{
if(arr[i]%2 ! = 0)
so = so + arr[i];
}
s = so;
break;
default :
s = 0;
}
return s;
}
No comments:
Post a Comment