Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PROBLEM 2: Start from the solution program of Problem 2 of Homework Modify the p

ID: 3846899 • Letter: P

Question

PROBLEM 2:

Start from the solution program of Problem 2 of Homework Modify the program as follows: a) Remove the variable for GPA from your code, you will not need it for this problem. b) Remove the initializing constructor, you will not need it for this problem. c) Create an integer array named score to store the exam scores from 5 tests as a private member. d) Create a double named average to store the average across the 5 test scores as a private member. e) Modify the accessor and mutator functions to get and set the values for the exam scores and the average score. The mutator function needs to get the individual test scores from the user and calculate the average score. The accessor function needs to display the individual scores as well as the average score. f) In your main function, declare a static array of 5 students. g) Demonstrate the use of the accessor and mutator functions you create in Problem 2(d) for each of the students you created in Problem 2(e), using a loop of your choice. I need a second opinion on a homework that I am having trouble with. I know it's a pretty long one but I will make two of these as a courtesy so you can Post as two answers for the time that is spent. Thanks a bunch! Design a class for complex numbers called Complex. It must have two private variables-one for the real part and one for the imaginary part of the complex number. Ohm rite appropriate constructor, accessor and mutator functions to create, get and set the objects and member variables. Implement the following for the class. a) A friend function that alpha dds two Complex objects and returns the sum as a Complex object to be assigned to a different Complex object. b) A member function that accepts another Complex object as an argument, computes the sum, and returns the sum as a Complex object to be assigned to a different Complex object. c) write operator overloading functions for + (plus), -(minus) and *(multiply) to perform addition, subtraction and multiplication of complex numbers. Your functions must accept two constant Complex objects passed by reference d) write Operator overlook a ding functions for > > and and

Explanation / Answer

Could only answer Problem 2 as you have not given the starting file for Problem 2. May be you can edit and post problem 2 with the starting solution file for problem2.

Answer for problem 1 : complex number

If any doubts/clarifications , please post a comment and I shall respond to it. Please don't forget to rate the answer if it helped. Thank you very much.

#include <iostream>

using namespace std;
class Complex
{
private:
double real, img;
public:
//default constructor
Complex()
{
real = img = 0;
}
//constructor with 2 parameters
Complex(double rl, double im)
{
real = rl;
img = im;
}

//accessor and mutaotr functions
double getReal()
{
return real;
}
double getImaginary()
{
return img;
}
void setReal(double r)
{
real = r;
}
void setImaginary(double i)
{
img = i;
}

//member function to add another complex number adn return the result
Complex add(const Complex &other) const
{
return Complex(real + other.real , img + other.img);
}

// friend function to add to return sum of 2 complex numbers
friend Complex sum(const Complex &c1, const Complex &c2);

//friend functions for + , - , *
friend Complex operator +(const Complex &c1, const Complex &c2);
friend Complex operator -(const Complex &c1, const Complex &c2);
friend Complex operator *(const Complex &c1,const Complex &c2);

//>> and << operators
friend ostream & operator <<(ostream &out, const Complex &c);
friend istream & operator >>(istream &in, Complex &c);

};


Complex sum(const Complex &c1, const Complex &c2)
{
return c1.add(c2);
}

Complex operator +(const Complex &c1, const Complex &c2)
{
double r = c1.real+ c2.real;
double i = c1.img + c2.img;
return Complex(r, i);
}

Complex operator -(const Complex &c1, const Complex &c2)
{
double r = c1.real - c2.real;
double i = c1.img - c2.img;
return Complex(r, i);
}

Complex operator *(const Complex &c1, const Complex &c2)
{
double r = (c1.real * c2.real) - (c1.img * c2.img);
double i = (c1.img * c2.real )+ (c1.real * c2.img);
return Complex(r, i);
}

ostream & operator << (ostream &out, const Complex &c)
{
if(c.img < 0) //check if we should show a - sign instead of +
out << c.real << " - " << (-c.img) << " i";
else
out << c.real << " + " << c.img << " i";
return out;

}

istream & operator >>(istream &in, Complex &c)
{
in >> c.real >> c.img;
return in;
}

int main()
{
Complex c1(4,5), c2(2, 3);
Complex c3 = c1+c2, c4 = c1 - c2, c5 = c1 * c2;
cout<< "c1 = " << c1.getReal() << " + " << c1.getImaginary() << " i "<<endl;
cout<< "c2 = " << c2.getReal() << " + " << c2.getImaginary() << " i "<<endl;
cout<< "c3 = c1 + c2 = " << c3.getReal() << " + " << c3.getImaginary() << " i "<<endl;
cout<< "c4 = c1 - c2 = " << c4.getReal() << " + " << c4.getImaginary() << " i "<<endl;
cout<< "c5 = c1 * c2 = " << c5.getReal() << " + " << c5.getImaginary() << " i "<<endl;

Complex c6;
cout<<"Enter real and imaginary part for a complex number: ";
cin>>c6;
cout<<"You enter the following complex number " << c6 << endl;

}

output

c1 = 4 + 5 i
c2 = 2 + 3 i
c3 = c1 + c2 = 6 + 8 i
c4 = c1 - c2 = 2 + 2 i
c5 = c1 * c2 = -7 + 22 i
Enter real and imaginary part for a complex number: 5 -3
You enter the following complex number 5 - 3 i

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote