"C-Array"

Source: Internet
Author: User

One or one-D arrays

①, define the way

Type specifier array name [constant expression];

such as: int array[10];

Attention:

1) The type of the array is actually the type of the exponent group element. For the same array, all of its elements have the same data type.

2) The writing rules of the array name shall conform to the written provisions of the identifiers.

3) The array name cannot be the same as other variable names.

4) The constant expression in square brackets represents the number of array elements, such as a[5], which indicates that array a has 5 elements. But its subscript is calculated starting from 0. So 5 elements were a[0], a[1], a[2], a[3], a[4].

5) You cannot represent the number of elements in square brackets, but they can be either symbolic constants or constant expressions.

②, quoting

The general form is: array name [subscript]

Exercise : Use a For loop to assign a value to an array and output the array in flashbacks.

  1. #include <stdio.h>
  2. int main(void){
  3. int i, a[ten];
  4. For(i=0; I<=9; I+ +) {
  5. A[i]=i;
  6. SACNF ("%d", &a[i]); Enter your own data
  7. }
  8. For(i=9; I>=0; I-) {
  9. printf("%d", a[i]);
  10. }
  11. return 0;
  12. }

③, initialization

1) You can assign an initial value to only a subset of elements. When the number of values in the {} is less than the number of elements, only the previous elements are assigned values.

2) You can assign values to elements only, not to the whole array.

3) If the entire element is assigned a value, then in the array description, you can not give the number of elements of the group.

such as: int a[3] = {£ º}; Can be written as: int a[] = {£ º};

Two or two-D arrays

①, definition

Type descriptor array name [constant-expression 1][constant expression 2];

②, quoting

Array name [subscript] [subscript]

Elements such as a[1][3] that represent the third column of the first row

Exercise : There are 5 people in a study group, each of whom has three-class exam results. The average score of the Perfection Component section and the total average score of each section.

-- Single Wang Li Zhao Week
Math 80 61 59 85 76
C 75 65 63 87 77
Foxpro 92 71 70 90 85

Idea: can set up a two-dimensional array a[5][3] to store five people three lessons of the results. Set up a one-dimensional array v[3] The average score of each branch is stored, and the variable average is the total average score of the whole group.

#include <stdio.h>

int main(void){

int i, J, s=0, average, v[3],a[5] [3];

printf("input score:\ n");

For(i=0; I<3; I+ +){

For(J=0; J<5; J+ +){

scanf("%d", &a[j][i]);

s=s+a[j][i];

}

v[i]=s/5;

s=0;

}

Average = (V[0 ]+v[1]+v[2])/3

printf("math:%d\c:%d\nfoxpro:%d\ n", v[0],v[1 ],v[2]);

printf("average =:%d\ n", average);

return 0;

}

③, initialization

The initialization of a two-dimensional array is also the initial value assigned to each subscript variable at the type description. Two-dimensional arrays can be assigned by row segments, or they can be continuously assigned by rows.

For example, array a[5][3]:

    • The assignment by row segment can be written as:
      int a[5][3]={{80,75,92}, {61,65,71}, {59,63,70}, {85,87,90}, {76,77,85}};
    • Successive assignments by row can be written as:
      int a[5][3]={80,75,92,61,65,71,59,63,70,85,87,90,76,77,85};

The result of these two initial values is exactly the same.

Description

1) You can only assign the initial value to some elements, and the elements without initial values automatically take 0.

2) If all elements are assigned an initial value, the length of the first dimension may not be given.

3) An array is a constructed type of data. A two-dimensional array can be thought of as a nesting of one-dimensional arrays. Each element of a one-dimensional array is also an array, and a two-dimensional array is formed. Of course, the premise is that each element type must be the same. According to this analysis, a two-dimensional array can also be decomposed into multiple one-dimensional arrays. The C language allows this decomposition.

Three, character array

The format is the same as the value array described earlier.
Example: Char c[10];
It can also be defined as int c[10] because of the character type and integral type.

Character arrays also allow initialization assignments at the time of definition.

Char c[10]={' h ', ' e ', ' l ', ' l ', ' o '};

End flag:

The C language allows the array to be initialized with a string of values.

Above can be written as: char c[10] = {"Hello"}

or remove {} write as:
Char c[10]= "c program";

Assigning a value in a string is more than one byte per assignment of a character, which is used to hold the string end flag ' \ s '.

Input Output:%s

Special Note : When you enter a string with the scanf function, the string cannot contain spaces, or a space will be used as the end of the string.

Four, the most commonly used string processing functions

Output: puts (character array name)

Input: Gets (character array name)

String connection: strcat (character array name 1, character array name 2) returns the first address of the character array 1

String copy: strcpy (character array name 1, character array name 2) requires a character array of 1 to have enough length to copy along with

string comparison: strcmp (character array name 1, character array name 2)

Compares the strings in two arrays in ASCII order and returns the comparison result by the return value of the function.
String 1 = String 2, return value = 0;
String 1〉 string 2, return value 〉0;
String 1〈 string 2, return value 〈0.

String length: strlen (character array name) The actual length does not contain the string end flag ' \ s ')

Exercise 1: Insert an integer into the sorted array in order of size.

Idea: In order to insert a number by size into an ordered array, you should first determine whether the sort is from large to small or to large. Set the order from large to small, you can compare the number you want to insert with each number in the array, and when you find the first element I that is smaller than the number of inserts, the insertion position is before the element. It then moves one cell at a time, starting from the last element of the array to the element. Finally, the insertion number is assigned to element I. Insert the last position if the number of inserts is smaller than all element values.

Code:

#include <stdio.h>int main (void) {    int i,j,p,q,n,a[10]={127,3,6,28,54,68,87,105,162,18};    for (i=0;i<10;i++) {        p=i;
q=a[i];//assigns the array element to the variable Q for (j=i+1;j<10;j++) if (Q<a[j]) {
         P=j;
Q=A[J];
} if (p!=i) {//swap int s=a[i]; A[I]=A[P]; a[p]=s; } printf ("%d", A[i]); } printf ("\ninput number:\n"); scanf ("%d", &n); for (i=0;i<10;i++) if (N>a[i]) {for (s=9;s>=i;s--) {
         a[s+1]=a[s];//element Move back
} break ; a[i]=n;//insert n into a[i] for (i=0;i<=10;i++) printf ("%d", A[i]); return 0;}

Exercise 2: Select the largest element of each row in a two-dimensional array A to form a one-dimensional array B.

Idea: Find the largest element in each row of array A, and then assign that value to the corresponding element in array B.

Code:

#include <stdio.h>int main (void) {    int a[][4]={3,16,87,65,4,32,11,108,10,25,12,27};//defines an array a    int b[3] , I,j,n;    for (i=0;i<=2;i++) {        n=a[i][0];//each line of the first element for        (j=1;j<=3;j++)            if (a[i][j]>n) {
N=A[I][J];
b[i]=n;//assigns the maximum number of each row to the array b } printf ("Array a:\n"); for (i=0;i<=2;i++) { for (j=0;j<=3;j++) printf ("%5d", A[i][j]); printf ("\ n"); } printf ("\narray b:\n"); for (i=0;i<=2;i++) printf ("%5d", B[i]); printf ("\ n"); return 0;}

"C-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.