| | |
Passing command line argument to function
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
•
•
•
•
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)
// 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).
•
•
Join Date: Oct 2007
Posts: 34
Reputation:
Solved Threads: 0
•
•
•
•
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).
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)
for (y=0; y<=ROWS*COLS; y++) { if ((rand() % 2) == 0) { movePuzzle(puzzle, 'v', rand() % COLS); } else { movePuzzle(puzzle, 'h', rand() % ROWS); } }
I'm still wandering about the code that suppose to go in movePuzzle
c++ Syntax (Toggle Plain Text)
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? >.<
•
•
Join Date: Nov 2007
Posts: 978
Reputation:
Solved Threads: 208
•
•
•
•
I'm still wandering about the code that suppose to go in movePuzzle
c++ Syntax (Toggle Plain Text)
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.
You might have ...
C++ Syntax (Toggle Plain Text)
const int COLS = 3; int array[COLS] = {1,2,3}; // todo: shift the ints in the array right 1 position, after which // the array will be {3,1,2}
•
•
Join Date: Oct 2007
Posts: 34
Reputation:
Solved Threads: 0
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
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.
those code will go to movePuzzle
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)
/*****************************************************************************\ This is a C++ program that runs a puzzle game. The puzzle consist of 2 row x 3 column grid. Grid filled with numbers 1 to 6 in random order. The main aim of this puzzle game is to get user swap those number so that they are in order (with number 1 in top left and 6 in bottom right). \*****************************************************************************/ #include "puzzle.h" void printPuzzle(int puzzle[][COLS]); void movePuzzle(int puzzle[][COLS], char dir, int rowcol); int main(int argc, char *argv[]) { unsigned int seed; int puzzle[ROWS][COLS]; if (argc != 2) { cout << "Please enter right number of argument"; return 1; } seed = atoi (argv[1]); initPuzzle(puzzle, seed); printPuzzle(puzzle); return 0; } void printPuzzle(int puzzle[][COLS]) { int i, j; cout << "\n"; cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "\n"; cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "\n"; cout << "i : " << "to print these instructions" << "\n"; cout << "q : " << "to quit" << "\n"; cout << "\n"; cout << "\n"; cout << setw(5) << "|" << setw(7) << "0" << setw(7) << "1" << setw(7) << "2" << "\n"; cout << setw(5) << setfill('-') << "+" << setw(22) << setfill('-') << "\n"; for(i = 0; i < ROWS; i++) { if (i != 1) { cout << "0" << " " << "|"; for(j = 0; j < COLS; j++) cout << " " << puzzle[i][j] ; cout << endl; } else { cout << "1" << " " << "|"; for(j = 0; j < COLS; j++) cout << " " << puzzle[i][j] ; cout << endl; } } cout << "\n"; cout << "Please enter next move : "; } void movePuzzle(int puzzle[][COLS], char dir, int rowcol) { cin >> dir, rowcol; }
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)
if (dir='v' && rowcol=0) { //some code for vertical array shifting 1 element to the right (index 0) } else if (dir='v' && rowcol=1) { //some code for vertical array shifting 1 element to the right (index 1) } else if (dir='v' && rowcol=2) { //some code for vertical array shifting 1 element to the right (index 2) } else if (dir ='h' && rowcol = 0) { //some code for horizontal array shifting 1 element to the right (index 0) } else if (dir='h' && rowcol = 1) { //some code for horizontal array shifting 1 element to the right (index 1) }
Last edited by delifion; Apr 1st, 2009 at 11:51 pm.
•
•
Join Date: Mar 2008
Posts: 13
Reputation:
Solved Threads: 0
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
this is wrong, read the specs, he says 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
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)
if (argc != 2) { cout << "Please enter right number of argument"; return 1; } seed = atoi (argv[1]); initPuzzle(puzzle, seed); printPuzzle(puzzle);
this is wrong, read the specs, he says
c++ Syntax (Toggle Plain Text)
initPuzzle()
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
![]() |
Similar Threads
- passing parameters to .exe file using C++ (C++)
- Edit sendmain aliases file. (Perl)
- array with pointers (C++)
- GUIs with c++ (C++)
- seek function (Perl)
- C++ GUI (Graphical User Interface) for beginners (C++)
- help with writing each 100 lines into different files. (C++)
- need help with a project (Shell Scripting)
- Problem in passing parameters (Visual Basic 4 / 5 / 6)
- Exit Codes (Python)
Other Threads in the C++ Forum
- Previous Thread: C++ Windows API over-riding?
- Next Thread: Array help
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





