Location>code7788 >text

while and do loops, buffers, one-dimensional arrays

Popularity:958 ℃/2024-10-18 11:25:58

buffer

input buffer

When getting data from the keyboard the data entered by the user first goes into the input buffer, then the program gets the number from the input buffer, the data that goes into the input buffer first must be processed first (similar to queuing), if the data that goes into the input buffer first can't be processed, the program doesn't get the data that goes into the input buffer later. Invalid data in the input buffer can be removed using the following two fixed statements:

scanf("%*[^\n]"); //delete everything before the first '\n' character in the input buffer
scanf("%*c"); //delete the first character in the input buffer

output buffer

The printf standard function puts the content to be displayed in the output buffer, and the computer displays the content in the output buffer on the screen at the appropriate time. The content in the output buffer is displayed on the screen in the following four cases:

1. If the output buffer contains a newline character, the portion of the output buffer preceding the newline character is displayed on the screen.

2. If the program ends, the contents of its output buffer are displayed on the screen.

3. If the output buffer is full, its contents are displayed on the screen.

4. Use a fixed statement, fflush(stdout), in your program to force the contents of the output buffer to be displayed on the screen.(The scanf function also does this)

arrays

Arrays can be used to represent a set of consecutive storage areas of the same type in memory, these storage areas are called elements of the array, arrays also need to be declared before they can be used, the declaration of arrays in addition to the need to provide the name of the type and the name of the array also needs to be provided to provide an integer to indicate the number of storage areas in the array.

Once the array exists it contains the number of storage areas can not be changed, the array usually can not be used as a whole, generally only one time to use one of the storage areas, the number of each storage area in the array is different, this number is called the array subscripts, the first storage area in the array subscript is 0, and so on, the maximum subscript is the number of storage areas minus one.Subscripts beyond the range are not allowed (similar to running a red light)

The array name and subscripts together can be used to represent the storage areas in the array, you can process all the storage areas in the array sequentially in a for loop, the loop variable of this loop should represent all the valid subscripts in the array sequentially, you can provide more than one data when initializing the array, they should be written in the middle of a pair of curly braces, and the adjacent initialization data should be separated by parentheses, and the excess data will be ignored if the number of initialization data is more than the number of storage areas when initializing the array. When initializing an array, if the number of initialization data is more than the number of storage area, the excess initialization data will be ignored.

When initializing an array, if the number of initialized data is less than the number of storage areas, initialize the back of the storage area to 0. When initializing an array, if the number of initialized data is the same as the number of storage areas in the array, you can omit the number of storage areas in the array declaration.

Exercise: Write a program to get multiple numbers from the keyboard (the number of numbers is up to 5, given by the user's implementation), display these numbers on the screen in reverse order, and then calculate the sum of these numbers and display the result on the screen.

Exercise: Write a program to get a lottery ticket (the ticket consists of 7 integers between 1 and 36) and then display all the numbers in the ticket on the screen.

The array name cannot represent the storage area, the array name can be used to represent the address of the first storage area, the array name cannot be assigned a value, you can do a sizeof calculation on the array name, the result is the total size of all the storage areas in the arrayThe C99 specification allows the use of variable-length arrays, which are declared with a variable indicating the number of storage areas in the array.

variable-length array

If the program is run multiple times, the number of storage areas in the variable-length array may be different each time. The number of areas contained in the variable-length array does not change during a given run of the program.Variable-length arrays cannot be initialized

one-dimensional array

Declaring a one-dimensional array requires only one integer to represent the number of storage areas in the array.

multidimensional array

Multidimensional arrays can not only represent multiple storage areas, but they can also be grouped (grouping can be done multiple times).

two-dimensional array

Two-dimensional arrays are the most common multidimensional arrays, he can only represent the storage area of a grouping situation, the declaration of two-dimensional arrays need to provide two integers, theThe first integer represents the number of groups and the second integer represents the number of storage areas in the group.The two-dimensional arrays are not usually used as a whole. Two-dimensional arrays are also usually not used as a whole; they are usually used one at a time.Two subscripts are required to indicate the storage area in a two-dimensional array, the first one indicating the number of the group (group subscript) and the second one indicating the number of the storage area in the group (group subscript).The valid range of group subscripts is from 0 to the number of groups minus one; the valid range of intra-group subscripts is from 0 to the number of storage areas in the group minus one.

You can initialize a two-dimensional array as if it were a one-dimensional array, or you can initialize a two-dimensional array by grouping the initialization data, with each group of initialization data being used to initialize a set of storage areas. If you can calculate the number of groups according to the number of initialized data, you can omit the number of groups in the two-dimensional array declaration.

Click to view code
/*
 *
 *
 * Input buffer management
 *
 *
 * */
#if 0
#include<>
int main (){
int num = 0;
printf("Please input a number:\n");
scanf("%d",&num);
scanf("%*[^\n]"); scanf("%*c",&num); scanf("%*[^\n]")
scanf("%*c");
printf("num=%d\n",num);
printf("Please enter a number:\n");
scanf("%d",&num);
printf("num=%d\n",num); printf("num=%d\n",num);
return 0.
}

#elif 0

 *
 * Output buffer
 *
 *
 * */
#include<>
int main (){
printf("1"); fflush(stdout); fflush(stdout)
fflush(stdout); while (1){
while (1){
}
return 0; }
}


#elif 0

/*
 *
 * Array demo
 *
 * */
#include<>
int main (){
int arr[5]; // array declaration statement
int num = 0;
arr[2] = 10;
for (num = 0;num <= 4;num ++){
arr[num] = num;
}
for (num = 4;num >= 0;num --){
printf(" %d ",arr[num]);
}
printf("\n");
return 0;
}

#elif 0
/* * * * * *
 *
 * Array exercise
 *
 *
 * */
#include<>
int main (){
int arr[5] = {0};
int qty = 0,num = 0,sum = 0; //number of digits entered by user qty <5
printf("Please enter the number of digits:\n");
scanf("%d",&qty);
for (num = 4;num >= 0;num--){
printf("Please enter a number:\n");
scanf("%d",&arr[num]);
if (num == 5 - qty){
//check if the array is filled
break;
}
}
for (num = 5 - qty;num <= 4;num++){
printf("%d",arr[num]); //display user-entered numbers in reverse order
}
printf("\n");
for (num = 5 - qty;num <= 4;num++){
sum += arr[num]; }
}
printf("The result of the summation is: %d \n",sum);
return 0; }
}

#elif 0
/* * * * * *
 *
 * Getting seven random numbers between 1 and 36
 * * Display the numbers
 *
 * */
#include<>
#include<>
#include<> #include<>
int main (){
int num = 0,tmp = 0,cnt = 0,num1 = 0; //cnt is used to keep track of the number of digits that have been obtained.
int arr[7] = {0},arr1[7] = {0}; srand(time(0)); //cnt is used to record the number that has been validated.
srand(time(0)); /*for (num = 0; //cnt
/*for (num = 0;num <= 6;num++){
arr[num] = rand() % 36 + 1;
}// Contains duplicate numbers
*/
do {
tmp = rand() % 36 + 1;
for (num = 0;num <= cnt - 1;num++){ //compare the new number with all the previously obtained numbers in sequence
if (tmp == arr[num]){
break;
}
}
if (num == cnt){
arr[cnt] = tmp.
cnt++; }
}
}while(cnt < 7).
for (num = 0;num <= 6;num++){
printf(" %d",arr[num]); }
}
printf("\n");
for (num = 0;num <= 6;num++){
printf("Please enter a number: \n"); }
scanf("%d",&arr1[num]);
}
cnt = 0;
for (num = 0;num <= 6;num++){
for (num1 = 0;num1 <= 6;num1++){
if (arr[num] == arr1[num1]){
cnt++;
break;
}
}
}
printf("Number of numbers bought correctly %d\n",cnt); return 0; }}
return 0; }
}


#elif 0

#include<>
int main (){
int arr[5] = {0};
printf("arr is %p\n",arr);
printf("arr[0] is %p\n",& arr[0]);
printf("sizeof(arr)=%ld\n",sizeof(arr));
return 0.
}

#elif 0
#include<>;
int main (){
int size = 0; int
printf("Please enter a number:");
scanf("%d",&size);
int arr[size]; // variable length array
printf("sizeof(arr)=%ld\n",sizeof(arr));
return 0; }
}


#elif 1
/*
 *
 *
 * two-dimensional array
 *
 * */
#include<>
int main (){

   
int row = 0, col = 0; for (row = 0; row = 0; col = 0)
for (row = 0;row <= 2;row++){
for (col = 0;col <= 1;col++){
arr[row][col] = cnt.
cnt++;
}
}
for (row = 0;row <= 2;row++){
for (col = 0;col <= 1;col++){
printf(" %d ",arr[row][col]);
}
printf("\n");
}
return 0; }
}
#endif