Posted on 13th October 20093 Responses
Array- Definition, Types and Example

Array
An array is a collection of variables of the same type that are referred to through a common name. A specific element in an array is accessed by an index. In C/C++, all arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Arrays may have from one to several dimensions. The most common array is the null-terminated string, which is simply an array of characters terminated by a null. Arrays and pointers are closely related; a discussion of one usually refers to the other.

Types of Array

Single-Dimension Arrays
Two-Dimensional Arrays
Multidimensional Arrays

A Tic-Tac-Toe Example

The longer example that follows illustrates many of the ways that you can manipulate arrays with C/C++. Here is a simple tic-tac-toe program. Two-dimensional arrays are commonly used to simulate board game matrices.
The computer plays a very simple game. When it is the computer’s turn, it uses get_computer_move() to scan the matrix, looking for an unoccupied cell. When it finds one, it puts an O there. If it cannot find an empty location, it reports a draw game and exits. The get_player_move() function asks you where you want to place an X. The upper-left corner is location 1,1; the lower-right corner is 3,3.
The matrix array is initialized to contain spaces. Each move made by the player or the computer changes a space into either an X or an O. This makes it easy to display the matrix on the screen.
Each time a move has been made, the program calls the check() function. This function returns a space if there is no winner yet, an X if you have won, or an O if the computer has won. It scans the rows, the columns, and then the diagonals, looking for one that contains either all X’s or all O’s.
The disp_matrix() function displays the current state of the game. Notice how initializing the matrix with spaces simplified this function.
The routines in this example all access the matrix array differently. Study them to make sure that you understand each array operation.

/* A simple Tic Tac Toe game. */
#include
#include
char matrix[3][3]; /* the tic tac toe matrix */
char check(void);
void init_matrix(void);
void get_player_move(void);
void get_computer_move(void);
void disp_matrix(void);
int main(void)
{
char done;
printf(”This is the game of Tic Tac Toe.\n”);
printf(”You will be playing against the computer.\n”);
done = ‘ ‘;
init_matrix();
do{
disp_matrix();
get_player_move();
done = check(); /* see if winner */
if(done!= ‘ ‘) break; /* winner!*/
get_computer_move();
done = check(); /* see if winner */
} while(done== ‘ ‘);
if(done==’X') printf(”You won!\n”);
else printf(”I won!!!!\n”);
disp_matrix(); /* show final positions */
return 0;
}
/* Initialize the matrix. */
void init_matrix(void)
{
int i, j;
for(i=0; i<3; i++)
for(j=0; j<3; j++) matrix[i][j] = ‘ ‘;
}
/* Get a player’s move. */
void get_player_move(void)
{
int x, y;
printf(”Enter X,Y coordinates for your move: “);
scanf(”%d%*c%d”, &x, &y);
x–; y–;
if(matrix[x][y]!= ‘ ‘){
printf(”Invalid move, try again.\n”);
get_player_move();
}
else matrix[x][y] = ‘X’;
}
/* Get a move from the computer. */
void get_computer_move(void)
{
int i, j;
for(i=0; i<3; i++){
for(j=0; j<3; j++)
if(matrix[i][j]==’ ‘) break;
if(matrix[i][j]==’ ‘) break;
}
if(i*j==9) {
printf(”draw\n”);
exit(0);
}
else
matrix[i][j] = ‘O’;
}
/* Display the matrix on the screen. */
void disp_matrix(void)
{
int t;
for(t=0; t<3; t++) {
printf(” %c | %c | %c “,matrix[t][0],
matrix[t][1], matrix [t][2]);
if(t!=2) printf(”\n—|—|—\n”);
}
printf(”\n”);
}
/* See if there is a winner. */
char check(void)
{
int i;
for(i=0; i<3; i++) /* check rows */
if(matrix[i][0]==matrix[i][1] &&
matrix[i][0]==matrix[i][2]) return matrix[i][0];
for(i=0; i<3; i++) /* check columns */
if(matrix[0][i]==matrix[1][i] &&
matrix[0][i]==matrix[2][i]) return matrix[0][i];
/* test diagonals */
if(matrix[0][0]==matrix[1][1] &&
matrix[1][1]==matrix[2][2])
return matrix[0][0];
if(matrix[0][2]==matrix[1][1] &&
matrix[1][1]==matrix[2][0])
return matrix[0][2];
return ‘ ‘;
}

Popularity: 1% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • BarraPunto
  • Bitacoras.com
  • BlinkList
  • blogmarks
  • BlogMemes Fr
  • BlogMemes Sp
  • Blogosphere News
  • blogtercimlap
  • co.mments
  • connotea
  • Current
  • Design Float
  • Diigo
  • DotNetKicks
  • DZone
  • eKudos
  • email
Comments
comment by Thamizhselvi
Posted on November 14, 2009 at 10:49 pm

Dear Sir/Madam.,
I admired by your article.It is very nice to understand…
I wanna know about array in c.Please explain with suitable simple examples.

Thanking you.
Thamizh.

comment by Anil
Posted on November 14, 2009 at 10:50 pm

ARRAY in C
An array is a collection of variables of the same type that are referred to through a common name. A specific element in an array is accessed by an index. In C/C++, all arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element. Arrays may have from one to several dimensions. The most common array is the null-terminated string, which is simply an array of characters terminated by a null. Arrays and pointers are closely related; a discussion of one usually refers to the other.
Types of Array
• Single-Dimension Arrays
• Two-Dimensional Arrays
• Multidimensional Arrays

To read in detail about Array you can visit..
http://www.authorpick.net/array-definition-types-and-example/
http://www.cplusplus.com/doc/tutorial/arrays/

We will also welcome your valuable feedback and comments.

———————–
Regards
Team Authors Choice

comment by Riya
Posted on December 26, 2009 at 3:11 am

Thats really a very nice n interesting article. It really help me lot.
Thanx sir….

Leave a Response