Answer:
Step-by-step explanation:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
// Functions
////////////////////////////////////////////////////////////////////////////////
//
// Function : main
// Inputs : argc - the number of command line parameters
// argv - the parameters
// Outputs : 0 if successful test, -1 if failure
// Function : display
// arr : integer array to be printed
// Return Type : void, prints the contentes of array
// Function : swap_int
// Takes two unsigned short int pointers to values to be swapped
// Return Type : void, swaps the two values
// Function : reverseBits
// num : integer number whose bits are to be reversed
// Return Type : int, number with bits reversed
// Function : binaryString
// num : integer number whose binary representaion is needed
// Return Type : int, binary representation of num
void display_array(int arr[])
{
for (int i = 1; i < 11; i++)
{
printf("%d ", arr[i - 1]);
}
printf("\\");
}
void swap_int(unsigned short *a, unsigned short *b)
{
if (a == b)
return;
*a = *a + *b;
*b = *a - *b;
*a = *a - *b;
}
int reverseBits(int num)
{
int reverse_num = 0;
while(num != 0)
reverse_num <<= 1;
reverse_num
return reverse_num;
}
void binaryString(int num)
{
if (num > 1)
binaryString(num >> 1);
printf("%d ", num & 1);
}
int main(int argc, char *argv[])
{
// Local variables
// NOTE: this is where you will want to add some new variables
int int_array1[10], int_array2[10];
unsigned short uint_array1[10];
int i;
//????
if (argc < 11)
{
printf("Exiting the program, missing input");
return 0;
}
// Step a - read in the integer numbers to process
for (i = 1; i < 11; i++)
{
int_array1[i - 1] = atoi(argv[i]);//converting input to integer
}
// Step b - Convert numbers into positive values by taking their
// absolute values and save them in int_array2.
for(int i = 1; i < 11; i++)
{
if(int_array1[i - 1] < 0)
int_array2[i - 1] = (-1) * int_array1[i - 1];
else
int_array2[i - 1] = int_array1[i - 1];
}
// Print all numbers in a single line using display_array function
// ????
display_array(int_array2);
// Step c - Convert these positive integers to numbers
// in the range 0,…,128 by implementing the mod operation
// save them back into int_array2.
// Print all numbers in a single line using display_array function
for(int i = 1; i < 11; i++)
{
int_array2[i - 1] = int_array2[i - 1] % 129;
}
display_array(int_array2);
// Step d - for each integer in int_array2 print:
// number, number of 1 bits, even or odd
for(int i = 1; i < 11; i++)
{
int num = int_array2[i - 1];
int count_1_bits = 0;
while(num != 0)
{
if(num % 2 == 1)
count_1_bits++;
num = num >> 1;
}
printf("number = %d, number of 1 bits = %d", int_array2[i - 1], count_1_bits);
if(int_array2[i - 1] % 2 == 0)
printf(" even\\");
else
printf(" odd\\");
}
// Step e - Cast each element of int_array2 to unsigned short
// and store them into uint_array1.
for(int i = 1; i < 11; i++)
{
uint_array1[i - 1] = (unsigned short) int_array2[i - 1];
}
// Step f - Reverse the order of array elements in uint_array1
// using swap_int function.
/*swap_ints(): function should swap the numbers without using
temp variable*/
for(i = 0; i < 10 / 2; i++)
{
swap_int(&uint_array1[i], &uint_array1[10 - i - 1]);
}
// Step g - Update each element of uint_array1 by using reverseBits function.
/* reverseBit(): The function should return the number (in
integer format) whose bits are reversed,
i.e., the top bit of the original number is
the bottom bit of the returned number, the
second from the top bit of the original
number is the second to the bottom bit of
the returned number. */
for(i = 1; i < 11; i++)
{
uint_array1[i - 1] = (unsigned short) reverseBits(uint_array1[i - 1]);
}
// Step h - Print each element of uint_array1 in a separate line along with
// binary representation of each of the numbers using binaryString function.
/* binaryString():This function should fill the text string
with a binary representation of the number
suitable for printing. You should be using
some bitwise operations to achieve this,
maybe shifting(<<) and and (&)
operation
*/
for(i = 1; i < 11; i++)
{
printf("number = %u and binary representation = ", uint_array1[i - 1]);
binaryString(uint_array1[i - 1]);
printf("\\");
}
// Return successfully
return(0);
}