-3

I was curious about how I could possibly iterate through an array, and keep track of every single possible ordered pair.

To create a problem to illustrate this; lets say I have a function that takes in an input array, the length of that array and a "target" which is the product of 2 values, and outputs an array consisting of the indices of the input array that you need to multiply in order to get the "target".

int* multipairs(int* inputarray, int arraysize, int target){
       /code
}

For example:

Given an array, arr = [2, 5, 1, 9, 1, 0, 10, 2], and target = 50

It should return output = [1,6].

In my mind, I would iterate through the arrays as follow; (0,1) -> (0,2) -> (0,3) -> (0,4)....

In the second pass I would do:

(1,2) -> (1,3) -> (1,4)...
.
.
.

and so on

I have the idea of what I want to do, but I am unfamiliar with C programming, and have no idea how to make a proper for loop. Please help me figure this out.

risingStark
  • 1,153
  • 10
  • 17
Johnnycode
  • 15
  • 2
  • 1
    Here a tutorial of `for` loops https://www.programiz.com/c-programming/c-for-loop – David Mar 27 '21 at 20:31
  • Since you said ordered pair, what about `(1, 0)` in the second pass. – risingStark Mar 27 '21 at 20:34
  • @risingStark - multiplication is commutative. You don't need to check (1,0) if you've already checked (0,1). – Carl Norum Mar 27 '21 at 20:36
  • @CarlNorum I know but in the question he mentioned "every single possible ordered pair". That's why I asked – risingStark Mar 27 '21 at 20:37
  • What if there are several such pairs in the input array? Do you need all of them or any one? – risingStark Mar 27 '21 at 20:38
  • *'I [...] 'have no idea how to make a proper for loop."* [Here's a list of books to help you get started.](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list). – user3386109 Mar 27 '21 at 20:49

2 Answers2

1

Your description of the algorithm is complete - as you say, the first item in the pair is iterating over all the array indices. For each of those, you want to iterate over all the pairs that follow that in the array.

for (int i = 0, i < arraysize; i++)
{
    for (int j = i + 1; j < arraysize; j++)
    {
        // operate on pair array[i] and array[j]
    }
}
Carl Norum
  • 219,201
  • 40
  • 422
  • 469
1

You can use nested for-loops to solve your problem.

int* multipairs(int* inputarray, int arraysize, int target){
    int i, j, k = -1;
    /*
    Maximum number of such pairs can be arraysize*(arraysize-1)/2
    Since, for each pair we store two indices (0-indexed),
    maximum size of output array will be arraysize*(arraysize-1)
    */
    int maxsize = arraysize*(arraysize-1); 
    int *output = (int*)malloc(sizeof(int)*maxsize);
    for (i = 0, i < arraysize; i++){
        for (j = i + 1; j < arraysize; j++){
            if(inputarray[i] * inputarray[j] == target){
                output[++k] = i;
                output[++k] = j;
            }
        }
    }
    return output;
}
risingStark
  • 1,153
  • 10
  • 17
  • Please see [Function returning address of local variable error in C](https://stackoverflow.com/questions/22288871/function-returning-address-of-local-variable-error-in-c) – Weather Vane Mar 27 '21 at 20:53