Alright can you guys tell me why the first call to printOcean prints out the memory address of the arrays rows/cols
and the second one prints like its suppossed to with 0's which the array is filled with

#include <stdio.h>
#include <string.h>


//macro
#define FLUSH_STDIN(x) {if(x[strlen(x)-1]!='\n'){do fgets(Junk,16,stdin);while(Junk[strlen(Junk)-1]!='\n');}else x[strlen(x)-1]='\0';}

//define constants
const int ROWS = 10;
const int COLS = 10;
const int NAME_SIZE = 20;

//function prototypes
void printIntro(void);
void printOcean(int ocean[ROWS][COLS],char player[]);

int main(void)
{
    //setup random seed time
    srand(time(NULL));

    //variables
    int randomNumber;
    int r,c;
    char Junk[16]; // buffer for discarding excessive user input,
                   // used by "FLUSH_STDIN" macro

    //should be long enough for a first name
    char playerOne[NAME_SIZE];
    char playerTwo[NAME_SIZE];



    // array for the "Ocean"
    int oceanOne[ROWS][COLS];
    int oceanTwo[ROWS][COLS];

    //initialize the array with 0's
    for(r = 0; r < ROWS; r++)
    {
        for(c = 0; c < COLS; c++)
        {
            oceanOne[r][c] = 0;
            oceanTwo[r][c] = 0;
        }
    }


    //get user input

    //Ask the players to enter their name.
    printf("Player 1 please enter your name: ");
    fgets(playerOne,sizeof(playerOne),stdin);
    FLUSH_STDIN(playerOne); // strip newline, flush extra chars

    printf("Player 2 please enter your name: ");
    fgets(playerTwo,sizeof(playerTwo),stdin);
    FLUSH_STDIN(playerTwo); // strip newline, flush extra chars

    //randomly select a player to start.
    randomNumber = (rand() % 10)+1;
    if (randomNumber > 5)
    {
        printf("%s will start\n", playerOne);
    }
    else if (randomNumber < 5)
    {
        printf("%s will start\n", playerTwo);
    }

    printOcean(oceanOne,playerOne);
    printf("\n\n");
    printOcean(oceanTwo,playerTwo);

    return 0;
}

void printIntro(void)
{

}
void printOcean(int ocean[ROWS][COLS],char player[])
{
    int r,c;

    printf("%s's Ocean\n\n",player);


    printf("     ");
    for(c = 0; c < COLS; c++)
    {
        printf(" ");
        printf("%d",c);
    }
    printf("\n");
    printf("    ======================");

    for(r = 0; r < ROWS; r++)
    {
        printf("\n ");
        printf("%d | ",r);
        for(c = 0; c < COLS; c++)
        {
            printf(" %d",ocean[ROWS][COLS]);
        }
        printf(" |");
    }
    printf("\n");
    printf("    ======================");
}

Recommended Answers

All 2 Replies

To answer your immediate question, printf(" %d",ocean[ROWS][COLS]); should be printf(" %d",ocean[r][c]);. The former is flat out wrong in that officially oceanOne[10][10] does not exist. In practice it does for your compiler, but only because that's the memory location for oceanTwo[0][0]. That's why the first call gives you random numbers while the second call gives you zeroes. However, if you populate the two arrays with other values, the real problem will be more apparent: both calls to printOcean display the wrong thing.

Well that solved the problem! not sure how i looked passed that one, i guess i just needed a break. Thanks alot for your help deceptikon.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.