Here are C++ programs to add two arrays. In First program two arrays are added using for-loop, and in second program operator overloading is used two add two arrays.
C++ program to add two arrays using for-loop
#include<iostream>
using namespace std;
int main()
{
int a1[20],a2[20],num;
cout<<"How many elements to be stored (max 20) : ";
cin>>num;
cout<<"Enter elements of array a1 : ";
for(int i=0;i<num;i++)
cin>>a1[i];
cout<<"Enter elements of array a2 : ";
for(int i=0;i<num;i++)
cin>>a2[i];
cout<<"Addition of two arrays : ";
for(int i=0;i<num;i++)
cout<<a1[i]+a2[i]<<" ";
return 0;
}
using namespace std;
int main()
{
int a1[20],a2[20],num;
cout<<"How many elements to be stored (max 20) : ";
cin>>num;
cout<<"Enter elements of array a1 : ";
for(int i=0;i<num;i++)
cin>>a1[i];
cout<<"Enter elements of array a2 : ";
for(int i=0;i<num;i++)
cin>>a2[i];
cout<<"Addition of two arrays : ";
for(int i=0;i<num;i++)
cout<<a1[i]+a2[i]<<" ";
return 0;
}
C++ program to add two arrays using operator overloading
#include<iostream>
using namespace std;
int num;
struct add
{
int ar[20];
};
add operator+(add &a1,add &a2)
{
add a3;
for(int i=0;i<num;i++)
a3.ar[i]=a2.ar[i]+a1.ar[i];
return a3;
}
int main()
{
add a1,a2,a3;
cout<<"How many elements to be stored (max 20) : ";
cin>>num;
cout<<"Enter elements of array a1 : ";
for(int i=0;i<num;i++)
cin>>a1.ar[i];
cout<<"Enter elements of array a2 : ";
for(int i=0;i<num;i++)
cin>>a2.ar[i];
a3=a1+a2;
cout<<"Addition of two arrays : ";
for(int i=0;i<num;i++)
cout<<a3.ar[i]<<" ";
return 0;
}
using namespace std;
int num;
struct add
{
int ar[20];
};
add operator+(add &a1,add &a2)
{
add a3;
for(int i=0;i<num;i++)
a3.ar[i]=a2.ar[i]+a1.ar[i];
return a3;
}
int main()
{
add a1,a2,a3;
cout<<"How many elements to be stored (max 20) : ";
cin>>num;
cout<<"Enter elements of array a1 : ";
for(int i=0;i<num;i++)
cin>>a1.ar[i];
cout<<"Enter elements of array a2 : ";
for(int i=0;i<num;i++)
cin>>a2.ar[i];
a3=a1+a2;
cout<<"Addition of two arrays : ";
for(int i=0;i<num;i++)
cout<<a3.ar[i]<<" ";
return 0;
}
No comments:
Post a Comment