C Language Learning Tutorials fourth-arrays (8)

Source: Internet
Author: User
Tags array length arrays comparison data structures printf

In two-dimensional array A, the largest element of each row is selected to form a one-dimensional array of B. A=3 4 108 37b= (87 108 37) The idea is to look for the largest element in each row of array A, and then assign the value to the corresponding element of array B. The procedure is as follows:
Main ()
{
static int a[][4]={3,16,87,65,4,32,11,108,10,25,12,27};
int b[3],i,j,l;
for (i=0;i<=2;i++)
{l=a[i][0];
for (j=1;j<=3;j++)
if (a[i][j]>l) l=a[i][j];
B[i]=l;}
printf ("\narray a:\n");
for (i=0;i<=2;i++)
{for (j=0;j<=3;j++)
printf ("%5d", A[i][j]);
printf ("\ n");}
printf ("\narray b:\n");
for (i=0;i<=2;i++)
printf ("%5d", B[i]);
printf ("\ n");
}

A For statement nested in the first for statement in the program consists of a double loop. The outer loop controls the line-by-row processing and assigns the No. 0 column element of each row to L. After entering the inner loop, I compare L to the following column elements and give l the larger than L. L is the largest element of the line at the end of the inner loop, and the L value is given to B[i]. When the outer loop is complete, the maximum value in the rows of a is loaded in array B. The following two for statements output arrays A and A and B respectively.

Enter the names of five countries in alphabetical order output.
The idea of programming is as follows: Five country names should be handled by a two-dimensional character array. However, the C language provides that a two-dimensional array can be treated as multiple one-dimensional arrays. So the subject can be processed in five one-dimensional arrays, and each one-dimensional array is a country name string. Compare the size of each one-dimensional array with the string comparison function, and sort the output.
Programming as follows:
void Main ()
{
Char st[20],cs[5][20];
int i,j,p;
printf ("Input country ' s name:\n");
for (i=0;i<5;i++)
Gets (Cs[i]);
printf ("\ n");
for (i=0;i<5;i++)
{p=i;strcpy (st,cs[i]);
for (j=i+1;j<5;j++)
if (strcmp (cs[j],st) <0) {p=j;strcpy (st,cs[j]);
if (p!=i)
{
strcpy (St,cs[i]);
strcpy (Cs[i],cs[p]);
strcpy (CS[P],ST);
}
Puts (cs[i]); printf ("\ n");
}

In the first for statement of this program, enter five country name strings with the gets function. It said that C language allows a two-dimensional array to be processed by multiple one-dimensional arrays, and this program describes CS[5][20] as a two-dimensional character array, which can be divided into five one-dimensional arrays cs[0],cs[1],cs[2],cs[3],cs[4]. Therefore, it is legal to use cs[i in the gets function. A For statement is nested in the second for statement to form a double loop. This double loop completes the work in alphabetical order. In the outer loop, the name string in the character array Cs[i] is copied to the array St, and subscript I is given p. After entering the inner cycle, the St and Cs[i] after the string for comparison, if there is a smaller than St the string to copy to St, and its subscript to give p. The inner loop is completed as P does not equal to I stating that there are smaller strings than cs[i] appearing, so swapping cs[i] and St content. The sorted value of the number-I element of the array CS has been determined thus far. The string is then output. Complete the sorting and output after the outer loop is complete.

Summary of this chapter

1. Arrays are the most commonly used data structures in programming. Arrays can be divided into numeric arrays (integers, real numbers), character arrays, and arrays of pointers, arrays of structures, and so on.

2. Arrays can be one-dimensional, two-dimensional, or multidimensional.

3. The array type description consists of a type descriptor, an array name, an array length (the number of array elements), and three parts. An array element is also called a subscript variable. The type of the array refers to the type of the subscript variable's value.

4. The assignment of arrays can be initialized with an array of values, input function dynamic assignment and assignment statement assignment three ways to implement. A value array cannot be assigned, entered, or exported as a whole, but must be manipulated with a circular statement.

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.