Question: Write a program in C that does the following. I will give upvotes!! 1. Read each bit in as a char using repeated calls to

Write a program in C that does the following. I will give upvotes!!

1. Read each bit in as a char using repeated calls to getchar() in a while loop.

2. Convert each char input bit to an int and store it in array. For this, you will need to be able to distinguish between the capacity of and the number of bits in an array.

3. Stop reading input when the user hits enter.

4. You must use the following functions for memory management: malloc, realloc and free.

5. You must grow the array by doubling its length each time its capacity is reached. You must use GROW BY for this.

6. For each call to malloc and realloc, you must check the return value for NULL. If NULL is returned, then you must print an appropriate error message and return from main with return value 1. You will probably not get malloc or realloc errors but I will check for this in your code.

7. You must use sizeof appropriately.

8. You must implement add one using bitwise operations (you may use standard arithmetic operations to increment a loop variable).

9. Do not forget to free all dynamically allocated memory before main returns.

10. Follow the input / output format exactly (see the Sample runs section).

Starting Code:

#include "stdio.h"

#include "malloc.h" // Needed for malloc, realloc and free

#define GROW_BY 2

// The following function interprets the bits in in_array

// as an integer in binary with lsb at in_array[num - 1]

// and msb at in_array[0]. The function returns with

// in_array updated to hold the bits of the result of

// adding one to the original value. The return value

// is the final carry (out) bit, e.g., if in_array[] = {0, 1, 1}

// and num == 3, then after add_one returns,

// in_array[] = {1, 0, 0} and the value 0 is returned.

// However, if in_array = {1, 1, 1} and num == 3, then

// after add_one returns, in_array[] = {0, 0, 0} and

// the value 1 is returned.

int add_one(int in_array[], int num);

int main() {

int* array;

return 0;

}

// Put the definition of add_one here:

Sample output:

Enter a num: 101011110

010100010

Enter a num: 000000000000000000000000000000000000000000001

111111111111111111111111111111111111111111111

Enter a num: 000000

1000000

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!