C/Data Type/Array Two Dimension
Содержание
- 1 applying sizeof( ) to determine an array"s size
- 2 A simple student grades database
- 3 A simple Tic Tac Toe game
- 4 A very simple text editor
- 5 English to German Translator
- 6 Find element inside a two dimensional array
- 7 For loop: two dimensional array
- 8 Getting the values in a two-dimensional array
- 9 Multi-dimensional arrays and pointers
- 10 Store value into two dimensional char array and output it
- 11 the use of a two-dimensional array
- 12 Two-Dimensional arrays
- 13 Two dimensional char array and for loop
- 14 Two dimensional int array
applying sizeof( ) to determine an array"s size
#include <stdio.h>
#define iDAYS_OF_WEEK 7
int main( )
{
int arr[iDAYS_OF_WEEK]={1,2,3,4,5,6,7};
printf(" %d ",(int)sizeof(arr));
return(0);
}
A simple student grades database
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#define CLASSES 3
#define GRADES 2
int grade[CLASSES][GRADES];
void enter_grades(void);
int get_grade(int num);
void disp_grades(int g[][GRADES]);
int main(void)
{
char ch, str[80];
enter_grades();
disp_grades(grade);
return 0;
}
void enter_grades(void)
{
int t, i;
char s[80];
for(t=0; t<CLASSES; t++) {
printf("Class # %d:\n", t+1);
for(i=0; i<GRADES; ++i)
grade[t][i] = atoi(gets(s));
}
}
void disp_grades(int g[][GRADES])
{
int t, i;
for(t=0; t<CLASSES; ++t) {
printf("Class # %d:\n", t+1);
for(i=0; i<GRADES; ++i)
printf("Student #%d is %d\n", i+1, g[t][i]);
}
}
A simple Tic Tac Toe game
/*
C: The Complete Reference, 4th Ed. (Paperback)
by Herbert Schildt
ISBN: 0072121246
Publisher: McGraw-Hill Osborne Media; 4 edition (April 26, 2000)
*/
#include <stdio.h>
#include <stdlib.h>
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 " ";
}
A very simple text editor
#include <stdio.h>
#define MAX 100
#define LEN 80
char text[MAX][LEN];
int main(void)
{
register int t, i, j;
printf("Enter an empty line to quit.\n");
for(t = 0; t < MAX; t++) {
printf("%d: ", t);
gets(text[t]);
if(!*text[t])
break; /* quit on blank line */
}
for(i = 0; i < t; i++) {
for(j = 0; text[ i ][ j ]; j++)
putchar(text[ i ][ j ]);
putchar("\n");
}
return 0;
}
English to German Translator
#include <stdio.h>
#include <string.h>
char words[][2][40] = {
"dog", "Hund",
"no", "nein",
"year", "Jahr",
"child", "Kind",
"I", "Ich",
"",""
};
int main(void)
{
char english[80];
int i;
printf("Enter English word: ");
gets(english);
/* look up the word */
i = 0;
/* search while null string not yet encountered */
while(strcmp(words[i][0], "")) {
if(!strcmp(english, words[i][0])) {
printf("German translation: %s", words[i][1]);
break;
}
i++;
}
if(!strcmp(words[i][0], ""))
printf("Not in dictionary\n");
return 0;
}
Find element inside a two dimensional array
#include <stdio.h>
int main(void)
{
int maps[5][2] = {
1, 14,
2, 28,
3, 19,
4, 8,
5, 15
};
int key;
int i;
printf("Enter the key: ");
scanf("%d", &key);
/* look it up in the table */
for(i = 0; i < 5; i++)
if(key == maps[ i ][ 0 ]) {
printf("Key %d value pair to %d.\n",
maps[ i ][ 1 ], key);
break;
}
/* report error if not found */
if(i == 5)
printf("value not listed.\n");
return 0;
}
For loop: two dimensional array
#include <stdio.h>
int main(void)
{
int arr[4][5];
int i, j;
for(i = 0; i < 4; i++)
for(j = 0; j < 5; j++)
arr[ i ][ j ] = i * j;
for(i = 0; i < 4; i++) {
for(j = 0; j < 5; j++)
printf("%d ", arr[ i ][ j ]);
printf("\n");
}
return 0;
}
Getting the values in a two-dimensional array
#include <stdio.h>
void main()
{
int i = 0; /* Loop counter */
char t[3][3] = {
{"1","2","3"},
{"4","5","6"},
{"7","8","9"}
};
/* List all elements of the array */
for(i = 0; i < 9; i++)
printf(" t: %c\n", *(*t + i));
}
Multi-dimensional arrays and pointers
#include <stdio.h>
void main()
{
int i = 0; /* Loop counter */
char t[3][3] = {
{"1","2","3"},
{"4","5","6"},
{"7","8","9"}
};
char *pointer = *t; /* A pointer to char */
for(i = 0; i < 9; i++)
printf(" t: %c\n", *(pointer + i));
}
Store value into two dimensional char array and output it
#include <stdio.h>
int main(void)
{
char text[10][80];
int i;
for(i = 0; i < 10; i++) {
printf("some string for index %d: ", i + 1);
gets(text[ i ]);
}
do {
printf("Enter number of string (1 - 10) : ");
scanf("%d", &i);
i--; /* adjust value to match array index */
if(i >= 0 && i < 10)
printf("%s\n", text[i]);
} while(i>=0);
return 0;
}
the use of a two-dimensional array
#include <stdio.h>
#define iROWS 4
#define iCOLUMNS 5
int main( )
{
int irow;
int icolumn;
int istatus[iROWS][iCOLUMNS];
int iadd;
int imultiple;
for(irow=0; irow < iROWS; irow++)
for(icolumn=0; icolumn < iCOLUMNS; icolumn++) {
iadd = iCOLUMNS - icolumn;
imultiple = irow;
istatus[irow][icolumn] = (irow+1) *
icolumn + iadd * imultiple;
}
for(irow=0; irow<iROWS; irow++) {
printf("CURRENT ROW: %d\n",irow);
printf("RELATIVE DISTANCE FROM BASE:\n");
for(icolumn=0; icolumn<iCOLUMNS; icolumn++)
printf(" %d ",istatus[irow][icolumn]);
printf("\n\n");
}
return(0);
}
Two-Dimensional arrays
#include <stdio.h>
void main()
{
char t[3][3] = {
{"1","2","3"},
{"4","5","6"},
{"7","8","9"}
};
printf("value of t[0][0] : %c\n", t[0][0]);
printf("value of t[0] : %c\n", *t[0]);
printf("value of t : %c\n", **t);
}
Two dimensional char array and for loop
#include <stdio.h>
int main(void)
{
char text[][80] = {
"1", "2", "3",
"4", "5", "6",
"7", ""
};
int i, j;
/* now, display them */
for(i = 0; text[ i ][ 0 ]; i++) {
for(j = 0; text[ i ][ j ]; j++)
printf("%c", text[ i ][ j ]);
printf(" ");
}
return 0;
}
Two dimensional int array
#include <stdio.h>
int main(void)
{
int t, i, num[3][4];
for(t = 0; t < 3; ++t)
for(i = 0; i < 4; ++i)
num[ t ][ i ] = ( t * 4 ) + i + 1;
/* now print them out */
for(t = 0; t < 3; ++t) {
for(i = 0; i < 4; ++i)
printf("%3d ", num[ t ][ i ]);
printf("\n");
}
return 0;
}