13-Dark Horse programmer------C language Learning Note---C language one-dimensional array

Source: Internet
Author: User

Dark Horse programmer------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------

Four simple construction data types

One by one-D arrays

Definition of a 11-D array:

Type identifier array name [constant expression]; such as: int a[10]; the expression defines an integer array named a, which has 10 elements, 10 integer variables, which can store 10 integer data.

The 02 type identifier represents the data type of an array element, the array name is a user-defined identifier, and the first address of the memory area that the array occupies, and the constant expression is enclosed in square brackets, whose value represents the number of array elements, that is, the array length.

The C language stipulates that the subscript of an array starts with 0, and if an array with n elements is defined, its elements correspond to the subscript 0~n-1

/*A class of 30 students is known to take the C language exam, requiring programs to be written, input each student's scores, calculate average scores, and output all students ' test scores and average scores*/#include<stdio.h>intMain () {floatcscore[ -]; floatsum=0, average; inti; printf ("Please enter the score of 30 students: \ n");  for(i=0; i< -; i++) {printf ("grade%d student results \ n", i+1); scanf ("%f",&Cscore[i]); Sum= sum+Cscore[i]; } Average= sum/ -;  for(i=0; i< -; i++) {printf ("score of%d:%f\n", i+1, Cscore[i]); printf ("average=%f\n", average); }        return 0; }

04 The reference form of an array element:

Array name [subscript]

When referencing, the subscript can be an integer constant, or it can be an integer variable or an integer expression that is assigned a value.

The C language allows you to specify an initial value for each element when the array is defined, called the initialization of the array.

such as: int a[10]={0,1,2,3,4,5,6,7,8,9};

You can also assign values to only a subset of the array elements, the system assigns the default values to the rest of the elements, and when the elements are numeric data, the default is 0, and the character data defaults to ' \ n ',

/*prompts the user to enter 5 students ' scores, calculates the average score and outputs*/#include<stdio.h>voidtest ();intMain () {intscore[5]; intsum =0;  for(inti =1; i<=5; i++) {printf ("Please enter the score of%d students: \ n", i); scanf ("%d", &score[i-1]); Sum+ = score[i-1]; } printf ("average is%f\n", sum/5.0);        Test (); return 0; }voidTest () {intAges[] = {4, -, +};  for(inti =1; i<=3; i++) {printf ("ages[%d] 's address is%p\n", I-1, &ages[i-1]); }                                    }

06 Assign values to all array elements, without specifying the length of the array, whose length is determined automatically by the number of initial values

07 arrays can only be assigned when initialized, and cannot be assigned as a whole when used

/*int count = 5;int Ages[count];ages[0] = 1;ages[1] = 6;ages[2] = 8;//error notation//If you want to initialize at the same time as the array is defined, the number of elements in the array must be constant or not write/int AG Es[count] = {10,11,12};*/#include<stdio.h>intMain () {intages[]={Ten,4,5,8, +,}; intCount = (sizeof(ages)) /sizeof(int);  for(inti =0; i<count; i++) {printf ("ages[%d]=%d\n", I,ages[i]); }        return 0; 

13-Dark Horse programmer------C language Learning Note---C language one-dimensional array

Related Article

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.