Need Help implementing this algorithm

How do you read variables into the command lines?
Prompt:
Make a program that implements this algorithm in a function. The main program should accept the value of n on the command line without prompting. The standard output (cout) should be exactly one line consisting of a statement of n and the result terminated by a newline. In addition, the standard error output (cerr) should be exactly one line consisting of n followed by a space followed by the number of basic operations terminated by a newline. So, a run of the program with an input of 5 would look exactly like this:
$ ./filename 5
The highest power of 2 in 5 is 4. this is cout
5 27 this is cerr
but note that 5 and 27 are not necessarily correct.

Here is an algorithm for finding the largest power of 2 smaller than or equal to a non-negative integer n.

i = n;
j = i & (i - 1);
while( j != 0 )
{
i = j;
j = i & (i - 1);
}
At the end of the loop, i is the largest power of 2 smaller than or equal to the input value n. In this algorithm, & is the bitwise and operator (not the boolean && operator).
Here is what I have so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

#include <iostream>

using namespace std;

int main(int argc)
{
   uint i, j, n;

   i = n;
   j = i & (i - 1);
   while (j != 0)
   {
      i = j;
      j = i & (i -1);
   }
   return 0;
}
Hello jlin55,

This would also be useful since you do not have function.

http://www.cplusplus.com/doc/tutorial/functions/

If you are still confused we will go through it.

Hope that helps,

Andy
you can also do it with a few (3, I think?) back to back logarithm commands, change the base to base 2 and extract the answer, no loop required.

Here is what I have so far:

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

using namespace std;




int main(int argc, char *argv[])
{
   
   
   
   
   
   cout << "the highest power of 2 in " << n << "is" << << endl;
   
   cerr << alg(count) << endl; 
   return 0;
   
}
int alg(uint n)
{
   if (argc != 2)
   {
      cerr << "error " << endl;
   }
   uint i = atoi(argv[1]);
   uint j = i & (i - 1);
   
   uint count = 1;
   while (j != 0)
   {
      i = j;
      j = i & (i -1); //counting this
      count++;
   }
}
Last edited on
Is there a there way to set up my function?
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
#include <iostream>
#include <string>

int main( int argc, char* argv[] )
{
    if( argc == 2 )
    {
        try
        {
            // try to convert argv[1] to an unsigned integer
            const unsigned long n = std::stoul( argv[1] ) ;

            unsigned long long pow2 = 1 ;
            int num_ops = 0 ;
            for( ; n >= pow2 ; pow2 <<= 1 ) ++num_ops ;
            // at this point, pow2 is the smallest power of two greater than n
            // pow2/2 is is the largest power of two less than or equal to n
            // and the loop executed num_ops times

            std::cout << n << ' ' << pow2/2 << '\n' ;
            std::cerr << n << ' ' << num_ops << '\n' ;
        }
        catch( std::exception& ) { /* conversion failed: print appropriate error message */ }
    }

    else { /* display usage */ }
}
Last edited on
Is that code suppose to be similiar to mine? It looks like it doesn't.

What about this way?

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

using namespace std;

int main()
{
   
   
   
   
   
   cout << "the highest power of 2 in " << n << "is" << alg()<< endl;
   
   cerr << alg(count) << endl; 
   return 0;
   
}
int alg(int argc, char *argv[])
{
   if (argc != 2)
   {
      cerr << "error " << endl;
   }
   uint i = atoi(argv[1]);
   uint j = i & (i - 1);
   
   uint count = 1;
   while (j != 0)
   {
      i = j;
      j = i & (i -1); //counting this
      count++;
   }
}
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
#include <iostream>
#include <string>

void alg( unsigned int n )
{
    int cnt = 0 ; // number of operations

    unsigned int i = n;
    unsigned int j = i & (i-1);
    while( j != 0 )
    {
        ++cnt ;
        i = j;
        j = i & (i-1);
    }
    // At the end of the loop, i is the largest power of 2
    // smaller than or equal to the input value n.

    // The standard output (cout) should be exactly one line consisting
    // of a statement of n and the result terminated by a newline.
    std::cout << n << ' ' << i << '\n' ;

    // the standard error output (cerr) should be exactly one line consisting
    // of n followed by a space followed by the number of basic operations
    // terminated by a newline.
    std::cerr << n << ' ' << cnt << '\n' ;
}

int main( int argc, char* argv[] )
{
    if( argc == 2 )
    {
        try
        {
            // try to convert argv[1] to an unsigned integer
            const unsigned long n = std::stoul( argv[1] ) ;

            alg(n) ; // compute and print the esults
        }
        catch( std::exception& ) { /* conversion failed: print appropriate error message */ }
    }

    else { /* display usage */ }
}
Topic archived. No new replies allowed.