Write a C program that reads in a list of integers between -20000 and 20000 from a file named testdata47. Your program does not know how many integers there are in the file. Your program read these integers one by one and after each integer is read it will then print out the exact number of 1s that exists in the number’s twos complement binary representation. You will need a loop to read in the number from the file until end of file is determined. You will need another loop – inside this loop – to process each bit with a given number. One strategy could be to test the rightmost bit. See if it is a 1 or a 0. You can do this by seeing if the number is an odd number or not. If the number is odd, then a 1 exists in the rightmost position. You can then rely upon C’s right shift operator to move all of the bits to right one position. You could enter the innermost loop sizeof (int) * 8 times. Do you understand these words? Remember: The right shift operator in C does not guarantee that a 0 bit will be rolled in on the left side. You could test to see if the number is negative or not and use a left shift operator instead. The left shift operator in C always guarantees a 0 is rolled in.
Here is what I have so far....
/************************************************************************/
/* Programmer: Kyle Homan */
/* */
/* Program 47: Bits of Fun */
/* */
/* Approximate completion time: minutes */
/************************************************************************/
#include <stdio.h>
int main (int argc, char *argv[]) {
int n, num, bits = 0;
FILE *fin = fopen("testdata47", "r");
while (fscanf(fin, "%d", &n) != EOF) {
num = n;
if (n >= 0) {
while (n > 0) {
if (n % 2 == 1) bits = bits + 1;
n = n >> 1;
}
}
else {
while (n < 0) {
if (n % 2 == 1) bits = bits + 1;
n = n << 1;
}
}
printf("\nThere are %d 1's in %d.\n", bits, num);
bits = 0;
}
return (0);
}
Could somebody please help me with this?
Edited by WingedPanther, 05 December 2008 - 10:16 AM.
add code tags (the # button)


Sign In
Create Account

Back to top









