Home > Blockchain >  Enter an array that contains many zero elements. Replace all groups of consecutive zeros with a sing
Enter an array that contains many zero elements. Replace all groups of consecutive zeros with a sing

Time:01-19

#include <iostream> 
using namespace std;

int main()
{
    int size = 0, new_size=0, zero_streak = 0;

    cout << "Input length of an array: ";
    cin >> size;

    double *arr = new double[size];
    double* arr2{ new double[size] };

    cout << "\nInput elements of an array:\n";
    for (int index = 0; index < size; index  )
    {
        cin >> arr[index];   
    }
    
    for (int index = 0; index < size; index  )
    {
        if (arr[index] == 0) {
              zero_streak;
            if (zero_streak == 1 || zero_streak == 0)
            {
                arr2[index]=arr[index];
                  new_size;
            }                
        }
        else if (arr[index] != 0)
        {
            arr2[index] = arr[index];
              new_size;
            zero_streak = 0;
        }            
    }

    cout << "\nNew array looks like this:\n";
    for (int index = 0; index < new_size; index  )
        cout << arr2[index] << " ";
    
    delete[] arr;
    delete[] arr2;

    return 0;
}

All structures seem to be ok. But the problem is when elements are copied to the new array, other consecutive zeros after the first are throwing unknown values.

More over, the last element of the first array is not copied to the second.

Any suggestions how to fix this?

console_output

CodePudding user response:

You are using index for both arr and arr2, but you need a seperate index for keeping track of the new position in arr2, as the resulting string lengths won't be the same: Here is the corrected and simplified for loop:

for (int index = 0; index < size; index  )
{
    if (arr[index] == 0) {
          zero_streak;
    }
    else if (arr[index] != 0)
    {
        // Write any zero streaks
        if (zero_streak>0)
        {
            arr2[new_size] = 0;
              new_size;
            zero_streak = 0;
        }
           
        arr2[new_size] = arr[index];
          new_size;
        zero_streak = 0;
    }
}

// And catch any trailing 0 streaks here
// For example 0 0 1 [0 0]
if (zero_streak > 0)
{
    arr2[new_size] = 0;
      new_size;
    zero_streak = 0;
}

This means input of '1 0 0 0 1' becomes '1 0 1' And '0 0 1 0 0' becomes '0 1 0'

CodePudding user response:

You seem to have all the necessary ingredients to make it work. Let's look at the logic.

for (int index = 0; index < size; index  )
{
    if (arr[index] == 0) {

        // what happens when zero_streak increments to 2 and above ?
        /*             
          zero_streak;
        if (zero_streak == 1 || zero_streak == 0)  // why == 1 ?
        */

        // should read more like this: 

        if (zero_streak == 0)
        {
            zero_streak = 1;

            // why arr2[index]?  index is the index of the symbol in input array
            /*
                arr2[index] = arr[index];
                  new_size;
            */

            // You want to insert into the last position of the output 
            // array, then increase the size.
            arr2[new_size  ] = arr[index];
        }
            
    }
    else // if (arr[index] != 0)  unnecessary.
    {
        // Same problem as avove
        /*
            arr2[index] = arr[index];
              new_size;
        */
        zero_streak = 0;
        // You want to insert into the last position of the output 
        // array, the increase the size.
        arr2[new_size  ] = arr[index];
    }            
}

This could be written more succintly as:

for (int index = 0; index < size; index  )
{
    if (arr[index] == 0) {
        if (zero_streak)
            continue;      // jump back to top of loop.
        zero_streak = 1;
    }
    else {
        zero_streak = 0;
    }

    arr2[new_size  ] = arr[index];
}

You can play with the code here: https://godbolt.org/z/qqz7z89bq

  • Related