Dark Horse Programmer-C Learning Diary-arrays

Source: Internet
Author: User

-------iOS training, Android training, Java training, and looking forward to communicating with you! ----------

A Basic concepts of arrays

1. array: A collection of data used to hold multiple identical data types

Define variable: Data type variable name

Define array: Data type array name [number of elements]

Two Multiple initialization methods for arrays

1. arrays are only defined for initialization and can only be initialized for use

Array traversal: Take a look at each element in the array

Code Demo :

1#include <stdio.h>2 3 intMainintARGC,Const Char*argv[]{4 5 //define an array6 7    intScores [0];={1,9,9,0,7,9, One};8 9 //computes the number of bytes occupied by an array, that is, the number of array elementsTen  One    intCount =sizeof(nums)/sizeof(int); A  -  //To iterate over an array: -  the     for(intI=0; i<count;i++){ -  -printf"scores [%d]=%d", I,nums[i]); -  +     }  -  +   return 0; A  at}

1.1 using constants as the number of elements

1.2 uses variables as the number of elements (C99 does not support using variables as the number of elements, but LLVM supports)

The benefit of using variables as the number of elements: You can decide how many elements you want to create when the program runs to this line

   

2. initialize the array as you define it

2.1 Specifies the number of elements and initializes all of them at the same time

 int nums[5] = {1,2,3,4,5}

2.2 does not specify the number of elements, all initialization

 int nums[] = {1,2,3,4,5}

2.3 Specifies the number of elements, partially initialized: Starting with the element subscript 0, the element system with no explicit initialization is automatically initialized to 0

int nums[5] = {.}

2.4 Specifies the number of elements to initialize for the specified element

 int nums[5] = {[1] = 1,[2]=5}

Note: It is not possible to use variables as the number of elements when defining an array for simultaneous initialization

Three. Self-understanding

The array name itself is an address

1. the address of the array is the address of the first element

2. the array is a contiguous storage space

3. Subscript Small element address on small address, subscript large element on large address

int chars[3] = {' a ', ' B ', ' C '} ;

Subscript out of Bounds:

Problems caused by array subscript out of bounds :

The wrong object, access to the elements you should not access, there are inexplicable errors

Out of bounds beyond a large range, program crashes

Summarize:

1. The basic data type is passed as a function parameter;

2. An array is passed as a function parameter type, and if a value of the array is changed in the function, the value of the argument is changed.

3. When the array is passed as a function parameter, it is automatically converted to the pointer type. Therefore, the length of the parameter group cannot be obtained in the function, only the key function can pass in

Four Basic concepts of two-dimensional arrays

Two-dimensional arrays: elements are arrays of one-dimensional arrays;

Format: Data type array name [ number of one-dimensional array ] [ number of one-dimensional array elements ]

Five Defining a two-dimensional array

Char Scores[3][2]

To traverse a two-dimensional array:

Code Demo:

1#include <stdio.h>2 3 intMainintARGC,Const Char*argv[]{4 5  //define a two-dimensional array6 7   Charscores[3][2]8 9  //Outer loop control row (number of one-dimensional arrays)Ten  One    for(intI=0;i<3; i++){ A  -      //Inner Loop control column (number of one-dimensional array elements) -  the       for(intj=0;j<2, J + +){ -  -printf"scores[%d][%d]=%c\n", I,j,scores[i][j]); -  +       } -  +    } A  at   return 0; -  -}

Multiple initialization methods for two-dimensional arrays:

1. First define a two-dimensional array, and then initialize (when defining a two-dimensional array, you must specify the number of elements of the one-dimensional array)

2. Defining two-dimensional array simultaneous initialization

2.1 Specify the number of one-dimensional arrays to initialize all elements

int scores[3][2]={{1,2},{3,4},{5,6}};

2.2 does not specify the number of one-dimensional arrays to initialize all elements

int scores[][2]={{1,2},{3,4},{5,6}};

2.3 Specifies the number of elements of a one-dimensional array, initialized with a curly brace

int scores[3][2]={1,2,3,4,5,6};

int scores[][2]={1,2,3,4,5,};

Ps: Initialized at the same time as defined, elements that are not explicitly initialized are automatically initialized to 0

Six Practice

1. Enter a class score, then calculate the average score, how many people in the class need to enter their own

#include <stdio.h>intMainintARGC,Const Char*argv[]{//Define student number variables, prompting users to enter the number of class students  intcount; printf ("Please enter the class number of students \ n"); //receiving user's inputscanf ("%d",&count); //define an array save student scores  intScores[count];//receive the results of each student entered by a user  for(intI=0; i<count;i++) {printf ("Please enter the grade of%d students: \ n", i+1); scanf ("%d",&Scores[i]); } //Calculate student Total   intsum =0;  for(intI=0; i<count;i++) {sum+=Scores[i]; } //Calculate Average   DoubleAverage = (Double) sum/count;//output average student scoresprintf ("The average student score is:%.2lf\n", average); return 0;}

2. Bubble sort rule: big sinking, small float

#include <stdio.h>intMainintARGC,Const Char*argv[]{array elements in ascending orderintnums[]={1,9,9,0,7,9, One}; //calculating the length of an array intLength =sizeof(nums)/sizeof(int); //using bubble sorting//Outer loop control number of times  for(intI=0; i<length-1; i++){     //Inner loop control how many times each trip compares      for(intj=0; j<length-1-i); J + +{        //element 22 Compares, if the preceding element is large, then the position is swapped with the following element         if(nums[j]>nums[j+1]){           inttemp; Temp=Nums[j]; NUMS[J]=nums[j+1]; Nums[j+1]=temp; }}} to iterate through the array and print the sorted results for(intI=0; i<length;i++) {printf ("%d", Nums[i]); }return 0;}

Dark Horse Programmer-C Learning Diary-arrays

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.