Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1.Implement the popCount function located in the header “hw1part1.h”. The popcou

ID: 3786608 • Letter: 1

Question

1.Implement the popCount function located in the header “hw1part1.h”. The popcount routine accepts an integer and then returns the number of 1’s in the binary representation of that number. Tests have provided in the accompanying c file.

int popCount( int val ){

// Count the number of ones in the binary representation of val

}

2.Part 2 Implement the getMode function located in the header “hw1part2.h”. The getMode routine accepts an array of integers and returns the value of the integer that occurs most frequently. Tests have been provided in the accompanying c file. You should submit your assignment the same way that we submit in class exercises.

int getMode( int arr[] , int size ){
// return the mode of the array
// you can assume that size will be less than 256
// you can assume that there will always be a clear mode (no special cases)
// you should assume that values will always be 0 or greater


}

Explanation / Answer

1.

#include <stdio.h>

int popCount( int val ){
int temp,i,ones,int_size;
// Count the number of ones in the binary representation of val
temp = val;
>
int_size = sizeof(int)*8;
for(i=0; i<int_size; i++)
{
//check if LSB is set then increment ones
if(temp & 1)
ones++;
  

//Right shift bits of temp to one position
temp >>= 1;
}

return ones;
}
int main(void) {
   int number,ones;
   printf("Enter the number");
   scanf("%d",&number);
  
   printf(" Total number of ones bits is %d (in %d byte integer representation) ",popCount(number) , sizeof(int));
  
   return 0;
}

output:

2.

#include <stdio.h>


int getMode( int arr[] , int size )
{
int maxValue = 0, maxCount = 0, i, j;

for (i = 0; i < size; ++i)
{
int count = 0;
  
for (j = 0; j < size; ++j)
{
if (arr[j] == arr[i]) // count the occurrence of a number with maximum times
++count;
}
  
if (count > maxCount)
{
maxCount = count;
maxValue = arr[i];
}
}

return maxValue;
}



int main(void) {
   int n = 5;
int array[] = {56,87,45,34,45,66,45};

printf("Mode = %d ", getMode(array,n));
  
   return 0;
}

output: