• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

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

0
spngegirl01
Asked:
spngegirl01
1 Solution
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now