View Single Post
  #1 (permalink)  
Old June 6th, 2006, 09:28 PM
manih manih is offline
Authorized User
 
Join Date: Jun 2006
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Outputting Array

Hi,
I really new on C++, and I try to output an array (All possible output)
for example:
we have an array with 3 element, all possible answer would be 3! = 3 * 2 * 1 = 6

/--------------------------------------------------------/
#include <iostream>
using namespace std;

int main()
{
  int arr[3] = { 1, 2, 3 };

  for (int i=0; i<3; i++)
  {
    for (int j=0; j<3; j++)
    {
      if (i != j)
      {
    for (int k=0; k<3; k++)
    {
      if ((k != i) && (k != j))
      {
        cout << arr[i] << "\t" << arr[j] << "\t" << arr[k] << "\n";
      }
      }
    }
   }
  } // END For

  return 0;
}

/------------------------------------------------------/

On example above I use 3 FOR loop and 2 IF. My question is if we have an array with 200 element, we will have 200 FOR loop and 199 IF.
Is there a better way to use (less using FOR and IF)? if yes how?

(As everyone in this forum know being C++ programmer means we have to solve problems by ourselves, but it's been 1 month I try to figure this out but I cannot, and I hope you guys can help me for that)


Thanks
Regards

Reply With Quote