Write a program to add and multiply elements in two arrays. Use operator overloa
ID: 3621036 • Letter: W
Question
Write a program to add and multiply elements in two arrays. Use operator overloading foraddition and multiplication by using two member functions. Also the size of each array should
be specified dynamically by entering them from a console, and the size of array should be set
during the construction of an object. If the two arrays have different sizes, then you can just
multiply and add the rest to zeros. Finally, use the ">>" operator overloading to enter the
elements of the array, and use "<<" operator overloading to output the results of addition and
multiplication.
Example:
A: 4 6 3 6 7
B: 2 2 5 1 1 3 4
--------------------------- (Multiply)
Result: 8 12 15 6 7 0 0
Q2) Show that the following equalities are correct:
a) 5n2 – 6n = T(n2 )
b) 33n3 + 4n = T(n3 )
Explanation / Answer
please rate - thanks
CRAMSTER rule - 1 question per post, I don't know the answer to Q2
#include <iostream>
#include <cmath>
using namespace std;
class Array
{
friend ostream &operator<<( ostream &, const Array & );
friend istream &operator>>( istream &, Array & );
public:
Array();
Array(int );
Array operator+(Array );
Array operator*(Array);
Array create(int[],int);
private:
int *numbers;
int elements;
};
Array Array::create(int a[],int n)
{int i;
numbers=new int[n];
elements=n;
for(i=0;i<n;i++)
numbers[i]=a[i];
}
Array::Array(int n)
{numbers=new int[n];
elements=n;
}
Array::Array()
{elements=0;
}
Array Array::operator+(Array b)
{int i,n,m;
n=elements<b.elements?elements:b.elements;
m=elements>b.elements?elements:b.elements;
Array c(m);
for(i=0;i<n;i++)
c.numbers[i]=numbers[i]+b.numbers[i];
for(i=n;i<m;i++)
c.numbers[i]=0;
return c;
}
Array Array::operator*(Array b)
{int i,n,m;
n=elements<b.elements?elements:b.elements;
m=elements>b.elements?elements:b.elements;
Array c(m);
for(i=0;i<n;i++)
c.numbers[i]=numbers[i]*b.numbers[i];
for(i=n;i<m;i++)
c.numbers[i]=0;
return c;
}
ostream& operator<<( ostream &output, const Array &num )
{int i;
for(i=0;i<num.elements;i++)
output <<num.numbers[i]<<" ";
cout<<endl;
return output;
}
istream &operator>>( istream &input,Array &num )
{int i=0,temp[50];
input>>temp[i];
while(temp[i]!=-999)
{i++;
input>>temp[i];
}
num.create(temp,i);
return input;
}
#include <iostream>
using namespace std;
int main()
{Array a,b,c;
int n;
cout<<"Enter values for a(-999 when done): ";
cin>>a;
cout<<"Enter values for b(-999 when done): ";
cin>>b;
cout<<"A: ";
cout<<a;
cout<<"B: ";
cout<<b;
cout<<"------------------------(multiply) Results: ";
c=a*b;
cout<<c;
cout<<"------------------------(add) Results: ";
c=a+b;
cout<<c;
cout<<endl;
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.