Two-dimensional array, multidimensional array, string array;

Source: Internet
Author: User
Tags string format strlen


Two-dimensional array, multidimensional array, string array;

Two-dimensional arrays:

The concept of a two-dimensional array: An array of two subscripts following an array name is called a two-dimensional array

Definition: Type descriptor array name [constant expression 1][constant expression 2]={value 1, value 2, ...}

Where the constant expression 1 represents the one-dimensional length, the constant expression 2 represents the two-dimensional length

When a two-dimensional array is defined, an unassigned element is automatically initialized to 0 if the element is not listed.

3) Two-dimensional array

int a[3][3]={{1},{2},{3}};

for (int i=0; i<3; i++) {

for (int j=0; j<3; j) {

printf ("a[%d][%d]=%d", I, J, A[i][j]);

}

printf ("\ n");

}

int array[3][5]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14};

//

printf ("array[0][0]=%d\n", array[0][0]);

printf ("array[0][1]=%d\n", array[0][1]);

printf ("array[0][2]=%d\n", array[0][2]);

printf ("array[0][3]=%d\n", array[0][3]);

printf ("array[0][4]=%d\n", array[0][4]);

//

printf ("array[1][0]=%d\n", array[1][0]);

printf ("array[1][1]=%d\n", array[1][1]);

printf ("array[1][2]=%d\n", array[1][2]);

printf ("array[1][3]=%d\n", array[1][3]);

printf ("array[1][4]=%d\n", array[1][4]);

//

printf ("array[2][0]=%d\n", array[2][0]);

printf ("array[2][1]=%d\n", array[2][1]);

printf ("array[2][2]=%d\n", array[2][2]);

printf ("array[2][3]=%d\n", array[2][3]);

printf ("array[2][4]=%d\n", array[2][4]);

//

printf ("array[1][5]=%d\n", array[1][5]);

//

ARRAY[1][5]==ARRAY[2][0]

//

Output with A For loop

int array[3][5]={0};

for (int i=0; i<3; i++) {

for (int j=0; j<5; j) {

Array[i][j]=arc4random ()% (30-10+1) +2;

printf ("array[%d][%d]=%d\n", I,j,array[i][j]);

//                    }

//                }

//

Swap rows and columns of a two-dimensional array

//

int array[3][5]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

int array1[5][3]={0};

//

for (int i=0; i<3; i++) {

for (int j=0; j<5; j) {

//

printf ("array[%d][%d]=%d\n", I,j,array[i][j]);

ARRAY1[J][I]=ARRAY[I][J];

//                    }

//                }

//

for (int i=0; i<5; i++) {

for (int j=0; j<3; j) {

printf ("array1[%d][%d]=%d\n", I,j,array1 [i][j]);

//                    }

//                            }


There is a two-dimensional array of three rows and four columns, finding the largest element and outputting the rows and columns

int max=0,k=0,l=0,g=0;

int arr[3][4]={0};

for (int i=0;i<3;i++) {

for (int j=0; j<4; j) {

Arr[i][j]=arc4random ()% (30-10+1) +10;

printf ("arr[%d][%d]=%d\n", I,j,arr[i][j]);

if (Max<arr[i][j]) {

K=ARR[I][J];

Max=k;

Arr[i][j]=k;

L=i;

G=j;

}

}

If you ask for the maximum number and rank of each line, add this sentence: printf ("max=%d,l=%d,g=%d\n", max,l,g);

}

printf ("max=%d,l=%d,g=%d\n", max,l,g);


Multidimensional array: array subscript above two. The number of multidimensional array elements equals the product of each dimension multiplied (for example, int arr[2][4][3]={0} has 24 elements)

Multidimensional array traversal, several dimensional arrays require several layers for loop 0.

Multidimensional array element access: array name [subscript] [subscript] [subscript] [subscript] ....





/**
* Review: Character array: An array that holds character variables, and an array of characters that is also one-dimensional, similar to the normal character definition, except that the type is char
char array name [constant expression 1]={value 1}{value of 2};
Char array[10]={' i ', ' P ', ' h ', ' o ', ' n ', ' e '};
for (int i=0; i<10; i++) {
printf ("array[%d]=%c\n", I,array[i]);
}

Char array1[10]= "iphone6p";
for (int i=0; i<10; i++) {
printf ("aaray1[%d]=%c\n", I,array1[i]);
}
An array of characters with a ' I ' end identity is also called a string; the length of the character array is greater than the length of the character, which is used to store ' the '
Strlen () Compute string length

String comparisons
Note: The string comparison function is compared (subtraction) based on the ASCII code value of the character corresponding to the subscript. If the difference is zero, the two characters are equal, then the next ASCII value is compared until a ' yes ' is encountered, and if the difference is not zero, the difference is returned, indicating that two characters are different
int strcmp (< #const char *#> s1, < #const char *#> S2);
Comparing the size of two strings (not ignoring case), the return value is learned: If S1 is less than S2 returns a number less than 0, if S1 is greater than S2 returns a number greater than 0, and returns 0 if equal. The return value is the difference of the first unequal character ASCII in two strings
Find string strstr (const char *DST, < #const char *#>src);
String Copy strcpy (< #char *#>dst, const char *CSC);
string concatenation char*strcpy (< #char *#>s1, < #const char *#>s2);

*/        //
String Assignment strcpy
Char newstr[]={' i '};
strcpy (NEWSTR *, str);
printf ("newstr=%s\n", newstr);
//
string concatenation
strcat (STR,STR1);
printf ("str1=%s\n", str1);

An array of strings that hold the string (an array called a string) that ends with a "."

The string array defines the char array name [constant expression 1][constant expression 2] = {value 1, value 2, Value 3, ...}

Char string[3][10]={"iphone", "Android" "Windows"};

Char string1[3][10]={{' i ', ' P ', ' h ', ' o ', ' n ', ' e ', ' 7 '},{' A ', ' n ', ' d ', ' r ', ' O ', ' I ', ' d '},{' w ', ' I ', ' n ', ' 8 '}};

Note: The string array is actually a two-dimensional array of character types

No longer than two dimensions per character length minus one (string needs to be stored ' ")

Access string, array name [first dimension subscript]

Access character, array name [first dimension subscript] [second dimension subscript]

sizeof and strlen differences: Strlen refers to the length of the character, sizeof refers to the file or the data in memory (bytes)

Exercise 2 Create an array of strings (the contents are the names of the people around them) and output the length of the longest string

The result data type for string length is unsigned long (unsigned length shaping)

Both the assignment and the comparison require both types of consistency

Access string, array name [first dimension subscript]

Char arr[5][13]={"Zhangpengfei", "Qiaozipeng", "Lizhipeng", "Guoyaru", "lishuming"};

unsigned long max=0, k=0;

for (int i=0; i<5; i++) {

if (Max<strlen (Arr[i])) {

Max=strlen (Arr[i]);

K=i;

//            }

//        }

printf ("%s,max=%lu\n", Arr[k],max);

%c is input and output in character format
%s is input and output in string format

//


Enter the date of the year

int dayofyear (int year,int month,int day) {

int sum=0;

int rundays[]={31,29,31,30,31,30,31,31,30,31,30,31};

int days[]={31,28,31,30,31,30,31,31,30,31,30,31};

if (year%4==0 | | (Year%400==0 && (year%100!=0))

{

for (int i=0; i<month-1; i++)

{

Sum +=rundays[i];

}

} else {

for (int i=0; i<month-1; i++)

{

Sum +=days[i];

}

}

sum = day;

return sum;

}




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.