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.
ncoming from?printf("count = %d, largest run = %d\n", n, count(n));. Not fromargv.uint64_trather thanint64_tnvisible inmain.n = atoll(argv[1])