Hello , I want to compare each element of an array with every other.
For example, array[0] with array[1] , with array[2] ...
Then , array[1] with array[2],with array[3] ...

So , I thought something like this :

for ( int i = 0; i < N; i ++ )
{
    for ( int k = i + 1; k < N; k++ )
    {
        temp[ i * N + k ] = (array[ i ] == array[ k ]);
      }
 }

But , for example , for i = 0 , temp indexing will be : temp[ 0 + 1] ,hence temp[ 1] .
There will not be temp[ 0 ] as first index!

I can't think how to solve this and also what the size of temp will be.If I leave it as N * N ,it will cover all cases but it is more than it is needed.

Thanks!

Hmm, I figured something!

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>

using namespace std;

int main( int argc, char** argv )
{
    int N = 16;

    int numCompares = (N / 2) * (N -1);

    //vector<int> test;
    //test.resize(numCompares*N);

    int test[ numCompares*N ];
    int array[ N ];

    array[0] = 0;
    array[1] = 1;
    array[2] = 1;
    array[3] = 6;
    array[4] = 8;
    array[5] = 4;
    array[6] = 0;
    array[7] = 11;
    array[8] = 4;
    array[9] = 23;
    array[10] = 3;
    array[11] = 15;
    array[12] = 2;
    array[13] = 12;
    array[14] = 19;
    array[15] = 12;

    int count = 0;
    //compare 
    for ( int p = 0; p < numCompares; p++ )
    {
        for ( int idx = 0; idx < N; idx++ )
        {
            for ( int k = idx + 1; k < N; k ++ )
            {
                if ( array[ idx ] == array[ k ] ) 
                {  
                       //test.push_back( 1 );
                      //test[ p ] = 1;
                       test[ p * N + idx * N + k ] = 1;
                  }
                  else
                  {

                        test[ p * N + idx * N + k ] = 0;
                        //test[ p ] = 0;
                        //test.push_back(0);
                  }

            }
        }

    }




    return 0;
}

I figured that the number of comparisons would be the sum of an arithmetic progression.
As for the size of test , since every comparison ( array[0] with array[1]) will result in a N dimension array, so N * numCompares will be the size of test.

Now , I am not sure how to handle the filling ( and index ) and size of test data.
That's why I have different version in the if statement.I also tried to use vector for test.

Any ideas?

Thanks!

One of the simplest ways to get the index of test is to use a counter:

    int index = 0;
    for ( int idx = 0; idx < N; idx++ )
    {
        for ( int k = idx + 1; k < N; k ++ )
        {
            if ( array[ idx ] == array[ k ] ) 
            {  
                   test[ index++ ] = 1;
              }
              else
              {
                    test[ index++ ] = 0;
              }
        }

Hello and thanks for helping.

Yes, I agree with the index.As you can see I already have defines numCompares which is 120 ( and using your code index = 120 ).

But,I think my problem is logical?
I think test should hold 120 * N elements because N is the size of the array and so 120*N for all the calculations.( and every comparison gives a N size array filled with 0 or 1 )

I can't find how to index test according to my above statement.

I'm not sure I understand what you're getting at. With the above code, test will contain the result of every comparison for that array. What else do you need?

If I compare array[0] with array[1] , then with array[2] , array[3] ...so on , the result is a N dimension array ( flled with 0 or 1 ).

Then , I am comparing array[1] with array[2] , array[3] ..so on , the result again is a N dimension array.

So , I will have 120 such comparisons , so 120 * N will be the elements of test.

So,I can't figure how to store test.
I hope this is clear.

Thanks!

I think you may be confused. Comparing each element of the array with each other element of the array is 120 comparisons. If test is set to a size of 120 then test is storing all the comparisons you need. Why do you need more storage? Why do you feel that test will be 120 * N when there are only 120 elements in total?

Yes , you are right!I was confused.
But then , the problem how to index(fill) test array still remains..
If you look to my code , let's correct the
int test[ numCompares*N ]; to int test[ numCompares ];

Inside the loop ,how can I handle test?
Right now it is test[ p * N + idx * N + k ] = 1; which is wrong.
using test[ p ] = 1; is also wrong since it will write all comparisons at the same p.

How can I overcome this?
Thank you.

The code I showed you before will do that:

        int index = 0;
        for ( int idx = 0; idx < N; idx++ )
        {
            for ( int k = idx + 1; k < N; k ++ )
            {
                if ( array[ idx ] == array[ k ] ) 
                {  
                       test[ index++ ] = 1;
                }
                else
                {
                    test[ index++ ] = 0;
                }
            }
        }

As long as test has sufficient size it will contain all the comparisons.

Edited 11 Months Ago by tinstaafl

Yes, you are right!I can see that.

But , if I want to follow an indexing like I am trying?Is there a way?

Thank you.

The problem with calculating the index is there is an offset that gets progressively bigger and relies on the previous offset. One way around this is to use a helper function that recaclulates the offset:

int FactorialOffset(int value)
{
    int outval = 0;
    for (int i = 1; i <= value + 1; i++)
    {
        outval += i;
    }
    return outval;
}

Using this function with this formula ((N*i) + k) - FactorialOffset(i), you can calculate the index for each comparison. In your code it would look like this:

    for ( int idx = 0; idx < N; idx++ )
    {
        //Calculate the offset here, since it won't change until i does
        int fo = FactorialOffset(i);
        for ( int k = idx + 1; k < N; k ++ )
        {
            if ( array[ idx ] == array[ k ] ) 
            {  
                   test[ ((N*i) + k) - fo ] = 1;
              }
              else
              {
                    test[ ((N*i) + k) - fo ] = 0;
              }
        }
    }

Edited 11 Months Ago by tinstaafl

Excellent!I could never thought of this..

Now, I have one more thing to solve.
Right now, the test array holds indices from 0-120 ( numCompares = 120 ).

We are comparing element 0 with element 1 , then with element 2..element 15
Comparison of array[0] with array[1]will be written to test[0] , comparison array[0] with array[2]will be writte to test[1]and so on.

I want a new vector to hold N ( N =16 right now ) labels (indices) :

testVector[ 0 ]will contain all the comparison of array[0] with every other element ( until N )

0->1
0->2
0->3
0->4
...
0->15   ( we said until N =16 )

Count sum = 15 times ( N-1)
So testVector[ 0 ] will hold 15 values

testVector[ 1 ]will contain all the comparison of array[1] with every other element

1->2
1->3
1->4
..
1->15    

Count sum = 14 times
SotestVector[ 1 ] will hold 14 values

and so on.

So , code will be like this:

vector< vector<int> > testVector( numCompares );
int jCount = N - 1; // count sum for every 'label' 

for ( int i = 0; i < N; i++ ) // I want vector to have N labels
{
    while ( jCount >= 1 )
    {
        for ( int j = 0; j < numCompares; j++ )
        {                   
            testVector[ i ].push_back( test[ j ] );
            continue; // must jump to next i
        }
        jCount--;
    }
}

The code is not right.
I can't figure a way to manipulate the 'j' index related to i and jCount.
Remember, that, for testVector[ 0], jCount = 15 and we must read the test array from j indices (0-14).
Then, we must jump to testVector[1], jCount =14 and we must read test array from j = 15-28 and so on.

I hope it is clear!

Edited 11 Months Ago by glao

Basically you use the same nested loop as you've already used. Before the start of the k loop, instantiate a temporary vector to hold the results of that set of comparisons. Add the results of each comparison to that vector inside the k loop. After the k loop is finished add that vector to the 2D vector that will hold all the results:

vector< vector<int> > testVector( numCompares );
    for ( int idx = 0; idx < N; idx++ )
    {
        vector<int> temp;
        //Calculate the offset here, since it won't change until i does
        int fo = FactorialOffset(i);
        for ( int k = idx + 1; k < N; k ++ )
        {

            if ( array[ idx ] == array[ k ] ) 
            { 
                    temp.push_back(1);
                   test[ ((N*i) + k) - fo ] = 1;
              }
              else
              {
                    temp.push_back(0);
                    test[ ((N*i) + k) - fo ] = 0;
              }
        }
        testVector.push_back(temp);
    }

Hmm. Nice!

I am little confused how to access the testVector now..
How can I print the contents?

Because if I try something like this:

 for (int i = 0; i< testVector.size(); i++) 
 {
        for (int j = 0; j< 120; j++)
        {
            cout << testVector[ i ][ j ] << " ";
        }
        cout << endl;
    }

gives seg fault.
My vector now is 3D vector?

Also, what about the way I tried to solve this problem? (my previous post) Is there a solution according that?

Thank you very much!

Ok, I figured!

    for (int i = 0; i< testVector.size(); i++) {
        for (int j = 0; j< testVector[i].size(); j++) {
            cout << testVector[i][j] << " ";
        }
        cout << endl;
    }

And it works fine!
So , it is still a 2D vector.
As far as I understand , testVector is just declared as a 2D vector ( we don't fill it as a 2D vector yet) , and then we fill it with a 1D vector.So , we have a vector (testVector) of vectors(temp).

Also , we don't need the test array anymore like that.

If you can answer me about my last comment ( if it is possible to solve it using my procedure)

Edited 11 Months Ago by glao

You will have to be more specific. You have code to:

  • use a counter as the index to fill an array
  • calculate the index and use that when filling the array
  • fill a 2D vector

What other ways of storing/accessing this data do you need?

Ok, your solution is very clear.
Maybe my thought is too complicated.
I was just wondering if there was a solution based on the flow I provided.

Anyway, thank you very much for your help.

This question has already been answered. Start a new discussion instead.