skip to Main Content

i have a problem with the question and it works on Clion compiler but on Visual Studio Code gives me random values , also it gives random memory value on the university compiler and failed the test cases below text.

An array of integers is to be read from SI. First the number N (N<=100) is entered followed by N integers. Write a program that will transform the array in the following way: starting at the beginning of the array, for each element that is equal to the next element in the array, double the value of the first one and overwrite the second one with 0. After that, rearrange the array so that all the zeroes will be moved at the end of the array, keeping the order of the other elements. Print all the elements of the array on the SO in a single line, separated by space.
Write separate functions for transforming the array and for reordering the elements.
Example:
6
2 2 0 4 8 8
(Intermediate: 4, 0, 0, 4, 16, 0)
Output:
4 4 16 0 0 0

And for this test cases it gives random memory value:

Input:

50
2 1 5 0 4 2 5 0 7 8 9 8 7 0 6 2 9 7 2 8 3 8 9 0 8 2 7 6 7 6 0 0 0 5 6 8 8 5 7 5 1 8 7 2 6 7 7 8 8 3

Output:

2 1 5 4 2 5 7 8 9 8 7 6 2 9 7 2 8 3 8 9 8 2 7 6 7 6 5 6 16 5 7 5 1 8 7 2 6 14 16 3 0 0 0 0 0 0 0 0 0 0

Input:

5
1 0 3 3 3

Output:

1 6 3 0 0
#include <iostream>
using namespace std;




int check_for_equality(int array[],int n){
    for (int i = 0;i<n-1;i++){
        if (array[i] == array[i+1]){
            array[i] = array[i] * 2;
            array[i+1] = 0;
        }
    }
    int l = n-2;
    int r = n-1;
    while (l >= 0){
        if (array[l] == 0 && array[r] != 0){
            int temp = array[l];
            array[l] = array[r];
            array[r] = temp;
            l++;
            r++;
        }
        else{
            l--;
            r--;
        }
    }
    for (int i = 0;i<n;i++){
        cout<<array[i]<<" ";
    }
}



int main(){
    int n = 0;
    cin >> n;
    int array[n];
    for (int i  =0;i<n;i++){
        cin >> array[i];
    }
    check_for_equality(array,n);

}

2

Answers


  1. Variable length arrays are not part of standard C++. If supported they are a compiler specific extension. This likely explains the difference in behavior, since you are using a variable length array in main. Use a std::vector<int> instead. The behavior of that is standardized and includes bounds checking when using at.

    Login or Signup to reply.
  2. Regarding your code, there’s several problems as mentioned in the question comments. Regarding your logic, as also mentioned in the comments, there’s a problem with your code to move all of the zeros to the right. In particular, consider

        int l = n-2;
        int r = n-1;
        while (l >= 0){
            if (array[l] == 0 && array[r] != 0){
                int temp = array[l];
                array[l] = array[r];
                array[r] = temp;
                l++;
                r++;
            }
            else{
                l--;
                r--;
            }
    

    For example, suppose that initially array[l] == 0 && array[r] != 0 is true. Then your code will execute the if part, including switching the values around, so array[r] = 0, i.e., array[n-1] = 0. Also, the line l++ will cause l = n - 1 and the line r++ which will result in r = n. However, in the next iteration of the while loop, your code will be checking that conditional again. Since array[l] == 0, the code will then try to check array[r] != 0, i.e., access past the end of the array, which is UB (Undefined Behavior)!

    Instead, you want to basically move down any top non-zero values and then have the zero replace the last non-zero value. For that purpose, I suggest keeping track of the right-most zero position in r, starting with it being n as a "virtual" zero, and then l being the left-most position being checked. Thus, that section of your code would look something like this:

        int l = n-1;
        int r = n;
        while (l >= 0){
            if (array[l] == 0){
                for (int i = l; i < r-1; i++){
                    array[i] = array[i+1];
                }
                array[--r] = 0;
            }
            l--;
       }
    

    Note that the above part of the algorithm that places all 0’s last in the array could be replaced by one call to std::stable_partition:

    std::stable_partition(array, array + n, [](int x) { return x != 0; });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search