C-Language Foundation sequel

Source: Internet
Author: User
Tags array definition sorted by name

1.  the definition of a two-dimensional array is very similar to the definition of a one-dimensional array, except that there is one dimension compared to an array      defining an array:  data type    array name/[number of rows] [ Number of columns] =  initial value (multiple elements assigned initial value to be enlarged brackets)     for example:  int a[5] [5] = {0};     2.  assign values to elements in a two-dimensional array      assign values to the first row of elements     outer loop to control the number of rows       for  (int i = 0; i< 5; i++)  {         a[0][i] = i + 1;   }  assigning values to the second row of elements      for  (int i = 0; i< 5; i++)  {         a[2][i] = i + 1 + 5;    assigning values to the third row of elements       for  (int i = 0; i< 5; i++)  {              a[0][i] = i +  1 +5*2;     }                          memory loops to control the number of columns           for  (int j = 0; j < 5; j++)  {             for  (int i = 0; i< 5;  i++)  {             a[0][i] =  i + 1;             a[j][i]  = i + 1 + 5*j;              printf ("%-2d ", A[j][i]);         }              printf ("\ n");          }         int a[4][5] ={0}; int b[5][4] =  {0};     for  (int i = 0; i < 4; i++)  {         for  (int j; j < 5;  j++)  {             b[j][i] =  a [i][j];         }          printf ("%d ", B[j][i]);     }                int c[3][4] = {0};      int max = 0;//is used to record the maximum value      int row =  0,column = 0;row  used to record when,column  used to record columns     3.       assigning values to two-dimensional arrays       iterates through the array elements, finding the maximum value in the group element, recording the row and column      for  (int i = 0; i  < 3; i++)  {        for  (int j =  0; j < 4; j++)  {              c[i][j] =arc4random ()  %  (30 - 10 +10)  +10;              printf ("%d ", C[i][j]);          }         printf ("\ n");      }     for  (int i = 0; i  < 3; i++)  {                  for  (int j = 0; j < 4; j++)  {              if  (Max < c[i][j])  {                 max = c[i][j];                  row =  i;                  column = j;             }                  }      }         for  (int i = 0;  i < 3; i++)  {              for  (int j = 0; j < 4; j++)  {                  if  (C[i][j] == max)  {                       printf ("Behavior:%d, classified as:%d\n", I,j);                  }             }          }        printf ( "max = %d,row = %d,column = %d\n",  max ,row,column);         annotations When:   a two-dimensional array definition, the first-dimension subscript may not be given (the number of rows can be omitted), but the second-dimensional column number subscript must be specified (must not be omitted, the number of columns indicates when the line is wrapped)           the subscripts of the first and second dimensions must be constants or constant expressions      int a  [][3] = {1,2,3,4,5,6,7};     for  (int i = 0;  i <3; i++)   {         for  (int j = 0; j <  3; j++)  {             printf ( "%d ", A[i][j]);         }          printf ("\ n");     }             4.  array of strings, which are called string groups, except that the string is stored in a one-dimensional character array, so essentially a two-dimensional character array .     char str[3][10] = {"Frank", "Dunke", "iphone"};     printf (" %s ", str[2]);           Enter three words to find and output the longest word         char str[3][10] = {"Frank", "Dunke", "iphone"};      int  maxlength  (str)  = 0;     if  (strlen (str)  > maxlength (str)) {         strlength (str)  = strlen (str);          printf ("Length%lu ",  strlen (str));    &NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;CHAR&NBSP;STR[3][10]  = {0};     printf ("Please enter the name of three persons: \ n");       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SCANF ("%s%s%s", str[0],str[1],str[2]);      unsigned long maxlength = 0;//unsigned  unsigned      for  (int  i = 0;i < 3;i++)  {                if  (strlen (str [i] ) > maxlength)  {                     Maxlength = strlen (Str[i]); &NBSP;&NBsp;              }                         }                     printf ("%lu\n", maxLength);     output the longest word,     After the length of the longest word is obtained, the longest length is compared to the length of each string, and once the same is found, the string .     for  is immediately output (int i =  0;i < 3;i++)  {               if  (strlen (Str[i])  == maxlength)  {                             printf ("%s\n", Str[i]);                        } /                   }          Example:    has 10 contacts (assuming only person names), sorted by name from small to large     char str[10[10]={"Frank", "Duke", "account", "Kisi", "Jack", "Ironman", "Spiderman", "Merry", "Cherry", "Rose"};    char tempstr [ 10] = {0};5.  Store temporary string      array name represents the first address of the array, is a constant address, cannot be modified .      printf ("Please enter 10 person's name: \ n");     for  (int i = 0; i  < 10; i++)  {        scanf ("%s", Str[i]);      }    for  (int i = 0; i < 10 -  1; i++)  {        for  (int j = 0;  j < 10 - 1 -i; j++)  {                    if  (strcmp (str[j], str[j + 1])  > 0)  {                6.   When swapping strings, use the strcpy function                                          strcpy (Tempstr,str[j]); Copy the contents of the Str[j] string to a temporary character array tempstr                          strcpy (str[j], str[j + 1]);                        strcpy (str[j  + 1] ,tempsTR);             }          }    }    printf ("List of people with moral integrity: \ n");     //output Sequential string     for  (int i = 0; i <  10; i++)  {        printf ("%s\n", Str[i]);   The meaning of the existence of the   }          function: Splitting the complex logic in the main function, By a separate function to handle a single module, the main function only plays the role of macro-control .   functions: including four parts;  1. Returns a value type (the type of data to return).  2. Function name ( Give the function a concise name).  3. Parameter list (used to accept incoming data).  4. function body (function real function, actual operation)   function definition of a format: (no parameters, no return value)   function name naming specification: Consists of multiple words, in addition to the first word, the first letter of the remaining words .  the naming specification of the project name: there are words, and each word is capitalized (remember: it is a word, not a phonetic alphabet) The naming specification of .  variable name: There are words to make, The first letter of the word except the first one is capitalized .  void buymilk  ()  {    //function Body      printf ("Can't Buy, no money"); the second format defined by the  } //function:  (no returnvalue, with parameters)  void buymilkversiontwo (Float money);  void buymilkversiontwo (Float money) {     printf ("haha   I bought it for you but I drank it myself"); the third format defined by the  } //function: (with a return value, no parameters)  int  Givemoney ()  {    return 1; return is used to return values, put the returned data after return, and who calls the function returns the value to WHO &NBSP;&NBSP, be sure to keep in mind that you do not put the code behind the return and will not execute         printf ("work hard for the next year and continue to refuel." n "); four formats defined by the  } //function: (with a return value, with parameters)  //The maximum value of two integers (separated by commas between multiple parameters)//control reaches end of  non-void function //does not arrive at the end of a return value that is not an empty function  //reason: a non-empty function needs a return value, and identifying the end of the function after return means the solution: Return the final data, (return action)    function and function can be nested calls, but can not nest definition  Function definition is not  allowed here  explains that this is not allowed to define functions   cause: There is a nested definition between functions and functions. (That is, another function;  is defined in one function body)   WORKAROUND: The definition of the inner function refers to the outside of the function to form a side-by-side relationship between the function and the function;   example 1. int  Maxvluefortwo (int a ,int b)  {    int max = 0;    if  (a > b)  {         max = a;     } else {          max = b;     }     return  max;  }  Example 2. int maxvaluefortwo (int a ,int b)  {     return  a > b ? a : b; } int  Minvaluefortwo (int a, int b)  {     return a < b &NBSP;?&NBSP;A&NBSP;:&NBSP;B;&NBSP: The maximum value of three numbers  int maxvalueforthree (int a ,int b,int &NBSP;C)  {     The largest     int max = 0;   of a and B first   max = maxvaluefortwo (A, b);      max =  Maxvaluefortwo (max, c);  &nbsP;  return max ;    return maxvaluefortwo (MaxValueForTwo (a,  b), c);         }  for a maximum value of four numbers  int maxvalueforfour (int &NBSP;A&NBSP;,INT&NBSP;B,INT&NBSP;C,&NBSP;INT&NBSP;F)  {    return maxvaluefortwo (Maxvalueforthree (a,b,c),   f); }  five-digit maximum   function declaration: Compared to the function definition, only the   function body is missing, and the last semicolon is added.  int  maxvalueforfive (int a ,int b,int c,int f,int e);  void sayHi () {     printf ("i love ios\n");    } int main (INT&NBSP;ARGC,  const char * argv[])  {     function use three parts;     1. Declaration of function;     2. function definition;     3. Call to function;     function call]      buyMilk  (); Function name  +  parameter list (argument)       Buymilkversiontwo (0.1);   &nBsp;  sayhi ();      int money = givemoney ();      printf ("money = %d", money);      int maxvalue =  maxvaluefortwo (12,30);     printf ("maxvalue = %d\n", MaxValue);      int minvalue = minvaluefortwo (12, 30);      printf ("minvalue = %d\n", MinValue);  Five number of maximum      arguments: actual parameters, real data, That is, the arguments given at the function call are called arguments      parameters: Formal arguments, which are used only to indicate what type of data is received, the specific stored data is unknown, that is, the parameter given when the function is defined is called formal parameters       when a function is called, the passing of an argument to a formal parameter is a copy of the process.     int maxvalue = maxvalueforfive (10,  20,30,40,50);     printf ("maxvalue = %d",maxvalue );             return 0; } int maxvalueforfive (int  a ,int b,int c,inT f,int e) {    return maxvaluefortwo (Maxvalueforfour (a,b,c,f), e);  }7.   Header file Import when importing a custom folder, using "", when importing a system-defined header file, using the <> 8.  struct is a custom data type and a large container for storing multiple data, except that the structure is more flexible than the array It can store different types of data .  structure definition  struct +  structure name  {braces fill in struct members, and multiple struct members are separated by semicolons} + ;( Semicolon required) struct student {  struct member    int number; //storage number               char sex ;    //Storage Sex };//semicolon is the end sign of a struct definition, essential  typedef struct student Student; struct point {      float x;//Storage horizontal     float y;//storage vertical coordinates  };   The struct type struct student  redefined a name student typedef the second format, while defining the structure of the body, typedef typedef struct  rect1 {     int length; Storage Length     int width; Storage width  }rect1; typedef struct rectangular rec;typedef  the first format, define the struct type first, and then typedef typedef  type redefine ( Re-name the existing type), the new name and original type have the same function  typedef int Integer;


C-Language Foundation sequel

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.