Hello All,

I am new to C programming. I was trying to write a code that will allow me to take an array of n elements, and take every combination of pairs and write it.

example: array: 1, 2, 3, 4, 5
output: (1,2), (1,3),(1,4),(1,5),(2,3),(2,4),(2,5),(3,4),(3,5),(4,5)

I would truly appreciate the help, keep in mind the example had a set number, im trying to set it up for n amount of inputs

make a nested loop
the outer loop used to simply traverse every element of the array
and the inner loop for pairing the array index from the outer loop with every element from the array that comes after the specified index

As, I explained earlier, I'm not sure where to start, can I get someone to help start me off and i can go from there

#include <stdio.h>
#include <stdlib.h>

int main()
{
    return(EXIT_SUCCESS)
}

And zeroliken's pseudo code should be enough help to start.

Ok, I have figured it out

   int arr[5]={1,2,3,4,5};

    for (int i=0;i<5;i++)
    {
       for(int j=i+1;j<5;j++)
       { 
                  cout<<arr[i]<<","<<arr[j]<<endl;
       }
       } 

I now would like to know, how would i do it if i dont know how many elements in the array there is. Lets say I didnt know what the elements were in the array or how many elements there were, how would i enter that info. and implement it to this code

OK, but that is not C, it is C++.

You would divide the sizeof array with sizeof of data in array to get number of elements. Or you would have loop counter in your reading loop to get how many of reserved spaces are used.

There you go!!

#include<conio.h>
#include<stdio.h>
int main()
{
  int a[10],i=0,j=0;
  printf("Enter array elements: ");
  for(i=0;i<10;i++)
    {
     scanf("%d",&a[i]);
    }
  printf("\n\nPaired elements are: \n\n");
  for(i=0;i<10;i++)
    {
      for(j=i+1;j<10;j++)
        {
         printf("(%d,%d) ",a[i],a[j]);
        }
    }
  return 0;
}

Lets say I didnt know what the elements were in the array...

I don't think this would be an issue since the concern would be pairing the indexes(position of each element) of the array

...or how many elements there were

I think using a counter for the number of elements should suffice or make a dynamic array so that there won't be any unnecessary space

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.