PART6 definition and initialization of arrays, pointers, and string 6.1 arrays

Source: Internet
Author: User
Tags array length

1 definition and use of arrays
An array is a set of variables of the same type that have a certain order relationship, and the variable that makes up the array is called the element of the array.

Using an array element must first be declared and then used.
You can only refer to an array element one at a time, but not the entire array

//6-1 Arrays#include <iostream>using namespacestd;intMain () {inta[Ten],b[Ten];  for(inti =0; I <Ten; i++) {A[i]= i*2-1; b[TenI1] =A[i]; }     for(inti =0; I <Ten; i++) {cout<<"a["<< I <<"] = "<< A[i] <<" "; cout<<"b["<< I <<"] = "<< B[i] <<Endl; }    return 0;}

2 storage and initialization of arrays
Storage of one-dimensional arrays:
Array elements are stored sequentially in memory, and their addresses are contiguous. Adjacent to the physical address of the element, corresponding to the adjacent logical order.
The array name is the memory address of the first element of the array. The array name is a constant and cannot be assigned a value.

Initialization of one-dimensional arrays:
1 list the initial values for all elements
For example: static int a[10]={0,1,2,3,4,5,6,7,8,9};
2 You can assign an initial value to only a subset of elements
For example: static int a[10]={0,1,2,3,4};
3 when assigning initial values to all array elements, you can not specify the array length
For example: static int a[]={0,1,2,3,4,5,6,7,8,9};

Storage of two-dimensional arrays:
Store by row, each line can be considered a one-dimensional array

Initialization of two-dimensional arrays:
1 writes all initial values within a {}, initialized sequentially
For example: static int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
2 branch lists the initial values of two-dimensional array elements
For example: Static int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
3 can be initialized only for partial elements
For example: Static int a[3][4]={{1},{0,6},{0,0,11}};
4 When all initial values are listed, the number of subscripts in the 1th dimension can be omitted
For example: static int a[][4]={1,2,3,4,5,6,7,8,9,10,11,12};
Or: Static int a[][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};

Attention:
1 if there is no initialization, there will be garbage data in the internal auto array, the data in the static array is initialized to 0 by default;
2 If only partial elements are initialized, the remaining elements that are not explicitly initialized are automatically initialized to zero;

//6-2 Finding the first 20 items of Fibonacci series#include <iostream>using namespacestd;intMain () {intf[ -] = {1,1};  for(inti =2; I < -; i++) {F[i]= f[i-2] + f[i-1]; }     for(inti =0; I < -; i++){        if(i%5==0) cout << Endl;//5 outputs per lineCout.width ( A);//Set output width tocout <<F[i]; }    return 0;}

//6-3 loop from the keyboard read several groups of choice answers, calculate and output the correct rate of each group of answers, until the input CTRL + Z. //Each group enters 5 consecutive answers, each of which can be ' a '. d '. #include <iostream>using namespacestd;intMain () {Const CharKey[] = {'a','C','b','a','D'}; Const intNum_ques =5; CharC; intQues =0, Numcorrect =0; cout<<"Enter the"<< num_ques <<"Question tests:"<<Endl;  while(CIN.)Get(c)) {        if(c! ='\ n'){            if(c = =Key[ques]) {Numcorrect++;cout <<" "; }Elsecout <<"*"; Ques++; }Else{cout<<"score"<< static_cast<float> (numcorrect)/num_ques* -<<"%"; Ques=0; Numcorrect=0; cout<<Endl; }    }    return 0;}

PART6 definition and initialization of arrays, pointers, and string 6.1 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.