You must use pointers and dynamic allocation to complete the exercise to receive points.

Question

You must use pointers and dynamic allocation to complete the exercise to receive points.
Write a program that takes a positive integer as input, indicating the number of bits that you will have in your array

  1.  Create a dynamic integer array based on the value of the input integer. Fill this array with 0’s If the input integer is less than 1. The program will output’ invalid Size Exiting Program..’ and exit the program.
  2.  In the main() you will accept three more integers from the user. Each input will represntt intervals at which the bits will bw flipped. After each input. You will call the FlipBit(). In this function; you will iterate through the array, flipping bits based on the input, always starting from the bit at index 0 (which means this one flips every time). Flipping a bit means if the value is 1,then change it 0 and vice versca. The value should always be either 0 or 1.
    For example, if the user enters 2 into the zero array of size 6, then it would look like this:
    101010
    Every 2nd bit will be flip, starting with the first bit in example. If the input was 3, then every 3rd bit be flip, starting with the first bit in the array do NOT reset the array to back to all 0s after each input, The only time you will change the content in the array is in the flipBit().
  3. 3] After each time you call the flipBit(), you will then output or print the whole array to show what bits were flip.

If the user inputs a number that is larger than or equal to the array size or if they input a number that is less than 1, You must output ‘Invalid number of bits to be skip has been enter’ and then continue the program.

Example Program:

6 2 3 7 //Input of array size first, then the bits that are the skip. The initial array (size 6) is  000000
101010 //Flip every 2nd bot (index0, 2, and 4)
00111 // Flip every 3rd bit (index 0 and 3)
An invalid number of bits to skip has been enter. //7 is larger than the array size.
You MUST define and call a flipBit() function to flip the bits in the array. The function takes as input an int pointer to the dynamic array, the size of the array, and the number of bits to skip
void flipBit (int *array, int size, int nskips);
NOTE THAT, you will receive 20 points deduction if you do not release the dynamic array at the end of the program or your program has a memory leaking issue during execution.

Summary

Here one array has been creating whose size will be entered by the user. Where the size of the array will be set to the memory. Next here is one function naming flipBit where every time the array will get execute the bit will get flip and each flipped bit will get printed on the screen.

Explanation

Basically, we define an array. Where its size will be defined by the user. Also, Size and the nSkip value will pass to the function naming as flipBit. Once the array starts to get execute there will be flipping off the bits such as loop will start to fetch execute and starts to fetch the elements from the array and check whether the element is 1 or 0.

Whatever the element will be function will flip it to its vice versa. That means if the element is 0 it will become 1. And if the element is 1 it will become 0. Once the flipping is done it will get print on the screen. When the user will enter the size of the array created is assigned with all the elements as 0. Also, we have passed the 3 skip intervals. Each time the function is called the 1 interval is passed.

Code

#include <iostream>
using namespace std;
/* here we implement the flipBit function */
void flipBit(int *array, int size, int nSkips)
{
    /* to check wheter the nSkips is valid or not */
    if(nSkips>size){
        cout<<"Invalid number of bits to be skipped has been entered"<<endl;
        return;
    }
    /* bit will get flipped according to intervals */
    for(int i=0;i<size;i+=nSkips)
    {
        if(array[i]==0)
        array[i]=1;
        else 
        array[i]=0;
    }
    /* printing the array */
    for(int i=0;i<size;i++)
    cout<<array[i];
    cout<<endl;
}
/* main code */
int main()
{
    int size;
    cin>>size;
    int *arr=new int[size];
    for(int i=0;i<size;i++)
    arr[i]=0;
    int nSkips1,nSkips2,nSkips3;
    cin>>nSkips1>>nSkips2>>nSkips3;
    cout<<"array (size "<<size<<") is set to ";
    for(int i=0;i<size;i++)
    cout<<"0";
    cout<<endl;
    flipBit(arr,size,nSkips1);
    flipBit(arr,size,nSkips2);
    flipBit(arr,size,nSkips3);
}

Output

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *