• C

Bit Counting Program in C

My assignment is:

(Exercise 11-4 from Practical C Programming, Steve Oualline)
"Write a program that counts the number of bits set in an integer. For example,
the number 5 (decimal), which is 0000000000000101 (binary), has two bits set."

More specifically, write a function, countBits() that takes an int as an
argument and returns an integer count of the number of set bits. Then write a
main function that reads an integer from the command line and outputs the
return value of countBits() to the screen.


I've rewritten the program from scratch and finally limited the number of small errors (mismatched variables, undefined terms, etc) but now there is some huge system failure that pops up every time I try to compile the program.  What is wrong????
#include <stdio.h>
#include <string.h>
 
int countBits(int a[], int b)
{
  int c;
  int d;
 
  c = 0;
 
  for (d=0; d<b; d++)
    {
      if(a[d] == 1)
        {c++;}
    }
  return c;
}
 
int main(int argc, char *argv[])
{
  int a;
int d;
int f;
int g;
int h;
int i;
int fil[10];
 char line[1000];
 
d = 0;
 
printf("Enter the integer whose count bit is to be found:\n");
fgets(line, sizeof(line), stdin);
sscanf(argv[1], "%d", &g);
 
while (g!=0)
  {
    f= g/2;
    i = g - (f*2);
  fil[d] = i;
    d++;
    g = f;
  }
 
a = countBits(fil, d);
 
printf("The set bits are %d\n", a);
 
return 0;
}

Open in new window

spngegirl01Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sunnycoderCommented:
Pls post the exact error message/number

fgets(line, sizeof(line), stdin);
sscanf(argv[1], "%d", &g);

Your number is in line ... you should be using

fgets(line, sizeof(line), stdin);
sscanf(line, "%d", &g);
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SreejithGCommented:
Your program is crashing because you are ysing argv[1] which is not available.
0
sunnycoderCommented:
Also, it is a good practice to use meaningful variable names ... names such as a,d,f,g,h are not easy to keep track of. Instead names such as num, count, num_bits are more meaningful.
0
Identify and Prevent Potential Cyber-threats

Become the white hat who helps safeguard our interconnected world. Transform your career future by earning your MS in Cybersecurity. WGU’s MSCSIA degree program was designed in collaboration with national intelligence organizations and IT industry leaders.

Infinity08Commented:
Btw, I don't think you're taking on this assignment correctly. You're passing an array of bit values to the countBits function, while the assignment said to pass an int value alone.

Also, if you're going to do bit counting, you might want to look into the bitwise operators (bitwise AND, OR, shift, etc.).
0
spngegirl01Author Commented:
Enter the integer whose count bit is to be found:
3
Segmentation Fault (core dumped)
$



That is the error I receive for every integer I enter
0
spngegirl01Author Commented:
Never mind, I fixed the problem, thank you all so much!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.