Question: Here are four C programs that count the bits on in an integer; that is , you run ' . / cb 5 ' and
Here are four C programs that count the bits on in an integer; that is you run cb and it will print because in binary is and two bits are turned on For each
program, indicate whether it works. If it does not, show how to fix it by changing the fewest characters possible. Note that these programs will crash if not given an argument; that's not considered an error per the design spec.
Program A
Works as is
Needs fixing.
#include
#include
int mainint arge, char argv
warning: assumes argv
int val atoiargv;
int sum ;
for int i ; i ; i
if i val
sum ;
printfd
sum;
Program B
Works as is
Needs fixing.
#include
#include
int mainint argc, char argv
warning: assumes argv
int val atoiargv;
int sum ;
for int i ; i ; i
if i & val
sum ;
printfd
sum;
Program C
Works as is Needs fixing.
#include
#include
int mainint argc, char argv
warning: assumes argv
int val atoiargv;
int sum ;
while val
if val
sum ;
val ;
printfd
val;
Program D
Works as is Needs fixing.
#include
#include
int mainint argc, char argv
warning: assumes argv
int val strtoulargv NULL, ;
int sum ;
while val
sum val & ;
val ;
printfd n sum;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
