Question

In: Computer Science

Given the following C function prototype which accepts an integer argument, complete the implementation of the...

Given the following C function prototype which accepts an integer argument, complete the implementation of the function in C language to return the number of 1’s in the binary representation of the number passed.

For example if the number is (011100011 ) then the function should return 5 as the number of 1s. Please remember that an integer is 32 bits long.

USE the following function int countOnes(int number)

Write a full c program that has the header required and will accept the user entry.

Solutions

Expert Solution

// Please save it as countOnes.c    Thanks
#include<iostream>
using namespace std;

int countOnes(int number)
{
   int count=0;
   int t = number;
   int remainder=0;

while (t != 0)
{
remainder = t % 10; // gets last digit
if(remainder==1) // if last digit is 1
{
       count++; // increase count
   }  
t = t / 10;
// by dividing 10, removes the last digit from number
// 1234/10 gives 123. because t is an integer.
}  
return count;
}

int main()
{
   int c, num;
   printf("\nEnter binary Number (consisting only 1's and 0's): ");
   scanf("%d", &num);   // get user input
   printf("\nNumber is %d", num);
   c = countOnes(num);   // get count into c
   printf("\nCount = %d ", c); // display value of c  
}
// -------------------- end of countOnes.c


Related Solutions

C++ (cpp) PLEASE Write a complete function (prototype and definition) that takes an integer array and...
C++ (cpp) PLEASE Write a complete function (prototype and definition) that takes an integer array and the array dimensions as an input. The function should then square each value of the array in-place. At the completion of the function return a Boolean which indicates the process has completed.
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should...
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should display if the argument "is a multiple of 5" or "is not a multiple of 5".
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns...
Python: Write a function named calc_odd_sum that accepts a positive integer as the argument and returns the sum of all the odd numbers that are less than or equal to the input number. The function should return 0 if the number is not positive. For example, if 15 is passed as argument to the function, the function should return the result of 1+3+5+7+9+11+13+15. If -10 is passes, it shall return 0. You shall run the program test the result at...
Python: Write a recursive function that accepts an integer argument, n. The function should display n...
Python: Write a recursive function that accepts an integer argument, n. The function should display n lines of asterisks on the screen, with the first line showing 1 asterisk, the second line showing 2 asterisks, up to the nth line which shows n asterisks. Test the function.
In c++ Write a function that: accepts a string and an integer as it's only arguments...
In c++ Write a function that: accepts a string and an integer as it's only arguments uses the argument to open a file for writing writes the integer to the file if it fails to open the file, returns -1 if it succeeds in opening the file, returns 0 does not interact with the user in any way does not use global variables
Using C++ Write a template function that accepts an integer parameter and returns its integer square...
Using C++ Write a template function that accepts an integer parameter and returns its integer square root. The function should return -1, if the argument passed is not integer. Demonstrate the function with a suitable driver program .
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusi...
Write a C or C++ program A6p2.c(pp) that accepts one command line argument which is an integer n between 2 and 4 inclusive. Generate 60 random integers between 1 and 49 inclusive and store them in a 5 by 12 two dimensional integer array (e.g.,int a[5][12];). Use pthread to create n threads to square all 60 array elements. You should divide this update task among the n threads as evenly as possible. Print the array both before and after the...
1) type a complete, working, C++ program that accepts two integer values from the variable num3,...
1) type a complete, working, C++ program that accepts two integer values from the variable num3, and store num1 raised to the power num2 into the variable num4. Then output the values of the four variables each on a separate line, formatted and with an appropriate message for each.keyboard into the variables num1 and num2, include prompts, store the square root of their sum into the program :
Using Python provide the implementation of a function called "isAdjacent" that accepts a string.   The function...
Using Python provide the implementation of a function called "isAdjacent" that accepts a string.   The function checks if two adjacent characters in the string are the same; if they are the same, then return True otherwise return False.   The function should ignore case and check for non-empty string.  If it's an empty string, then return the message 'Empty string'. Sample runs: print( isAdjacent('ApPle')) # True print( isAdjacent('Python')) # False print( isAdjacent('')) # Empty string
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through...
Write a function in JAVASCRIPT that accepts an array as argument. The function should loop through the array elements and accumulate the sum of ASCII value of each character in element and return the total. For example: function([‘A’, ‘bc’, 12]); // returns 361 which is the sum of 65 + 98 + 99 + 49 + 50 Use of any built in string functions or built in array functions is not allowed, Any help would be much appreciated
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT