loop 'j' in subroutine starterequive

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 51
Reputation: shamila08 is an unknown quantity at this point 
Solved Threads: 0
shamila08 shamila08 is offline Offline
Junior Poster in Training

loop 'j' in subroutine starterequive

 
0
  #1
Dec 3rd, 2008
hello, dear all
  1. #include <iostream>
  2. #include <stdio.h>
  3. using namespace std;
  4. int count = 1;
  5.  
  6. void print( int *arr, int SIZE){
  7. if (arr != 0) {
  8. for (int i = 0; i < SIZE; i++) { // i = position
  9. cout << arr[i];
  10. }
  11. cout <<"\n";
  12. }
  13. }
  14. void circular_left(int *arr, int start, int SIZE)
  15. {
  16. int tmp = arr[start]; //i is a position
  17. for(int i = start; i<= SIZE-1; i++){
  18. arr[i]= arr[i+1];
  19. }
  20. arr[SIZE-1] = tmp;
  21. }
  22. void starterequiv(int *arr, int start, int SIZE)
  23. { int i, j;
  24. print(arr, SIZE);
  25. if (start < SIZE)
  26. {
  27. for (i = SIZE-1; i > start; i--)
  28. {// outer loop
  29. for (j = i+1 ; j < SIZE; j++)
  30. { // inner loop
  31. circular_left(arr,i, SIZE);
  32. starterequiv(arr, i, SIZE);
  33. count++;
  34. } //end inner loop
  35. circular_left(arr,i,SIZE);
  36. } // end outer loop
  37. }
  38.  
  39. }
  40.  
  41. void initiate(int *arr, int SIZE)
  42. {
  43. for (int i = 0; i <SIZE; i++) { // i is a position
  44. arr[i] = i+1;
  45. }
  46. } // init
  47.  
  48.  
  49. int main()
  50. {
  51. int SIZE;
  52. cout << "Enter the number of elements: "; //SIZE = n
  53. cin >> SIZE;
  54.  
  55. if (SIZE > 0 && SIZE <= 100) {
  56. int *arr = new int[SIZE];
  57. initiate(arr, SIZE);
  58. starterequiv(arr,0,SIZE);
  59. delete [] arr;
  60. cout << "No. Permutation :" << count;
  61. cout << endl;
  62. }
  63. }
and then the output as follows:
  1. Enter the number of elements: 4
  2. 1234
  3. 1243
  4. 1342
  5. 1324
  6. 1423
  7. 1432
  8. No. Permutation :6
  9. Press any key to continue . . .
Actually i need that as my output which at position 1, the element '1' is fixed as a head.
then i just wondering about this routine
  1. void starterequiv(int *arr, int start, int SIZE)
  2. { int i, j;
  3. print(arr, SIZE);
  4. if (start < SIZE)
  5. {
  6. for (i = SIZE-1; i > start; i--)
  7. {// outer loop
  8. for (j = i+1 ; j < SIZE; j++)
  9. { // inner loop
  10. circular_left(arr,i, SIZE);
  11. starterequiv(arr, i, SIZE);
  12. count++;
  13. } //end inner loop
  14. circular_left(arr,i,SIZE);
  15. } // end outer loop
  16. }
  17. }
with the loop 'j'
  1. for (j = i+1 ; j < SIZE; j++)
i'm try to throw it but the output is weird. if i left it, i couldn't find where i use 'j' in my algorithm. please help me. i just cycle left all those element exclude '1' to generate next (n-1)! array. it something similar to permutation but unfortunately it is not.
Last edited by shamila08; Dec 3rd, 2008 at 9:04 pm. Reason: error typing
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC