C Getting Started section fifth multidimensional array string array

Source: Internet
Author: User


#pragma Mark----------A two-dimensional array----------
int main (int argc, const char * argv[]) {
/*
1. The essence of a two-dimensional array: An array of array elements, called Arrays of array
2. Classes such as: Managing the age of 4 students in a class, storing a one-dimensional array as an array element in another array
int age[4] = {12,13,14,15};
12,13,14,15
21,22,23,24
31,32,33,34
3. To visualize a two-dimensional array, a two-dimensional array is usually written in rows and columns.
Two-dimensional arrays are often referred to as: M*n arrays, or M row n columns
M two-dimensional array contains the number of one-dimensional arrays, which is the first dimension, which indicates the line
n the number of elements in a one-dimensional array, which is the second dimension, which represents the first column
5. A two-dimensional array is often called a matrix. Matrix: A data table that is arranged horizontally.
Defining a two-dimensional array
Type array name [number of rows] [number of columns] = {value}
1. Rows and columns use constant expressions
2. The number of rows can be omitted and the number of columns cannot be saved
3. Set the initial value, using {{},{},{}}; form
4. Each line has insufficient elements and is automatically padded

1. Defines a two-dimensional array that stores 3 classes of students of the age, each class is 5 people
int Age[3][5] = {{12,13,14,15,16},{21,22,23,24,25},{31,32,33,34,35}};
2. Defines a two-dimensional array form 2, omitting the number of rows
A[][3] = {{1,2,3}{1,2,3,4,5}};
3. Define two-dimensional array form 3, not recommended, it is not easy to see the beginning and end of each line
int a[][3] = {1,2,3,4,5};
Accessing elements of a two-dimensional array
Access to a single element must specify two subscripts, subscript 1 for the row, subscript 2 for the column;
Attention to cross-border issues
printf ("%d", a[1][0]);
printf ("%d", a[1][2]);
Modifying two-dimensional array elements
A[0][1] = 6;
printf ("%d", a[0][1]);

Traversing a two-dimensional array requires two layers of loops, the first layer of the loop controls the number of rows, and the second loop controls the first few columns.
int A[2][3] = {{1,21,3},{4,5}};
for (int i = 0; i < 2; i + +)
{
for (int j = 0; J < 3; J + +)
{
printf ("%2d\t", A[i][j]); %2d is printed out with each character occupying two cells
}
printf ("\ n");
}
*/
/*
Exercise: Talking about the row and column exchange of a two-dimensional array, storing to another array
int Arr[2][3] = {{1,2,3},{4,5}};
int brr[3][2] = {0};
for (int i = 0; i < 3; i + +)
{
for (int j = 0; J < 2; J + +)
{
printf ("%d", arr[i][j]);
BRR[I][J] = Arr[j][i];
printf ("%d", brr[i][j]);

}
printf ("\ n");
}


Exercise: Define an array of 3 rows and 4 columns, find the largest element of the array, print it, and print the row and column
int A[3][4] = {{5,45,566,58},{4,15,65,456},{21,47,5,566,}};
int c = 0;
for (int i = 0; i < 3; i + +)
{
for (int j = 0; J < 4; J + +)
{
if (c < a[i][j])
{
c = A[i][j];

}
}
}
printf ("%d \ n", c);
for (int i = 0; i < 3; i + +)
{
for (int j = 0; J < 4; J + +)
{
if (c = = A[i][j])
{
printf ("Column%d, section%d \ n", i,j);
}
}
}
printf ("\n\n\n");

*/
/*
#pragma mark---------An array of strings---------
1. Character array: array element is an array of characters, one-dimensional array
2. String array: The data element is an array of strings, a two-dimensional array. The string itself is also a group of characters
Defines an array of strings, which are somewhat different when assigning values

Char name[3][30] = {"Pengqifeng", "caijingping", "Zhangdonghui"};
Access a string, array name [first-dimensional subscript]

for (int i = 0; i < 3; i + +)
{
printf ("%s\n", Name[i]);
}

Access a character array name [first-dimensional subscript] [second-dimensional subscript]
for (int i = 0; i < 3; i + +)
{
for (int j = 0; J < J + +)
{
printf ("%c", Name[i][j]);
}
printf ("\ n");
}


Exercise: Create an array of strings (content is the name of people around you) and output the length of the longest string
Char a[4][50] = {"Asdf", "GADF", "Qwerejir", "ASDFXCV"};
int c = 0;
for (int i = 0; i < 4; i + +)
{
int d = (int) strlen (a[i]);
if (C < D)
{
c = D;
}
}

printf ("%d \ n", c);


Exercise: Create a string array (content is the name of a circle of people around you), the string (in English) from the to the big sort.
Char a[3][30] = {"Lvyao", "Gaolei", "Gaojinghong"};
for (int i = 0; i < 2; i + +)
{
for (int j = 0; J < 2-i; J + +)
{

if (strcmp (A[j], a[j + 1]) > 0)//by Accii
if (strlen (A[J) < strlen (a[j + 1]))//by string length
{
Char b[30]= {0};
strcpy (b, a[j]);
strcpy (A[j], a[j + 1]);
strcpy (A[j + 1], b);
}
}
}
for (int i = 0; i < 3; i + +)
{

printf ("%s", A[i]);


}

Enter a string from the console
Character array Store string
Char a[100] = {0};
Gets reads the string from the standard input device, ends the read with a carriage return, ends with ' \ n ', and the carriage return ' \ nthe ' is discarded without remaining in the buffer.
Can be used to enter a string with spaces
Can be read indefinitely without judging the upper limit, so using gets is unsafe and can cause overflow

printf ("Please enter a string");
Gets (a); Read
printf ("%s\n", a);
scanf (%s\n,a); end reading with a space or carriage return, a space or carriage return left in the buffer
You cannot enter a string with spaces directly
scanf ("%[^\n]", argc); %[] Input Character set
[^\n] means that except for ' \ n ' characters are received, you can receive a space, which can be used to enter a string with a space.
printf ("Please enter a string");
scanf ("%[^\n]", a);
printf ("%s\n", a);
Enter a string from the console
string Arrays Store multiple strings
Char names[6][30] = {};
printf ("Please enter string: \ n");
for (int i = 0; i < 6; i + +)
{
scanf ("%s", Names[i]); Loop input string
GetChar ();

}
printf ("You input string: \ n");
for (int i = 0; i < 6; i + +)
{
printf ("%s\n", Names[i]);
}
*/
#pragma mark------Multidimensional array------
multi-dimensional array; array subscript at more than 2
Three-dimensional arrays, three-dimensional layers, rows, columns
Define a three-dimensional array type array name [layer] [row] [column]
int a[2][3][4] = [0];
Traversing a three-dimensional array requires a three-layer loop
int Ages[2][3][4] = {{{1,2,3,4},{4,5,6,7},{7,8,9,10}},{{12,13,14,15},{15,16,17,18},{21,22,23,24}}};

for (int i = 0; i < 2; i++) {
for (int j = 0; J < 3; J + +) {
for (int k = 0; k < 4; k++) {
printf ("ages[%d][%d][%d] =%d\n", i,j,k,ages[i][j][k]);
}
}
}

Multidimensional array access element, array name [subscript] [subscript] ....
Number of multidimensional array elements = number of members product
multiple arrays of memory = number of elements multiplied by the space occupied by a single element


C Getting Started section fifth multidimensional array string array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.