error message "ID RETURNED 1 EXIT STATUS

closed account (9G6fSL3A)
Trying to compile this however Im getting a message "ID RETURNED 1 EXIT STATUS" Does anybody know how to fix my code to solve this problem?

1
2
3
4
5
6
7
  int pow2(int n){
  int result=1;
  for (int i=0;i<n;i++){
    result*=2;
  }
  return result;
}
It's not ID, it is ld
http://www.freebsd.org/cgi/man.cgi?query=ld&sektion=1


> Does anybody know how to fix my code to solve this problem?

Write main()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
  int pow2(int n){
  int result=1;
  for (int i=0;i<n;i++){
    result*=2;
  }
  return result;
}

#include <iostream>

int main()
{
    std::cout << pow2(17) << '\n' ;
}
closed account (9G6fSL3A)
It gives an answer for this one

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int pow2(int n){
  int result=1;
  for (int i=0;i<n;i++){
    result*=2;
  }
  return result;
}

#include <iostream>

int main()
{
    std::cout << pow2(29) << '\n' ;
}



But but for this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
int pow2(int n){
  int result=1;
  for (int i=0;i<n;i++){
    result*=2;
  }
  return result;
}

#include <iostream>

int main()
{
    std::cout << pow2(30) << '\n' ;
}


Whenever I go over power 30 i get 0 as answer. Any way to change this?
Last edited on
pow2() returns an int; it can't return a value that is bigger than the largest value that an int can represent.

Try this:

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
#include <limits>
#include <iostream>

unsigned int pow2( unsigned int n )
{
    if( n >= std::numeric_limits<unsigned int>::digits ) return 0 ;

    unsigned int result = 1 ;

    // return result << n ;

    for( unsigned int i=0; i<n; ++i )
    {
        result *= 2 ;
    }

    return result;
}

int main()
{
    for( unsigned int n = 0 ; n < 128; ++n )
    {
        unsigned int result = pow2(n) ;

        if( result > 0 ) std::cout << "2^" << n << " == " << result << '\n' ;

        else
        {
            std::cerr << "the number 2^" << n
                       << " is too big to be represented by an unsigned int\n" ;
            return 0 ;
        }
    }
}


And then, replace unsigned int with unsigned long long and try it again: http://ideone.com/9R08mx
i've already told him this in another one of his posts.

OP, look at this table:
http://www.cplusplus.com/doc/tutorial/variables/
Topic archived. No new replies allowed.