One-dimensional array example

Assign values ​​0-9 to the 10 array elements in turn, requiring the reverse output
. The idea of ​​the problem: First define an array of length 10. The integer type must have a regular pattern to cycle these numbers. Also use this loop to output these numbers. When outputting, output first The last element outputs 10 numbers in ascending order.

Programming

#include <stdio.h>
int main()
{
  int i,a[10];
  for(i=0;i<=9;i++)        //使a[0]-a[9]的值为 0 —9
    a[i]=i;
  for(i=9;i>=0;i--)          //使a[9]-a[0]的值为 9 —0
   printf("%2d",a[i]);
  printf("\n");
  return 0;
}

One-dimensional array of running results
Insert picture description here
, enter ten numbers, sort from small to large 1

#include <stdio.h>
int main()
{
    int a[10];
    int i,j,t;
    printf("input 10 numbers :\n");
    for(i=0;i<10;i++)
        scanf("%d",&a[i]);
    printf("\n");
     for(j=0;j<9;j++)                      //每进行9次循环,实现9次比较 
        for(i=0;i<9-j;i++)                 //每一堂进行9-j次比较 
          {
     	    if(a[i]>a[i+1])                //相邻的两个数进行比较 
             {
               t=a[i];a[i]=a[i+1];a[i+1]=t;
             }
		  } 
            printf("the sorted number :\n");
      for(i=0;i<10;i++)
        printf("%4d",a[i]);
    printf("\n");
    return 0;
}

operation result
Insert picture description here

Published 10 original articles · won 12 · visited 1858

Guess you like

Origin blog.csdn.net/qq_44236958/article/details/89117141