spngegirl01
asked on
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????
(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;
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Your program is crashing because you are ysing argv[1] which is not available.
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.
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.).
Also, if you're going to do bit counting, you might want to look into the bitwise operators (bitwise AND, OR, shift, etc.).
ASKER
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
3
Segmentation Fault (core dumped)
$
That is the error I receive for every integer I enter
ASKER
Never mind, I fixed the problem, thank you all so much!