Home > Software design >  Wrong value for the last element of the array c
Wrong value for the last element of the array c

Time:03-29

I am trying to make an array (in this case int b[]) that stores all numbers that are larger than their neighbor, int a[10] are all the elements. I'm getting everything put out correctly only the last element is some random large number do you guys have any ideas?

Concole everything is alright except the

#include <iostream>

using namespace std;

int main()
{
    int n;
    cout << "how many elements do you want ";
    cin >> n;
    int a[10];
    int b[10];

    for (int i = 0; i < n; i  )
        cin >> a[i];
    cout << endl;

    int c = 0;

    for (int i = 0; i < n; i  )
    {
        if (a[0] > a[1] && i == 0)
        {
            swap(a[0], b[0]);
            c  ;
        }
        if (a[i] > a[i   1] && a[i] > a[i - 1] && i != 0 && i != n)
        {
            swap(a[i], b[c]);
            c  ;
        }
        {
            if (a[n] > a[n - 1] && i == n - 1)
            {
                swap(a[n], b[c]);
                c  ;
            }
        }
    }
    for (int i = 0; i < c; i  )
        cout << b[i] << endl;
}

CodePudding user response:

swap(a[0],b[0]);

The b array was never initialized. Every swap() statement references the uninitialized array's contents. This results in undefined behavior.

a[i] > a[i   1]

When i reached n-1, this will be an out of bounds or uninitialized value access, which is also undefined behavior.

a[i] > a[i - 1]

When i is 0, this will attempt to access a[-1], which is also undefined behavior.

a[n] > a[n - 1]

a[n] does not appear to be ever initialized in the shown code, this is also undefined behavior.

It is unclear what this algorithm is trying to accomplish, but the entire algorithm appears to be logically flawed. No proper algorithm relies on undefined behavior.

CodePudding user response:

In your code when you get the number of inputs the array indexes will be from 0 to n-1 so the last index is n-1 but if you look at this if statement:

    if (a[n] > a[n - 1] && i == n - 1)
        {
            swap(a[n], b[c]);
            c  ;
        }

you have used a[n] as the last array index and because a[n] wasn't initialized we don't know what number is stored in it and the random number in your output has come from this index.

#include <iostream>
using namespace std;
int main()
{
    int n;
    cout << "how many elements do you want ";
    cin >> n;
    int a[10];
    int b[10];
    for (int i = 0; i < n; i  )
        cin >> a[i];
    cout << endl;
    int c = 0;
    for (int i = 0; i < n; i  )
    {
        if (a[0] > a[1] && i == 0)
        {
            swap(a[0], b[0]);
            c  ;
        }
        if (a[i] > a[i   1] && a[i] > a[i - 1] && i != 0 && i != n -1)
        {
            swap(a[i], b[c]);
            c  ;
        }
        if (a[n-1] > a[n - 2] && i == n - 1)
        {
            swap(a[n], b[c]);
            c  ;
        }
    }
    for (int i = 0; i < c; i  )
        cout << b[i] << endl;
}

the above code prints the right output.

  •  Tags:  
  • c
  • Related