-
Notifications
You must be signed in to change notification settings - Fork 0
/
11_function_overloading2.cpp
70 lines (62 loc) · 1.48 KB
/
11_function_overloading2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
// Created by Varsha on 21-03-2023.
//
/*Write three versions of overloaded function sum().
When it takes an integer array as argument, returns the sum of all elements of
the array. When it takes array and a character, returns the sum of even
elements if char='E', returns the sum of odd elements if char='O'.
When it takes array and two integers, it will swap the positions indicated by
the integers. */
#include <iostream>
using namespace std;
int array(int a[], int n){
int sum=0,i;
for(i=0;i<n;i++){
sum += a[i];
}
return sum;
}
int array(int a[], char c, int n){
int sum=0,i;
if(c=='E'){
for(i=0;i<n;i++){
if(a[i]%2==0){
sum += a[i];
}
}
}
if(c=='O'){
for(i=0;i<n;i++){
if(a[i]%2!=0){
sum += a[i];
}
}
}
return sum;
}
void array(int a[],int x,int y, int n){
int temp;
temp = a[x];
a[x] = a[y];
a[y] = temp;
cout<<"\nSwapped array:"<<endl;
for(int i=0;i<n;i++){
cout<<a[i];
}
}
int main(){
int a[100],n,i,x,y;
cout<<"Enter the number of elements in the array: ";
cin>>n;
cout<<"Enter the elements";
for(i=0;i<n;i++){
cin>>a[i];
}
cout<<"Sum: "<<array(a,n)<<endl;
cout<<"Sum of even numbers: "<<array(a,'E',n)<<endl;
cout<<"Sum of odd numbers: "<<array(a, 'O',n)<<endl;
cout<<"Enter the positions to swap: ";
cin>>x>>y;
array(a,x,y,n);
return 0;
}