(1) Enter 5 numbers and print them in reverse order
(2) Enter 50 numbers and print them in reverse order.
(3) Enter n numbers and print them in reverse order.
An array is an ordered collection of elements of the same type that store a fixed size.
2. How do I use arrays?2.1 defining arrays
(1) Grammar
Type array name[Number of elements];
(2) Examples
int days[12];
2.2 initialization array
(1) Grammar
Type array name[Number of elements] = ;
(2) Examples
int days[12]=;
(3) Simplify
You can initialize the array without specifying the array size. At this time, the size of the array is the number of elements at the time of initialization. For example:
int days[]=;
This initialization method is called array integration initialization.
2.3 accessing array elements
Access is reading and writing
Array elements can be accessed by adding an index (subscript) to the array name. The index (subscript) of the element is placed in square brackets, following the array name. Note: the index (subscript) of the array starts from 0.
(1) Grammar
Array name[Indexes];
Each array element is a variable, and the type of the variable is the type of the array declaration.
Array elements can be left or right of the assignment.
(2) Examples
// Gets the number of days in January int n = days[0]; // Modify the number of days in February days[1]=29; // Print days of January printf("%d",days[0]);
2.4 array traversal
(1) Basic routine
for (int i=0;i<n;i++){ // From 0 to n-1 array indexes are generated in turn arr[i] // Access each element of the array }
(2) Practice
Print the month and corresponding days in order
#include <stdio.h> int main(){ int days[]=; int i=0; for(i;i<12;i++){ printf("The first%d The number of days in the month is%d\n",i+1,days[i]); } }
2.5 array input
(1) Case
int n; scanf("%d",&n); int arr[n]; for (int i=0;i<n;++i){ // From 0 to n-1 array indexes are generated in turn scanf("%d",&arr[i]); // Access each element of the array }
(2) Practice
a) Input 10 integers and output them in sequence according to the input direction.
#include <stdio.h> int main(){ int nums[10]={}; int i=0; for(i;i<10;i++){ scanf("%d",&nums[i]); } i=0; for(i;i<10;i++){ printf("%d\n",nums[i]); } }
b) Input 100 integers and output them in sequence according to the input direction.
#include <stdio.h> int main(){ int nums[100]={}; int i=0; for(i;i<100;i++){ scanf("%d",&nums[i]); } i=0; for(i;i<100;i++){ printf("%d\n",nums[i]); } }
2.6 array assignment
Array cannot be assigned directly with = and can be assigned by traversal.
for(int i=0;i<12;++i){ temp[i] = days[i]; }3. Characteristics
(1) The size of the array cannot be modified after it is created.
(2) All elements in the array have the same data type.
(3) The elements in the array are arranged successively in memory.
4.1 statistics
How many months in December are 30 days? How many months are 31 days?
#include <stdio.h> int main(){ int days[]=; int days_31=0,days_30=0,i=0; for(i;i<12;i++){ if(days[i] == 31){ days_31++; }else if(days[i] == 30){ days_30++; } } printf("31 Days of months%d individual\n",days_31); printf("30 Days of months%d individual\n",days_30); }
4.2 replacement
Enter the year and print the days of all months in the current year. (Note: leap years replace February days)
#include <stdio.h> int main(){ int year,i; scanf("%d",&year); int days[]=; int days1[12]={}; for(i=0;i<12;i++){ days1[i]=days[i]; } days1[1]=(year%4==0 &&year%100!=0 || year%400==0)?29:28; printf("%d The number of days in each month of the year is as follows:\n",year); for(i=0;i<12;i++){ printf("%d ",days1[i]); } }
4.3 search
(1) What is the first 30 day month?
#include <stdio.h> int main(){ int days[]=; int i; for(i=0;i<12;i++){ if(days[i]==30){ break; } } printf("The first 30 day month is%d month\n",i+1); }
(2) What month is the nth 30 day month?
#include <stdio.h> int FindDays(int y){ int days[]=; int x=y,i=0; while(y){ if(days[i] == 30){ --y; } i++; } printf("The first%d A 30 day month is%d month\n",x,i); } int main(){ int n; printf("Please enter the number you want to query n 30 day months (0)<n<5:\n"); scanf("%d",&n); if(n>=0 && n<=4){ FindDays(n); }else{ printf("Input Error!\n"); } }
(3) Enter the year and month to get the number of days in this month
#include <stdio.h> int main(){ int year,month; printf("Please enter the year and month of the query:\n"); scanf("%d%d",&year,&month); int days[]=; printf("%d-%d have%d day",year,month,days[month-1]); }
4.4 accumulation
(1) Enter the year, starting month m and ending month n, and calculate the number of days from m to n.
#include <stdio.h> int main(){ int month1,month2,year,sum=0,i; printf("Please enter two months between years:\n"); scanf("%d%d%d",&year,&month1,&month2); int days[]=; for(i = month1+1;i<month2;i++){ sum+=days[i]; } printf("%d-%d and%d-%d Between%d day\n",year,month1,year,month2,sum); }
(2) Enter a day of a month of a year to judge which day of the year this day is?
#include <stdio.h> int main(){ int month,day,year,sum=0,i; printf("Please enter the date:\n"); scanf("%d%d%d",&year,&month,&day); int days[]=; for(i=0;i<month-1;i++){ sum+=days[i]; } sum+=day; printf("%d-%d-%d yes%d The second day of the year%d day\n",year,month,day,year,sum); }