### error.

Hello!

I have a code where I need to input n number, and then programm must find numbers a, b,c which are a<=b<=c<=n and are working in this formula ( a^2+b^2=c^2)

but my code isnt working right. when I enter n value 5, programm just print me 0102030405 and so on.

Thanks for any help!

here is code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include #include using namespace std; int main () { int n ; int a,b,c; cin>>n; int mas [n]; for (int i=0; i<=n; i++) { mas [i]=i; } for (int i=0; i<=n;i++) { c=mas[i]; for(int z=0; z<=n;z++) { b=mas[z]; for (int x=0;x<=n;x++) { a=mas[x]; if (a*a+b*b==c*c) { cout << a,b,c; } } } } return 0; }``````
`cout<<a,b,c; //This evaluates to the value of c `
The comma operator in C++ is a little weird, and evaluates left to right, returning the right most value (when it is not used as a parameter separator in function calls:
http://msdn.microsoft.com/en-us/library/zs06xbxh(v=vs.80).aspx

So your code has just been outputting the values for c, and discarding a and b.

You want something like this:
 `` `` ``cout<< a << " : "<< b <<" : "<< c <
1.
 `` `` ``int mas [n];``
This is illegal. The length of an array can't depend on the value of a variable at run time. You're probably using GCC, which lets you get away with it, but you still shouldn't do it.

2.
 ``1234`` ``````for (int i=0; i<=n; i++) { mas [i]=i; }``````
This is a buffer overflow. i can reach n, but mas only goes from 0 to n-1.

3. What's the point of mas, if all it does is give you back the same number you put in, and you never modify it?

4.
 ``123`` ``````for (int i=0; i<=n;i++){ //... for(int z=0; z<=n;z++){``````
You're not respecting the conditions of the problem. z can at times be higher than i.

5.
 `` `` ``cout << a,b,c;``
I'm not too clear on the precedence of the comma operator, but this means either (cout << a),b,c or cout << (a,b,c). Probably the former.
The former prints a and then evaluates b and c to no effect, and the latter evaluates a and b to no effect, then prints c.
To print a, then b, then c, do
 `` `` ``std::cout <

6. Just an algorithmic note: You're going to get a lot of useless output if you start your loops at zero, since 0^2 + a^2 = a^2 is true for all a.
Last edited on
thanks for responding!

about condition a<=b<=c<=n , i just changed there:

for (int i=0; i<=n; i++)
{
mas [i]=i;
}

for (int i=0; i<=n;i++)
{
c=mas[i];

for(int z=0; z<=c;z++)
{
b=mas[z];

for (int x=0;x<=b;x++)
{

a=mas[x];

and now im getting right numbers.

Youre saying that int mas [n] is illegal? can you suggest what i need to change than?
Last edited on
I'd suggest just getting rid of mas, since it does nothing. The loops may as well be rewritten to
 ``1234567`` ``````for (int c=1;c<=n;c++){ for(int b=1;b<=c;b++){ for(int a=1;a<=b;a++){ //... } } }``````
Last edited on
@Helios:
<< is a function call and not an operator, so (a,b,c) must be evaluated before the function is called.
thanks! everything is working!
 << is a function call and not an operator, so (a,b,c) must be evaluated before the function is called.
Nope. Just tried it.
 ``12345`` ``````#include int main(){ std::cout <<1,2,3; }``````
Output: 1
Topic archived. No new replies allowed.