943,694 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2008
  • C++ RSS
You are currently viewing page 3 of this multi-page discussion thread; Jump to the first page
Mar 28th, 2009
0

Re: Passing command line argument to function

Click to Expand / Collapse  Quote originally posted by delifion ...
so it should scramble the puzzle around right?

movePuzzle is the part where user can swap the order around (but the scrambling should be done in initPuzzle)
Hmm, but looking closely at initPuzzle(), it clearly achieves the scrambling by calling movePuzzle(...) on two occasions.
   // scramble the puzzle
   for (y=0; y<=ROWS*COLS; y++) {
      if ((rand() % 2) == 0) {
         movePuzzle(puzzle, 'v', rand() % COLS); 
      }
      else {
         movePuzzle(puzzle, 'h', rand() % ROWS); 
      }
   }

So, this means that you have to write the code for movePuzzle(...) in order to have the puzzle scrambled. Once you have movePuzzle() coded, you can call it from the main() too (passing in the user's input).
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 28th, 2009
0

Re: Passing command line argument to function

Click to Expand / Collapse  Quote originally posted by mitrmkar ...
Hmm, but looking closely at initPuzzle(), it clearly achieves the scrambling by calling movePuzzle(...) on two occasions.
   // scramble the puzzle
   for (y=0; y<=ROWS*COLS; y++) {
      if ((rand() % 2) == 0) {
         movePuzzle(puzzle, 'v', rand() % COLS); 
      }
      else {
         movePuzzle(puzzle, 'h', rand() % ROWS); 
      }
   }

So, this means that you have to write the code for movePuzzle(...) in order to have the puzzle scrambled. Once you have movePuzzle() coded, you can call it from the main() too (passing in the user's input).
umm... thats right... lol sorry I didn't look close enough...
I'm frustrated with the output format, but I got it now.

So the next part is complete the movePuzzle function.

The project specification says:
movePuzzle expects to be given the puzzle array, a char variable of 'v' or 'h', representing whether a vertical column or horizontal row is to be moved, and another variable indicating which column or row is to be moved.

c++ Syntax (Toggle Plain Text)
  1. for (y=0; y<=ROWS*COLS; y++) {
  2. if ((rand() % 2) == 0) {
  3. movePuzzle(puzzle, 'v', rand() % COLS);
  4. }
  5. else {
  6. movePuzzle(puzzle, 'h', rand() % ROWS);
  7. }
  8. }

I'm still wandering about the code that suppose to go in movePuzzle
c++ Syntax (Toggle Plain Text)
  1. void movePuzzle(int puzzle[][COLS], char dir, int rowcol)

it takes 3 arguments right? the array puzzle, the char 'v' or 'h', and the third argument is the number of index correspond to the vertical/horizontal.

Do I use If function to swap the array around? >.<
Reputation Points: 10
Solved Threads: 0
Light Poster
delifion is offline Offline
34 posts
since Oct 2007
Mar 28th, 2009
0

Re: Passing command line argument to function

Click to Expand / Collapse  Quote originally posted by delifion ...
I'm still wandering about the code that suppose to go in movePuzzle
c++ Syntax (Toggle Plain Text)
  1. void movePuzzle(int puzzle[][COLS], char dir, int rowcol)

it takes 3 arguments right? the array puzzle, the char 'v' or 'h', and the third argument is the number of index correspond to the vertical/horizontal.
Yes. A suggestion, maybe first exercise with a smaller problem, e.g. try to simulate the "(0 <= n <= 1) to move row n right 1 position".
You might have ...
C++ Syntax (Toggle Plain Text)
  1. const int COLS = 3;
  2. int array[COLS] = {1,2,3};
  3.  
  4. // todo: shift the ints in the array right 1 position, after which
  5. // the array will be {3,1,2}
Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Apr 1st, 2009
0

Re: Passing command line argument to function

Hi, I'm back >.<

Been busy with other assignment.

Hmm, Yea I realize that I should use array shifting.
But I still have problem in the input

c++ Syntax (Toggle Plain Text)
  1. /*****************************************************************************\
  2. This is a C++ program that runs a puzzle game.
  3. The puzzle consist of 2 row x 3 column grid.
  4. Grid filled with numbers 1 to 6 in random order.
  5.  
  6. The main aim of this puzzle game is to get user swap those number so that
  7. they are in order (with number 1 in top left and 6 in bottom right).
  8. \*****************************************************************************/
  9.  
  10. #include "puzzle.h"
  11.  
  12. void printPuzzle(int puzzle[][COLS]);
  13. void movePuzzle(int puzzle[][COLS], char dir, int rowcol);
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17. unsigned int seed;
  18. int puzzle[ROWS][COLS];
  19.  
  20.  
  21. if (argc != 2)
  22. {
  23. cout << "Please enter right number of argument";
  24. return 1;
  25. }
  26.  
  27. seed = atoi (argv[1]);
  28.  
  29. initPuzzle(puzzle, seed);
  30.  
  31. printPuzzle(puzzle);
  32.  
  33. return 0;
  34. }
  35.  
  36. void printPuzzle(int puzzle[][COLS])
  37. {
  38. int i, j;
  39.  
  40.  
  41. cout << "\n";
  42. cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n";
  43. cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n";
  44. cout << "i : " << "to print these instructions" << "\n";
  45. cout << "q : " << "to quit" << "\n";
  46. cout << "\n";
  47. cout << "\n";
  48.  
  49. cout << setw(5) << "|" << setw(7) << "0" << setw(7) << "1" << setw(7) << "2" << "\n";
  50. cout << setw(5) << setfill('-') << "+" << setw(22) << setfill('-') << "\n";
  51.  
  52. for(i = 0; i < ROWS; i++)
  53. {
  54. if (i != 1)
  55. {
  56. cout << "0" << " " << "|";
  57.  
  58.  
  59. for(j = 0; j < COLS; j++)
  60.  
  61. cout << " " << puzzle[i][j] ;
  62. cout << endl;
  63. }
  64. else
  65. {
  66. cout << "1" << " " << "|";
  67.  
  68.  
  69. for(j = 0; j < COLS; j++)
  70.  
  71. cout << " " << puzzle[i][j] ;
  72. cout << endl;
  73. }
  74. }
  75. cout << "\n";
  76. cout << "Please enter next move : ";
  77.  
  78.  
  79. }
  80.  
  81. void movePuzzle(int puzzle[][COLS], char dir, int rowcol)
  82. {
  83. cin >> dir, rowcol;
  84.  
  85. }

Since the program requires user to input the direction followed by the number of index to be shifted, where should I put those input? in the movePuzzle? printPuzzle? or main?

At the moment i put it in movePuzzle.

The next difficulties for me will be connecting the variable dir and rowcol between function, because I want to make a if conditional statement.

e.g.
c++ Syntax (Toggle Plain Text)
  1.  
  2. if (dir='v' && rowcol=0)
  3. {
  4. //some code for vertical array shifting 1 element to the right (index 0)
  5. }
  6. else if (dir='v' && rowcol=1)
  7. {
  8. //some code for vertical array shifting 1 element to the right (index 1)
  9. }
  10. else if (dir='v' && rowcol=2)
  11. {
  12. //some code for vertical array shifting 1 element to the right (index 2)
  13. }
  14. else if (dir ='h' && rowcol = 0)
  15. {
  16. //some code for horizontal array shifting 1 element to the right (index 0)
  17. }
  18. else if (dir='h' && rowcol = 1)
  19. {
  20. //some code for horizontal array shifting 1 element to the right (index 1)
  21. }
those code will go to movePuzzle
Last edited by delifion; Apr 1st, 2009 at 11:51 pm.
Reputation Points: 10
Solved Threads: 0
Light Poster
delifion is offline Offline
34 posts
since Oct 2007
Apr 2nd, 2009
0

Re: Passing command line argument to function

you are assigning dir = 'h', instead you should be checking if dir is equal to a specific char, same goes for rowcol.

What you want is to assign location of say [0][0] to [1][1], you cannot do simply by [0][0] = [1][1] as doing so will overwrite the values. Try using a temporary value and then assign temporary value i to [0][0], that way you will be able to assign [0][0] to [1][1] and then assign [1][1] to your temporary value


c++ Syntax (Toggle Plain Text)
  1.  
  2. if (argc != 2)
  3.  
  4. {
  5.  
  6. cout << "Please enter right number of argument";
  7.  
  8. return 1;
  9.  
  10. }
  11.  
  12.  
  13.  
  14. seed = atoi (argv[1]);
  15.  
  16.  
  17.  
  18. initPuzzle(puzzle, seed);
  19.  
  20.  
  21.  
  22. printPuzzle(puzzle);

this is wrong, read the specs, he says
c++ Syntax (Toggle Plain Text)
  1. initPuzzle()
comes RIGHT after you declare puzzle array, he might deduct marks off you if he sees the code above.

Also I'm pretty sure our lecturer checks various forums (maybe even this one) so it wouldn't be very wise to post your entire code for him and/or other students to see
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Gerlan is offline Offline
13 posts
since Mar 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ Windows API over-riding?
Next Thread in C++ Forum Timeline: Array help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC