ABXZone Computer  Forums

Thread Tools Display Modes
Old 04-04-2009, 09:38 AM   #1
Registered User
Tbolt's Avatar
Join Date: Jul 2006
Posts: 5
Nested looping in C - Im stuck

So I thought I had a pretty good understanding of looping even though I missed class the day it was taught. However now I am in a conceptual mind**** of how to approach this.

The assignment is to create a dynamic tic-tac-toe board using multidimensional arrays and functions (with array passing)

here is what the output should look like:

here is what I have so far, focusing on the display board part
#include <stdio.h>

#ifndef __TRUE_FALSE__
#define __TRUE_FALSE__
#define TRUE 1
#define FALSE 0

// ROWS and COLS
#define ROWS 7
#define COLS 7

#define MARKONE 'X'
#define MARKTWO 'O'
#define BLANK   ' '

void InitializeBoard(char[ROWS][COLS]);
void DisplayBoard(char[ROWS][COLS]);
int PlayerMove(int, int, char[ROWS][COLS], char);

int main() {
	// declare variables
	char board[ROWS][COLS];

	// initialize board
	// populate the board with moves
	PlayerMove(1, 1, board, MARKONE);
	PlayerMove(1, 2, board, MARKONE);
	PlayerMove(4, 3, board, MARKONE);
	PlayerMove(1, 1, board, MARKTWO);
	PlayerMove(6, 2, board, MARKTWO);
	PlayerMove(4, 12, board, MARKTWO);
	// display the board
	// exit program
	return 0;

void InitializeBoard(char board[ROWS][COLS]) {
	int i = 0, j = 0;
	for(i = 0; i < ROWS; i++) {
		for(j = 0; j < COLS; j++) {
			board[i][j] = BLANK;

void DisplayBoard(char board[ROWS][COLS]) {
	int i = 0, j = 0, k = 0;

	for(i = 1; i <= COLS; i++) {
		printf("%2d", i);


int PlayerMove(int row, int col, char board[ROWS][COLS], char symbol) {
I feel like I am trying to approach this from the wrong angle, and because of that I can't comprehend how to do it. I imagine I will need to nest a loop within a loop within another loop.

Any advice would be great, not necessarily with this particular problem but anything with looping that would help me understand it better would be great.
(Offline)   Reply With Quote

Advertisement [Remove Advertisement]
Old 04-04-2009, 03:41 PM   #2
Registered User
XJ.'s Avatar
Join Date: Apr 2002
Location: USA-NH
Posts: 8,430
Re: Nested looping in C - Im stuck

For your DisplayBoard() function, just do it the same way you did the InitializeBoard() function. Just replace 'board[i][j] = BLANK' with the appropriate print function.

Good luck!
Asus P6T V2 Deluxe Ci7 970 @ 4.0GHz w/HT, Corsair H100i, 2x240GB SanDisk Extreme RAID0, 1x WD VR 150GB,
MSI GTX 680 PE @ 1110MHz, 12GB G.Skill Ripjaws DDR3 1600, Corair 850HX, Corsair 800D case. Win10 Pro x64. Dell U2412M.

(Offline)   Reply With Quote
Old 04-04-2009, 05:48 PM   #3
Registered User
Tbolt's Avatar
Join Date: Jul 2006
Posts: 5
Re: Nested looping in C - Im stuck

Alright, I am starting to get it. I just had to go back to the basics and play around a bit before tackling this.

Thanks though =p
(Offline)   Reply With Quote
Old 04-04-2009, 08:03 PM   #4
&lt;unknown level&gt;
zapionics<alt>'s Avatar
Join Date: Dec 2002
Posts: 6,262
Re: Nested looping in C - Im stuck

just on this "developers block" syndrome you seemed to be experiencing... I'm a big fan of design... ie: conceptualising the solution via a process of design which then gives you a skeletal structure in which to hang your code.

I tend to use "structure" charts for this... and use cases of course... and activity diagrams or even sequence diagrams...

I know this may be a trivial example for some ppl... coding top down has advantages (eg: speed) if you are very familiar and experienced with the solution domain... but otherwise... design!
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Please note: any advertising within this post has been placed there by the site owner and NOT by me!
Why not avoid the ads by joining the 'Zone today? The site is mostly ad free for members!

"You Cannot Hide..."
(Offline)   Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Powered by vBulletin® Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
vBulletin Skin developed by: vBStyles.com

© 2006 - 2016 ABXZone Forums | About ABX Zone Forums | Advertisers | Investors | Legal | A member of the Crowdgather Forum Community