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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <stdio.h>
#include <math.h>
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 <<std::endl;
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.
1
2
3
4
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.
1
2
3
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 <<a<<"    "<<b<<"    "<<c<<std::endl;


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
1
2
3
4
5
6
7
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.
1
2
3
4
5
#include <iostream>

int main(){
	std::cout <<1,2,3;
}
Output: 1
Topic archived. No new replies allowed.