Passing command line argument to function

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

Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Passing command line argument to function

 
0
  #21
Mar 28th, 2009
Originally Posted by delifion View Post
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).
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 34
Reputation: delifion is an unknown quantity at this point 
Solved Threads: 0
delifion delifion is offline Offline
Light Poster

Re: Passing command line argument to function

 
0
  #22
Mar 28th, 2009
Originally Posted by mitrmkar View Post
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.

  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
  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? >.<
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Passing command line argument to function

 
0
  #23
Mar 28th, 2009
Originally Posted by delifion View Post
I'm still wandering about the code that suppose to go in movePuzzle
  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 ...
  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}
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 34
Reputation: delifion is an unknown quantity at this point 
Solved Threads: 0
delifion delifion is offline Offline
Light Poster

Re: Passing command line argument to function

 
0
  #24
Apr 1st, 2009
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

  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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 13
Reputation: Gerlan is an unknown quantity at this point 
Solved Threads: 0
Gerlan Gerlan is offline Offline
Newbie Poster

Re: Passing command line argument to function

 
0
  #25
Apr 2nd, 2009
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


  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
  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
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC