Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Passing command line argument to function

delifiondelifion Posts: 3Member
Hi, I've got a puzzle game program consist of 2D array 2 rows x 3 cols.
The professor wants no changes to be made in puzzle.h, which is:

[code]
#ifndef _PUZZLE_H
#define _PUZZLE_H

#include
#include
#include

using namespace std;

// Constants
const int COLS = 3;
const int ROWS = 2;

// Function prototypes
// Your cpp file must have the code for movePuzzle
void movePuzzle(int puzzle[][COLS], char dir, int rowcol);
void initPuzzle(int puzzle[][COLS], unsigned int seed);

void initPuzzle(int puzzle[][COLS], unsigned int seed)
{
/* Initialise the puzzle and scramble it around */

int x, y;

// initialise random number generator using the seed value
srand(seed);

// fill the puzzle
for (y=0; y<ROWS; y++) {
for (x=0; x<COLS; x++) {
puzzle[y][x] = 1 + y * COLS + x;
}
}

// 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);
}
}
}http://www.programmersheaven.com/mb/CandCPP/PostNew.aspx?S=B20000
Programmer's Heaven - Post New Message

#endif
[/code]


This is my puzzle.cpp which includes puzzle.h

[code]
/*****************************************************************************
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"

int main(int argc, char *argv[])
{
int temp;
int puzzle[ROWS][COLS];

[b]initPuzzle(); ????[/b]

cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "
";
cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "
";
cout << "i : " << "to print these instructions" << "
";
cout << "q : " << "to quit" << "
";

system("PAUSE");
return 0;
}

void movePuzzle(int puzzle[][COLS], char dir, int rowcol)
{
}
[/code]

My problem is to call initPuzzle function before any other code.
I need to pass array and seed using command line argument.
I'm new to C++, can someone explain how to do that? (the line with ????)

initPuzzle function suppose to set up the puzzle and randomise it.
The function itself is in the puzzle.h and I can only call it in the puzzle.cpp (no changes can be made to puzzle.h).

If my understanding is right, I need to populate the array from the command line.

Any help would be appreciated.



Comments

  • Malcolm_McLeanMalcolm_McLean Posts: 252Member ✭✭

    User calls your program with an argument, eg

    puzzle 1234

    you extract the seed like this

    unsigned int seed;

    seed = atoi(argv[1]);

    and simply pass it to initPuzzle().

    For extra marks use strtol() instead of atoi and do error checking.
  • Malcolm_McLeanMalcolm_McLean Posts: 252Member ✭✭

    User calls your program with an argument, eg

    puzzle 1234

    you extract the seed like this

    unsigned int seed;

    seed = atoi(argv[1]);

    and simply pass it to initPuzzle().

    For extra marks use strtol() instead of atoi and do error checking.
  • Malcolm_McLeanMalcolm_McLean Posts: 252Member ✭✭

    User calls your program with an argument, eg

    puzzle 1234

    you extract the seed like this

    unsigned int seed;

    seed = atoi(argv[1]);

    and simply pass it to initPuzzle().

    For extra marks use strtol() instead of atoi and do error checking.
  • delifiondelifion Posts: 3Member
    Hi,

    Thanks for the help.

    I did something like this:

    [code]
    /*****************************************************************************
    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]);

    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);

    cout << "vn : " << "(0 <= n <= 2) to move column n down 1 position" << "
    ";
    cout << "hn : " << "(0 <= n <= 1) to move row n right 1 position" << "
    ";
    cout << "i : " << "to print these instructions" << "
    ";
    cout << "q : " << "to quit" << "
    ";

    printPuzzle(puzzle);

    system("PAUSE");
    return 0;
    }

    void printPuzzle(int puzzle[][COLS])
    {
    int i,j;

    for(i = 0; i < ROWS; i++)
    {
    for(j = 0; j < COLS; j++)
    cout << " " << puzzle[i][j];
    cout << endl;
    }
    }
    void movePuzzle(int puzzle[][COLS], char dir, int rowcol)
    {
    }
    [/code]

    but when i tried to print the array, it's printing something like
    123
    456

    it suppose to scramble the order of number, but it doesn't.

    any idea what's wrong? do I need pointer in here?
  • Malcolm_McLeanMalcolm_McLean Posts: 252Member ✭✭
    In the code you've posted movePuzzle() is an empty stub function. So of course it doesn't make any changes to the puzzle.
Sign In or Register to comment.