In C++. Please include comments for explanations (beginner coder). There is one
ID: 3717880 • Letter: I
Question
In C++. Please include comments for explanations (beginner coder).
There is one task to be accomplished in this practical test. The main idea in this test is to develop, implement and run a successfully compiled program that reads about 10 complex numbers of the forin tn±bi, contained in a file ,,ComplexData.txt", into an array of structt..}; data type without using the the C++ complex data type library. The array of complex numbers may be called ComplexArrayl of size at most 100. Each element of the array is a struct of the form struct ComplexType t float re float ing: Ji You are required to multiple each element of the array ComplexArray by a constant complex number (5 2i) and then print the contents of the result array in the form "(tn ±bi)", complete with the parenthesis. If a or b is positive the " +" sign need not be printed.Explanation / Answer
#include<iostream>
using namespace std;
struct complex{
float re;
float img;
};
struct complex readComplexnum()
{
struct complex c;
cout<<" Enter the real and imaginary part of complex number=";
cin>>c.re>>c.img;
return c;
}
struct complex multComplexnum(struct complex c1,struct complex c2)
{
struct complex res;
res.re=c1.re*c2.re-c1.img*c2.img;
res.img=c1.re*c2.img+c1.img*c2.re;
return res;
}
void printcomplex(struct complex c)
{
cout<<" complex number is="<<c.re<<"+i"<<c.img<<endl;
}
int main()
{
struct complex c1,c2,res;
c1=readComplexnum();
c2=readComplexnum();
res=multComplexnum(c1,c2);
printcomplex(res);
return 0;
}
Output:
Enter the real and imaginary part of complex number=1 1
Enter the real and imaginary part of complex number=2 1
complex number is=1+i3
----------
Here You easly get the main function and manage file accordonly.