Question: 6. (20 points) Here are fou C programs that count the bits on in an integer, that is, you run ' Cb5 and it will
6. (20 points) Here are fou C programs that count the bits on in an integer, that is, you run ' Cb5 and it will print ' 2 ' (becatse 5 in binary is - 101 , 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 fratest 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 stdlib, b> int Dain( int argc, char *argu[) t // varning: assunes argv[1]! int val =atoi(argv[1]); int sun =0; for (int 1=0;1=0;1) if (val \& 11) sum +=1; printf ("\%d ", sum); \} Program C Works as is. Neets fixing: \#include stdio.h> "include stdlib. h> int main(int argc, char *argv[]) i. // rarning: assuses argv[1]! int sum=0; while (val) f if ( val int main(int arge, char *argv[]) i // varning? assumes argv[1]! int val=strtoul(argv[1], NULL. 10); int sum=0; while (val) ( sun+m val t1; val 0 i; \} printf(" /d ", sum); \}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
