14,451,065 members
Rate this:
See more:
I am still learn about algorithms, i have a homework. I must make an output

```Sum of : 1/2 + 1/4 + 1/6 - 1/8 + 1/10 + 1/12
Result : 0.975```

But output of my program
```Sum of : 1/2 + 1/4 + 1/6-1/8 + 1/10 + 1/12
Result : 0.975```

I dont know how to make space `negative sign`, if i use cout there will show twice negative sign.

my program

```#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
int i ,sign, p, q, n;
double x , S;
S=0;
cout << "Sum of :";
for (i=1; i <= 6; i++)
{
if ( (i % 4 == 0) && ( i > 1 ) ) // to make condition where the number become negative
{
sign = -1;

}
if ( ( i % 4 != 0 ) && ( i > 1 ) )  // to make condition where the number become positive
{
sign = 1;
cout << " + ";
}
if ( i == 1 ) // to prevent 1st number not show " + " symbol
{
sign =1;
}

p = sign*1;
q = ( 2 * ( i - 1 ) ) + 2;
cout << p << "/" << q;
x = ( 1.0 * p / q );
S = S + x;

}

cout << "\n" << S;
}```

I realize my program too many operation that not needed, could u help me make it more effecient ?

What I have tried:

Output of my program
```Sum of : 1/2 + 1/4 + 1/6-1/8 + 1/10 + 1/12
Result : 0.975```
Posted
Updated 19-Mar-16 7:25am
jeron1 18-Mar-16 10:34am

Shouldn't that be '- 1/4'? or am I missing something.
Sergey Alexandrovich Kryukov 18-Mar-16 20:00pm

Are you sure the problem should be solved in floating-point numbers?
Actually, it can be solved in rational numbers (I hope you know what is that), which only can make the problem look interesting (but still simple).
—SA
Arthur V. Ratz 21-Mar-16 2:05am

5.
Sergey Alexandrovich Kryukov 21-Mar-16 10:19am

Thank you, Arthur.
—SA
Arthur V. Ratz 22-Mar-16 0:29am

You're welcome
Arthur V. Ratz 20-Mar-16 10:47am

You can represent "-" negative sign as the "plus negative value", which is 1/2 + 1/4 + 1/6 + -1/8 + 1/10 + 1/12
Arthur V. Ratz 20-Mar-16 10:48am

Or if it is a string containing a sum of fractions, we'll need to use "polish notation" and stack as it has been discussed here: https://en.wikipedia.org/wiki/Polish_notation

Rate this:

## Solution 4

Quote:
I dont know how to make space `negative sign`, if i use cout there will show twice negative sign.
Your mistake is that you try to add a negative number instead of substract a positive number.
```#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
int i;
double x , S;
cout << "Sum of :";

// first
i = 2;
s= ( 1.0 / i );
cout << "1/" << i;

for (i=4; i <= 12; i+= 2)
{
x = ( 1.0 / i );
if ( (i % 8 == 0)) { // to make condition where substration
cout << " - ";
S = S - x;
} else {
cout << " + ";
S = S + x;
}
cout << "1/" << i;
}
cout << "\nResult : " << S;
}```
v2
Arthur V. Ratz 20-Mar-16 10:45am

Why aren't you using "polish notation" and stack ?
Patrice T 20-Mar-16 14:37pm

Why should I use a polish notation and stack ?
Does it make the program shorter or faster ?
Arthur V. Ratz 21-Mar-16 2:04am

If you need to compute the expression represented as string you'll need to use polish notation which makes the computation more efficient in this particular case.
Patrice T 21-Mar-16 2:12am

My code is a correction to code provided. And it don't need to compute what is in the string because the program build the string and compute on fly. So there is no need to analyze the string.
Arthur V. Ratz 21-Mar-16 2:15am

O'key. I agree with you. +5
Patrice T 21-Mar-16 2:24am

Do you know that +5 means that you upvoted the answer ?
So if you don't upvote 5 stars, there is no need to say +5
Arthur V. Ratz 21-Mar-16 2:25am

Patrice T 21-Mar-16 2:28am

Thank you
Arthur V. Ratz 21-Mar-16 2:29am

You're welcome. No problem.
Rate this:

## Solution 2

Please don't take this answer too seriously. It may or may not be appropriate.

Instead of a positive 1/4 minus 1/8, skip the 1/4 and use a positive 1/8. The resulting value is the same.

Sum of : 1/2 + 1/6 + 1/8 + 1/10 + 1/12
Result : 0.975

Otherwise, jeron's answer looks good to me.

If you *must* have all six terms, here's another take:

```#include <iostream>
#include <math.h>

#define countof(arg) ( (sizeof arg) / (sizeof arg[0]) )

int main ()
{
static const int denominators[] = {2, 4, 6, -8, 10, 12};
double S = 0.f;
std::cout << "Sum of :";
for (int i = 0; i < countof(denominators); i++)
{
int denom = denominators[i];
if (denom < 0)
std::cout << " - ";
else if (i > 0)
std::cout << " + ";
std::cout << 1 << '/' << abs(denom);
double term = 1.f / denom;
S += term;
}

std::cout << std::endl << S;
return 0;
}```
v3
Philippe Mori 18-Mar-16 21:52pm

In C++, one should avoid macro. You can easily replace `countof` macro by a template and it will be much safer.

See How Would You Get the Count of an Array in C++? or oven better with modern C++:
Obtaining the size of a C++ array using templates and other techniques (from C++11) - Part 2
Macros are dangerous as they can cause subtle bugs. For example, your macro would given wrong answer if you call it on a pointer.
Arthur V. Ratz 21-Mar-16 2:05am

5.
[no name] 21-Mar-16 13:46pm

This. This kind of feedback is why I participate on these forums. Thanks again Phillipe!

Unfortunately, VS.2013 does not seem to support constexpr. When I move to VS.2015, I will make use of this.

The _countof macro has been in in MSVC for a long time. Other compilers may not have it, so included the macro here.

https://msdn.microsoft.com/en-us/library/ms175773.aspx

"In C++, _countof will fail to compile if array is a pointer."
Arthur V. Ratz 22-Mar-16 0:47am

+5.
Philippe Mori 18-Mar-16 21:54pm

Your code does not take the absolute value of the denominator when printing... so would print `... - 1/-8 + ...`
Arthur V. Ratz 21-Mar-16 2:05am

5.
[no name] 21-Mar-16 13:30pm

Fixed. Thanks Phillipe!
Rate this:

## Solution 1

A quick attempt.
```#include <iostream>
#include <math.h>
using namespace std;
int main ()
{
int i, q;
double x , S;
S=0;
cout << "Sum of :";
for (i=1; i <= 6; i++)
{
if ( (i % 4 == 0) && ( i > 1 ) ) // to make condition where the number become negative
{
cout << " - ";
}
if ( ( i % 4 != 0 ) && ( i > 1 ) )  // to make condition where the number become positive
{
cout << " + ";
}
q = ( 2 * ( i - 1 ) ) + 2;
cout << 1 << "/" << q;
x = ( 1.0 / q );
S = S + x;

}

cout << "\n" << "Result = " <<S;
}```
v2
Philippe Mori 18-Mar-16 21:57pm

Not very optimal code...Thus you have only respond to half the question since OP also ask to make the code more optimal.
jeron1 19-Mar-16 11:18am

To be honest I wasn't looking to optimise just yet, he should get it working first, understand why it works, then look to optimise.
Philippe Mori 18-Mar-16 22:33pm

I have written an optimized version based on that code in my solution...
Rate this:

## Solution 3

A more optimal version of solution 1 code could be:

```    #include <iostream>
using namespace std;

int main ()
{
// You should initialize variable when you declare them (if possible).
double s = 0;

cout << "Sum of :";

for (int i = 1; i <= 6; ++i)
{
double sign = 1.0;
if (i % 4 == 0)
{
cout << " - ";    // Always display negative sign
sign = -1.0;
}
else if (i > 1)       // Skip initial (positive) sign...
{
cout << " + ";
}

int q = 2 *  i;
cout << 1 << "/" << q;
s += sign / q;

}

cout << "\n" << "Result = " << s;
}```

Corrections have been made in above code in bold so that 1/8 get substracted from the result.

That code is more optimal for mainly for the time it take to write or read it. At execution time, there won't be much difference. It is more typical of expert code that know what they do. For example, we only check once the modulo and skip first sign only if positive.

One could simplify code like that:
```if (i > 1)
{
cout << (i % 4 == 0 ? " - " : " + ");
}```

But that code is slightly less maintainable if first number is negative as more change would be required. Thus I would not recommend that change in this case.
v3
Philippe Mori 19-Mar-16 11:39am

It seems that code project editor has problems handling <. They were showing correctly in the preview but were displayed as &lt;
Philippe Mori 21-Mar-16 9:10am

I fix the above code to substract negative numbers from the result.