How to find the position of sequentially enlarged elements in default and two-dimensional array?

时间:2017-12-18 06:06:25

标签: c++ arrays

I want to find sequentially enlarged elements in an array:

#include "stdafx.h"
#include <iostream>
#include "stdlib.h"
using namespace std;

void main()
{
    int n;
    cout << "How many elements in array? array = ";

    cin >> n;

    int *arr = new int[n];

    for (int i = 0; i < n; i++)
    {
        cout << "Element arr[" << i << "] = ";
        cin >> arr[i];
    }
    cout << "\n#### Founded sequentially enlarged elements ####\n" << endl;

    for (int i = 0; i < n; i++)
    {
        if (arr[i] + 1 == arr[i + 1])
        {
            cout << "arr[" <<i<< "] = " << arr[i] << endl;
        }
    }

    system("pause");
}

Running the code yields the following output:

How many elements in array? array = 7
Element arr[0] = 9
Element arr[1] = 13
Element arr[2] = 1
Element arr[3] = 2
Element arr[4] = 3
Element arr[5] = 4
Element arr[6] = 81

#### Founded sequentially enlarged elements ####

arr[2] = 1
arr[3] = 2
arr[4] = 3

Why does it not output all sequentially enlarged elements in the array? In the example, the last element arr[5] = 4 is missing.

How do I get this result:

arr[2] = 1
arr[3] = 2
arr[4] = 3
arr[5] = 4

Example my code in two-dimensional array:

#include "stdafx.h"
#include <iostream>
#include "stdlib.h"

using namespace std;

void main()
{
    int n, m;
    cin >> n;
    cin >> m;
    int **arr = new int*[n];

    for (int i = 0; i < n; i++)
    {
        arr[i] = new int[m];
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cout << "arr[" << i << "," << j << "] = ";
            cin >> arr[i][j];
        }
    }

    cout << "\n#### Founded sequentially enlarged elements ####\n" << endl;
    int c = 0;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; i < m; j++)
        {
            arr[i] = arr[i] + 1;
            arr[j] = arr[j] + 1;
            if (arr[i][j] == arr[i + 1][j + 1])
            {
                cout << "arr[" << i << "," << j << "] = " << arr[i][j] << endl;
                c++;
            }

            else if (c != 0)
            {
                cout << "arr[" << i << ","<<j<<"] = " << arr[i][j] << endl;
                c = 0;
            }
        }
    }

    system("pause");
}

2 个答案:

答案 0 :(得分:1)

You can use this trick.

Inside if conditional checking, the OR '||' operator skips the second condition if the first condition is true

for (int i = 0; i < n ; i++)
{

    if(i>0 && i < n-1){//avoid UB
        if ((arr[i] + 1 == arr[i + 1]) || (arr[i]-1==arr[i-1]) )// the second condition will always be skipped when the first is true. So the second condition will be checked only for the last sequential element
        {
            cout << "arr[" <<i<< "] = " << arr[i] << endl;
        }
    }else if(i==0){
        if (arr[i] + 1 == arr[i + 1])
            {
                cout << "arr[" <<i<< "] = " << arr[i] << endl;
            }

    }else{
         if (arr[i]-1==arr[i-1] )// when i = n-1
        {
            cout << "arr[" <<i<< "] = " << arr[i] << endl;
        }

    }
}

Example:

Let the array be: {9,13,1,2,3,4,81}

When the cursor reaches the element 4 i.e. i=5 it will first check for the condition with the element 81. But since it will be false and there is '||' OR in the conditional checking, the second condition i.e. 'arr[i]-1==arr[i-1]' will be checked, i.e. between the element 4 and 3.

The code could be cleaned and optimized to a certain level but the idea is the catch here.

答案 1 :(得分:0)

Your code has logical error .You are comparing the both arr[i]==a[i+1],if those ar equal you are printing the arr[i] which is present element.you are refused print the second element.thats why whenever arr[5] == arr[4] but not equal to arr[6] so thats why it is not displayed.

Here is the following code which generates the results which you expected.

look at the for loop.add following things to your for loop.

int c=0;//checking if sequence is found
for (int i = 0; i < n-1; i++)
{
    if (arr[i] + 1 == arr[i + 1])
    {
        cout << "arr[" <<i<< "] = " << arr[i] << endl;
        c++;//increasing the counter if sequence is found
    }
    else if(c!=0)
    {
        cout << "arr[" <<i<< "] = " << arr[i] << endl;//printing the seccond element to if further comparison returns false.
        c=0;//reset counter which tells that sequence is over.
    }

}