0

I am doing computer science course which it is required for my Information Technology major. So I am trying to understanding this steps by step. I don't know how I did it wrong or isn't what the expected output.

Any suggestion or help? Thank you.

My code:

/**
 * Create a function called count that takes a 64 bit long integer parameter (n)
 * and another integer pointer (lr) and counts the number of 1 bits in n and
 * returns the count, make it also keep track of the largest run of
 * consecutive 1 bits and put that value in the integer pointed to by lr.
 * Hint: (n & (1UL<<i)) is non-zero when bit i in number n is set (i.e. a 1 bit)
 */


/* 1 point */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int count (uint64_t n)
{       
  int ret = 0;
  long x = n;
  if (x < 0)
    x = -x;
  while (x != 0)
    {
      ret += x % 2;
      x /= 2;
    }
  return ret;     //done summing when n is zero.
}

/**
 * Complete main below and use the above function to get the count of 1 bits
 * in the number passed to the program as the first command line parameter.
 * If no command line parameter is provided, print the usage:
 *   "Usage: p3 <int>\n"
 * Hints:
 * - Use atoll to get a long long (64 bit) integer from the string.
 * - Remember to use & when passing the integer that will store the longest
 *   run when calling the count function.
 *
 * Example input/output:
 * ./p3 -1
 * count = 64, largest run = 64
 * ./p3 345897345532
 * count = 17, largest run = 7
 */
int main (int argc, char *argv[])
{
  if (argc < 2)
    {
      printf ("Usage: p3 <int>\n");
    }
  int n = atoll(argv[1])
  printf("count = %d, largest run = %d\n", n, count(n));

}

When I run the compile to see the output but it doesn't seems correct to match the example output.

8
  • Where is n coming from? printf("count = %d, largest run = %d\n", n, count(n));. Not from argv. Commented Oct 31, 2018 at 19:15
  • First you should switch to uint64_t rather than int64_t Commented Oct 31, 2018 at 19:20
  • n is from the function, Commented Oct 31, 2018 at 19:21
  • 1
    There is no n visible in main. Commented Oct 31, 2018 at 19:26
  • You need something like: n = atoll(argv[1]) Commented Oct 31, 2018 at 19:28

2 Answers 2

2
  1. use atoll get int64_t from argv[1]
  2. use (n&(1UL<<i)) define each bit is 1 or 0
  3. use var to record the current consecutive 1 bits count

explain:

temp means current consecutive 1 bits count

  1. if n&(1UL<<i) == 1, current bit is 1, so current consecutive 1 bits count add 1, so ++temp;

  2. if n&(1UL<<i) == 0, current bit is 0, so current consecutive 1 bits count is 0, so temp = 0;

The following code could work:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

int count(int64_t n, int* lr) {
    *lr = 0;

    int temp = 0;
    int ret  = 0;

    for (int i = 0; i != 64; ++i) {
        if (n&(1UL<<i)) {
            ++ret;
            ++temp;
            if (temp > *lr)
                *lr = temp;
        } else {
            temp = 0;
        }
    }
    return ret;
}

int main (int argc, char *argv[]) {
    if (argc != 2) {
        printf ("Usage: p3 <int>\n");
        return -1;
    }

    int64_t n = atoll(argv[1]);
    int k;

    int sum = count(n, &k);

    printf("count = %d, largest run = %d\n", sum, k);

    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can you explain more about Pointer Integer?
@MikeODell I add a simple explain.
Never use atoll(), or any of the other ato*() functions. Per 7.22.1 Numeric conversion functions of the C standard: "The functions atof, atoi, atol, and atoll need not affect the value of the integer expression errno on an error. If the value of the result cannot be represented, the behavior is undefined." You can't tell if atoll() works or not as there's no error return. Not only that, if "the value ... cannot be represented", then the behavior is undefined. Use strtoll() instead.
1

What you've posted results in a compilation error:

main.c:52:44: error: ‘n’ undeclared (first use in this function)
   printf("count = %d, largest run = %d\n", n, count(n));
                                            ^

As the comments in your code suggest, you need to add the following line:

int n = n = atoll(argv[1]);

Modify your main function to look something like this:

int main (int argc, char *argv[])
{
    if(argc < 2)
    {
        printf ("Usage: p3 <int>\n");
    }
    else
    {
        int n = atoll(argv[1]);
        printf("count = %d, largest run = %d\n", n, count(n));
    }

    return 0;
}

If your count function is supposed to return the number of 1 bits in n your implementation won't work. Change the while body to the following:

ret += x % 2;
x /= 2;

3 Comments

Updated my code, but it's only show: count = -1, largest run = 1
That's because you're changing -1 to 1 here: if (x < 0) x = -x;. What answer do you expect?
I have 'if (x < 0) x = -x;' in function. So when I input -1 then the output should be 64 for count and 64 for largest run.. So here is for example, if it is 345897345532 then it would be 17 for count and 7 for largest run... Does this makes sense?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.