double a, b, c, d, x, y;
char operation
cout << setw(6) << a << b
<< setw(3) << operation
<< setw(6) << c << d
<< " = "
<< setw(6) << x << y
<< endl;
I'm making a calculator which takes two complex numbers and adds subtracts etc. My question is how do I format my output so that 0's are not displayed.
I.E. if the input is (a+bi)(c+di) the output is a+bi * c+di = x+yi But a, b, c, d, x, y are only displayed if they are nonzero.
I know I can do it with if statements and stuff but I was hoping there's a shorter, more efficient, path.
I don't think you can avoid doing the condition and printing if non-zero somewhere.
About all you can do is wrap it up so most code doesn't need to deal with it:
class complex {
double x;
double i;
public:
// ...
friend std::ostream &operator<<(std::ostream &os, complex const &c) {
// if both parts are 0, we probably want to print *something*
if (c.x == 0.0 && c.i == 0.0)
return os << 0;
if (c.x != 0.0)
os << c.x;
if (c.i != 0.0)
os << c.i << "i";
return os;
}
};
complex a, b, c;
// ...
cout << a << operation << b << " = " c << "\n";
You'll have to add a little more if you want this to honor (for example) width/precision correctly (though for real use, you undoubtedly want to use the complex class that's already in the standard library instead).
Yes, you can do it by including <complex>
std::complex<double> com_one; // value 0 + 0i
std::complex<double> com_two(3.14); // value 3.14 + 0i
std::complex<double> com_three(1.5, 3.14) // value 1.5 + 3.14i
std::complex<double> com_four(com_two); // value is also 3.14 + 0i
Then to use arithmetic operations, you can just use
std::cout << com_one + com_two << std::endl;
std::cout << com_one - 3.14 << std::endl;
std::cout << 2.75 * com_two << std::endl;
com_one += com_three / 2.0;
Source: http://stdcxx.apache.org/doc/stdlibug/20-2.html
For checking if it's a zero check it using if, and compare it with a zero. This is the most clean technique.
Related
I was wondering if someone could help me in this problem.
So i tested the code but it didn't show the right answer below for
equation result of x2 + 5x + 6
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
double roots() {
double a, b, c, x1, x2;
cout << "Enter quadratic equation in order (a, b, c): \n";
cin >> a >> b >> c;
double discriminant = b * b - 4 * a * c;
x1 = -b + sqrt(discriminant) / 2 * a;
x2 = -b - sqrt(discriminant) / 2 * a;
if (discriminant >= 0 && a > 1) {
cout << "Your quadratic equation is " << a << "x^2 + " << b << " x + " << c << '\n';
cout << "x1 = " << x1 << '\n';
cout << "x2 = " << x2 << '\n';
}
else if (a == 1) {
cout << "Your quadratic equation is " << "x^2 + " << b << " x + " << c << '\n';
cout << "x1 = " << x1 << '\n';
cout << "x2 = " << x2 << '\n';
}
else {
cout << "Negative value returned from (b2 - 4ac), please try again!";
exit(1);
}
}
int main() {
roots();
}
You have the formula incorrect. Try this
x1 = (-b + sqrt(discriminant)) / (2 * a);
x2 = (-b - sqrt(discriminant)) / (2 * a);
Notice the extra parenthesis in order to put 2*a in the denominator and have it divide both b and the sqrt().
You also need to check if discriminant >= 0 before doing so, because if it is negative there is no root and the above lines are going to fail.
Firstly, using namespace std was not used, so if you don't want to use it write std::court and std::cin. Secondly, the formula is b^2 -4ac so you need to put round brackets around b*b so that the answer is subtracted from -4ac. Then, you don't need to write else if for a==1 you can add it in the above condition as a>=1 and else put down a condition where discriminant is >=0 but a==0 which violates quadratic eq condition and you can write a cannot be equal to zero. Also, the main formula for x1 and x2 is wrong since the bracket should be applied around -b+sqroot(discriminant) so that the answer is then divided by multiplication of (2*a). Otherwise, what happens is that first sqrt is divided by 2 then multiplied by a and then added to -b.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
double roots() {
double a, b, c, x1, x2;
std::cout << "Enter quadratic equation in order (a, b, c): \n";
std::cin >> a >> b >> c;
double discriminant = (b * b)- (4 * a * c);
x1 = (-b + sqrt(discriminant)) / (2 * a);
x2 = (-b - sqrt(discriminant))/ (2 * a);
if (discriminant >= 0 && a >= 1) {
std::cout << "Your quadratic equation is " << a << "x^2 + " << b << " x
+ " << c << '\n';
std::cout << "x1 = " << x1 << '\n';
std::cout << "x2 = " << x2 << '\n';
}
else if (a==0){
std::cout << "a cannot be zero!";
exit(1);
}
else{
std::cout << "Negative value returned from (b2 - 4ac), please try again!";
exit(1);
}
}
int main() {
roots();
}
I have got some problems with my main.cpp.There are some places where I do not know what I have to write to make my code work well. I will write in my code where the problems are. I write the Problem word where my code is wrong. Does anybody have an idea what I have to change to make my code work?
My code is about Complex numbers add,sub,divide,mul.
Komolex.h :
#pragma once
#include <iostream>
class NullDivision : public std::exception{};
class Complex{
private:
int n, d;
public:
Complex(int _n = 0, int _d = 1) : n(_n), d(_d)
{
if(d == 0)
{
throw NullDivision();
}
}
Complex add(const Complex &b) const
{
Complex c(n + b.n , d + b.d);
return c;
}
Complex sub(const Complex &b) const
{
Complex c(n - b.n , d - b.d);
return c;
}
Complex mul(const Complex &b) const
{
Complex c(n * b.n - d * b.d ,n * b.d - d * b.n );
return c;
}
Complex div(const Complex &b) const
{
if(b.n == 0 || b.d == 0)
{
throw NullDivision();
}
Complex c((n * d + d * b.d ) / (b.n * b.n + b.d * b.d ), (d * b.n + n * b.d )/(b.n * b.n + b.d * b.d));
return c;
}
friend Complex operator+(const Complex &a, const Complex &b)
{
return Complex(a.n + b.n , a.d + b.d);
}
friend Complex operator-(const Complex &a, const Complex &b)
{
return Complex(a.n - b.n , a.d - b.d);
}
friend Complex operator*(const Complex &a, const Complex &b)
{
return Complex(a.n * b.n - a.d * b.d ,a.n * b.d - a.d * b.n );
}
friend Complex operator/(const Complex &a, const Complex &b)
{
if(b.n == 0)
{
throw NullDivision();
}
return Complex((a.n * a.d + a.d * b.d ) / (b.n * b.n + b.d * b.d ), (a.d * b.n + a.n * b.d )/(b.n * b.n + b.d * b.d));
}
friend std::ostream& operator<< (std::ostream& o, const Complex &a)
{
o << "(" << a.n << "/" << a.d << ")";
return o;
}
};
main.cpp :
#include <iostream>
#include "Komplex.h"
using namespace std;
int main()
{ bool fut = false;
int szam;
while (fut == false){
cout << "1.Komplex számok összeadása" << endl;
cout << "2.Komplex számok kivonása" << endl;
cout << "3.Komplex számok szorzása"<< endl;
cout << "4.Komplex számok osztása"<< endl;
cout << "5.Kilépés"<< endl;
cout << "Írjon be egy sorszámot!"<< endl;
cin >> szam;
if(szam == 5)
{
fut=true;
break;
}
cout << endl;
Complex n, d;
cout << "Adja meg az első szám valós részét" << endl;
cin >> n.a; // Problem
cout << "Adja meg az első szám képzetes részét" << endl;
cin >> n.b; // Problem
cout << "Adja meg a második szám valós részét" << endl;
cin >> d.a; // Problem
cout << "Adja meg a második szám képzetes részét" << endl;
cin >> d.b; // Problem
Complex eredmeny;
switch(szam){
case 1:
eredmeny = n + d;
cout << "Az eredmény:" << eredmeny.a << + eredmeny.b << "i" << endl << endl;
break;
case 2:
eredmeny = n - d;
cout << "Az eredmény:" << eredmeny.a << + eredmeny.b << "i" << endl << endl;
break;
case 3:
eredmeny = n * d;
cout << "Az eredmény:" << eredmeny.a << + eredmeny.b << "i" << endl << endl;
break;
case 4:
try {
eredmeny = n / d;
cout << "Az eredmény:" << eredmeny.a << + eredmeny.b << "i" << endl << endl;
}
catch(NullDivision e){std::cout << "NullDivision"<< std::endl;}
std::cout << std::endl;
break;
}
}
return 0;
}
Your Complex class has two member variables, n and d. You appear to be trying to put values into the (non-existent) a and b member variables.
That's not going to end well :-)
I would suggest, for a start, using variable names that make your intent clearer. For example, member variables n and d would be far better named as something like m_real and m_imag (n and d look like they should represent numerator and denominator, but that has nothing to do with complex numbers(a)).
By using decent variable names, reading of the code should be enough to figure out what's happening and you're unlikely to get confused between member variables and objects of the class (which also shouldn't be named n and d).
(a) To be honest, it looks like you've re-tasked some code meant to do rationals to be used as complex numbers. I base this on the variable naming and the fact your Complex constructor has:
Complex(int _n = 0, int _d = 1) : n(_n), d(_d)
{
if(d == 0)
{
throw NullDivision();
}
}
I can see no reason why the imaginary part of a complex number would default to one, nor why you would throw a NullDivision exception if it was zero. That would basically remove the entire set of real numbers from your complex class.
So, it's even more important that you use better names so that you can figure out why these re-tasked things are wrong.
I'm going to give you an example of how a professional developer would code up something like this (albeit without the copious comments I normally have).
I wouldn't suggest using this if this is educational classwork since you're likely to get pinged for plagiarism but it should serve as a guide on how to do it.
#include <iostream>
class ZeroDivision : public std::exception{};
struct Complex{
public:
Complex(double real_bit = 0.0, double imag_bit = 0.0)
: m_real(real_bit)
, m_imag(imag_bit)
{}
friend Complex operator+(const Complex &me, const Complex &them) {
return Complex(
me.m_real + them.m_real,
me.m_imag + them.m_imag);
}
friend Complex operator-(const Complex &me, const Complex &them) {
return Complex(
me.m_real - them.m_real,
me.m_imag - them.m_imag);
}
friend Complex operator*(const Complex &me, const Complex &them) {
return Complex(
me.m_real * them.m_real - me.m_imag * them.m_imag,
me.m_real * them.m_imag + me.m_imag * them.m_real);
}
friend Complex operator/(const Complex &me, const Complex &them) {
if (them.m_real == 0 && them.m_imag == 0)
throw ZeroDivision();
return Complex(
(me.m_real * them.m_real + me.m_imag * them.m_imag) / (them.m_real * them.m_real + them.m_imag * them.m_imag),
(me.m_imag * them.m_real - me.m_real * them.m_imag) / (them.m_real * them.m_real + them.m_imag * them.m_imag));
}
friend std::ostream& operator<<(std::ostream& os, const Complex &var) {
const char *sep = "+";
if (var.m_imag < 0)
sep = "";
os << "(" << var.m_real << sep << var.m_imag << "i)";
return os;
}
private:
double m_real, m_imag;
};
#include <iostream>
using namespace std;
int main() {
Complex c1(19.65, 3.142);
Complex c2(19.68, 2.718);
Complex c3(2, 0);
Complex c4(0, 2);
Complex c5(0, 0);
cout << c1 << " + " << c2 << " = " << (c1 + c2) << '\n';
cout << c1 << " - " << c2 << " = " << (c1 - c2) << '\n';
cout << c1 << " * " << c2 << " = " << (c1 * c2) << '\n';
cout << c1 << " / " << c2 << " = " << (c1 / c2) << '\n';
cout << c1 << " / " << c3 << " = " << (c1 / c3) << '\n';
cout << c1 << " / " << c4 << " = " << (c1 / c4) << '\n';
try {
cout << c1 << " / " << c5 << " = " << (c1 / c5) << '\n';
cout << "Did NOT successfully catch divide-by-zero\n";
} catch (ZeroDivision &exc) {
cout << "Successfully caught divide-by-zero\n";
}
return 0;
}
As part of this, I:
Got rid of superfluous stuff. There's little point having add, sub, and so on, when you can just use the built-in operators.
Created all complex numbers using the constructors (or operators). You should not be trying to change a classes internal (private) data. If you must do that, use a setter function.
Made a (very slight) adjustment to your output stream code to improve the format. And again, this is what you should use to output an object, not attempting to directly access private data.
Fixed the divide-by-zero detection. This only happens if both the real and imaginary parts are zero whereas you raised it if either were. That would bean you could never divide something by two since the imaginary part is zero, (2+0i).
Made the parts floating point rather than integers. You can change them back if you wish but the floating point is probably better in a general purpose class.
Fixed your multiplication and division formulae.
To explain that final bullet point, what you had for multiplication a * b was, where a and b are the complex numbers, R is the real part, I the imaginary:
R = a.R * b.R - a.I * b.I # correct.
I = a.R * b.I - a.I * b.R # should be adding, not subtracting
^
For division a / b, you had:
R = (a.R * a.I + a.I * b.I) / (b.R * b.R + b.I * b.I)
(a.R * a.R + a.I * b.I) / (b.R * b.R + b.I * b.I) <- should be
^
I = (a.I * b.R + a.R * b.I) / (b.R * b.R + b.I * b.I)
(a.I * b.R - a.R * b.I) / (b.R * b.R + b.I * b.I) <- should be
^
The run of that test code generates these results (reformatted for readability):
(19.65+3.142i) + (19.68+2.718i) = (39.33+5.86i)
(19.65+3.142i) - (19.68+2.718i) = (-0.03+0.424i)
(19.65+3.142i) * (19.68+2.718i) = (378.172+115.243i)
(19.65+3.142i) / (19.68+2.718i) = (1.00142+0.021348i)
(19.65+3.142i) / (2+0i) = (9.825+1.571i)
(19.65+3.142i) / (0+2i) = (1.571-9.825i)
(19.65+3.142i) / (0+0i) = Successfully caught divide-by-zero
void ComplexNum::printComplexNum()
{
if (imaginary = 1)
{
return ""; //cannot return "" I've also tried imaginary == ""; to no avail
}
cout << "(" << noshowpos << real << showpos << imaginary << "i)" << endl;
}
I have a complex number program, and when I want to display complex numbers it shows them incorrectly in the sense that (4-1i) should be shown as (4-i). While (4-1i) is technically correct, it is not displayed like I want it to be displayed. I created a simple if-statement inside a print method but it is not working because the variable imaginary is not a string. *How do I make it so that when my variable imaginary is equal to 1 that it returns "" or blank or nothing so that it can just print out the appropriate "i)" that I have set.
SAMPLE OUTPUT:
First Complex Number:
Enter real part of complex number: 2
Enter imaginary part of complex number: -3
Form '(a+bi)': (2-3i)
Second Complex Number:
Enter real part of complex number: 4
Enter imaginary part of complex number: -4
Form '(a+bi)': (4-4i)
The addition of the two Complex Numbers is: (6-7i)
The difference of the two Complex Numbers is: (-2+1i)
The product of the two Complex Numbers is: (-4-20i)
First Complex Number Squared: (-5-12i)
Second Complex Number Squared: (0-32i)
Notice how the difference is (-2+1i)...I don't like that. I don't want that. Also, I don't want that (0-32i). So basically when it's 0 or when it's 1 I would like the print function to reflect that. So the difference would look like (-2+i) and the Second Complex Num Squared would look like (32i)
Now onto my code:
class ComplexNum
{
public:
ComplexNum(float = 0.0, float = 0.0); //default constructor that uses default arg. in case no init. are in main
void getComplexNum(); //get real and imaginary numbers from keyboard
void sum(ComplexNum a, ComplexNum b); //method to add two ComplexNum numbers together
void diff(ComplexNum a, ComplexNum b); //method to find the difference of two complex numbers
void prod(ComplexNum a, ComplexNum b); //method to find the product of two complex numbers
void square(); //squares values of a and b when called in main
void printComplexNum(); //print sum, diff, prod, square
void formComplexNum(); //and "a+bi" form
private:
float real; //float data member for real number (to be entered in by user)
float imaginary; //float data member for imaginary number (to be entered in by user)
float realSquare; //squared real number data member for square method
float imaginarySquare; //squared imaginary number data member for square method
};
And the driver:
int main()
{
ComplexNum a, b, c, d, e, f, g;
cout << "First Complex Number:" << endl;
a.getComplexNum();
a.formComplexNum();
cout << endl;
cout << "Second Complex Number:" << endl;
b.getComplexNum();
b.formComplexNum();
cout << endl;
c.sum(a, b);
c.printComplexNum();
d.diff(a, b);
d.printComplexNum();
e.prod(a, b);
e.printComplexNum();
cout << "First Complex Number Squared: ";
a.square();
cout << "Second Complex Number Squared: ";
b.square();
cout << endl;
system("PAUSE");
return 0;
}
You have marked this as C++
(4-1i) should be shown as (4-i)
You might find std::stringstream helpful. It simplifies the special handling for the imaginary part:
virtual int foo()
{
std::cout << std::endl;
show(4, -2);
show(5, -1);
return(0);
}
void show(int real, int imaginary)
{
std::stringstream ss; // default is blank
if (-1 == imaginary) { ss << "-i)"; }
else /* (-1 != imaginary)*/ { ss << imaginary << "i)"; }
std::cout << "("
<< std::noshowpos << real
<< std::showpos << ss.str()
<< std::endl;
}
with output
(4-2i)
(5-i)
void ComplexNum::printComplexNum()
{
if (imaginary == -1 && real == 0)
{ cout << "(-i)" << endl; }
else if (imaginary == 1 && real == 0)
{ cout << "(i)" << endl; }
else if (imaginary == -1)
{ cout << "(" << noshowpos << real << "-i)" << endl; }
else if (imaginary == 1)
{ cout << "(" << noshowpos << real << "+i)" << endl; }
else if (real == 0 && imaginary == 0)
{ cout << "(0)" << endl; }
else if (real == 0)
{ cout << "(" << noshowpos << imaginary << "i)" << endl; }
else if (imaginary == 0)
{ cout << "(" << noshowpos << real << ")" << endl; }
else
{ cout << "(" << noshowpos << real << showpos << imaginary << "i)" << endl; }
}
Accounts for -1i, 1i, 0i, 0real, both 0i and 0real
The ultimate if-else block. There's no other way around this.
First, when you're checking if two things are equal, you want to use == or .equals(). In this case you want ==, because you're comparing primitives.
Next, this function is a void function. That means you've defined it as not returning anything, but you're trying to return an empty string.
What you want to do is print one pattern when imaginary is 1, and a different pattern when imaginary isn't 1. Use a print statement, like you do in your default case, to print the special case where imaginary = 1.
I'm sure I'm doing something wrong, but I just can't figure it out. I've created an object with integer data members, and I want to have a member function return the quotient of it's members as a floating point value, which it does. It then appends some additional stuff. The output is below the program, which should run as is.
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
class Rational
{
public:
explicit Rational(int = 0, int = 1);
double getRationalAsDouble() const;
private:
int numerator;
int denominator;
};
Rational::Rational(int numerator, int denominator)
{
if (denominator == 0)
this->denominator = 1;
else
this->denominator = denominator;
this->numerator = numerator;
}
// ******* Problem Function *********
double Rational::getRationalAsDouble() const
{
double a = 0.0, b = 0.0;
a = static_cast<double>(numerator);
b = static_cast<double>(denominator);
cout << endl << "a = " << a;
cout << endl << "b = " << b;
cout << endl << "a/b = " << (a/b);
}
// ******** End Problem Function ********
int main()
{
{ //Create a new Scope so that I can view Destructor Message, not used here
Rational c(2, 6);
int data = 10;
cout << c.getRationalAsDouble(); // prints rational object c as double, but not really
cout << "\n\n";
} // End of Scope
return 0;
} // end main
And here's the output:
a = 2
b = 6
a/b = 0.3333332.31196e-317
I've been playing around, and if I change the function to have any regular division in it, it works fine. What's really interesting is if I add any output after the cout << endl << "a/b = " << (a/b); line, that output is handled before (a/b) part of the line. Any help would be greatly appreciated! Thank you in advance for your time.
Solution:
The function wasn't returning anything. When the code was changed to:
double Rational::getRationalAsDouble()
{
return static_cast<double>(numerator)/denominator;
}
It worked as expected. Thank you tc.
Three problems:
You want to print endl at the end of the line, not the "beginning". Your code ends up doing (effectively) cout << endl << "a/b = " << (a/b); ... cout << c.getRationalAsDouble(); cout << "\n\n"; which prints the two doubles 0.333333 and 2.31196e-317 next to each other with no space.
You want (perhaps) cout << "\n" << endl instead of cout << "\n\n". endl causes the stream to be flushed; plain "\n" might not.
Rational::getRationalAsDouble() is not returning a value. Listen to your compiler warnings.
The fix looks something like
double Rational::getRationalAsDouble() const
{
double a = 0.0, b = 0.0;
a = static_cast<double>(numerator);
b = static_cast<double>(denominator);
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "a/b = " << (a/b) << endl;
return a/b;
}
Your implementation of Rational::getRationalAsDouble() can be simplified to:
double Rational::getRationalAsDouble() const
{
return 1.0*numerator/denominator;
}
I think you had everything else there for debugging purposes, and hence are not really needed.
I was wondering if anyone could help me with a more elegant way to code this program that I am writing please. The code I have is bellow, I would like to know if there is a way to separate the part that prints out the totals into a new function. I have tried but I always just get the total is 0, so I must be passing things wrong or something.
void printNumbers(int x, double y, double z, double v, int sum, double sum2,int sum3,int sum4){
while(x != 0){
y = sqrt (x);
z = pow (x,2);
v = pow (x,3);
sum = sum + x;
sum2 = sum2 + y;
sum3 = sum3 + z;
sum4 = sum4 + v;
cout << " " << x << setw(12) << setprecision (4) << y << setw(8) << z << setw(8) << v << endl;
x--;
}
cout << " total is" << sum << setw(12) << sum2 << setw(8)<< sum3 << setw(8) << sum4 << endl;
}
This is what I tried, at the time I only had one total to get, but It still did not work just gave the answer 0:
void printFooters(int sum){
cout << " " << "====================================="<< endl;
cout << "Totals " << sum << endl << endl;
cout << " " << "====================================="<< endl;
}
This is how I was calling it in main():
printFooters(sum);
You need to make the sums into references if you want them to be updated.
void printNumbers(int x, double y, double z, double v, int& sum, double& sum2,int& sum3,int& sum4)
If you don't the sums are passed by value, so you just get a copy of the current value of the sums.
Alternatively you can use pointers to the sums, but that would involve changing the syntax when accessing the sum variables.
You should decide first, what variables are input and what variables should carry output. Try to not use one variable for both input and output, it is often more confusing than valuable.
Only input value is x. Everything else is output values. In way you have used that values, content of values is modified only in local copy inside function printNumbers(). They will get lost on end of function. I expect you want to output computed results from function (however then printNumbers is wrong name for that function).
C and C++ always pass variables to function parameters by value. That means, variable is initialized from parameter, but any changes are done only inside function. At the end of function, copy is discarded and will NOT change anything you passed to it.
If you want output from function, you can use return, or use pointers or references to a variable. I suggest to use references in C++, they are easier to understand and easier to use.
Use references instead of copied variables in function. Then, when you modify that value inside function, it will keep modified value after function return.
void f1(int in, int out)
{
out = in + 1;
}
void f2(int in, int &out)
{
out = in + 1;
}
int o1=-1, o2=-1;
f1(1, o1);
f2(1, o2);
cout << o1 << "," << o2 << endl; // will print -1,2
So declare your function as:
void printNumbers(int x, double &y, double &z, double &v, int &sum, double &sum2,int &sum3,int &sum4);
you can then do:
double y,z;
int sum, sum2, sum3, sum4;
printNumbers(4, y, z, sum, sum2, sum3, sum4);
printFooters(sum);
And this time, it should print whatever printNumbers computed. See http://www.cprogramming.com/tutorial/references.html for a bit more, or use google.
You could batch all those parameters into structs, and separate all the calculations and outputs.
This is probably overkill in your case, and is quite a bit of code, but anyway...
struct Powers
{
double sqroot;
int one;
double square;
double cube;
};
Powers calculatePowers(int x)
{
Powers powers;
powers.sqroot = sqrt(x);
powers.one = x;
powers.square = x * x;
powers.cube = x * x * x;
return powers;
}
struct Sums
{
int sum1;
double sum2;
int sum3;
int sum4;
};
Sums addPowers(Sums sums, Powers powers)
{
sums.sum1 += powers.one;
sums.sum2 += powers.sqroot;
sums.sum3 += powers.square;
sums.sum4 += powers.cube;
return sums;
}
void printPowers(Powers powers)
{
cout << " " << powers.one
<< setw(12) << setprecision (4) << powers.sqroot
<< setw(8) << powers.square
<< setw(8) << powers.cube
<< endl;
}
void printTotals(Sums sums)
{
cout << " total is"
<< sums.sum1
<< setw(12) << sums.sum2
<< setw(8) << sums.sum3
<< setw(8) << sums.sum4
<< endl;
}
void doEverything(int x)
{
Sums sums = {0, 0, 0, 0};
while (x > 0)
{
Powers powers = calculatePowers(x);
printPowers(powers);
sums = addPowers(sums, powers);
x--;
}
printTotals(sums);
}