Detailed explanation of typical application codes for arrays in C language - expert mistakes (step-by-step code explanation)

Foreword:

       This article is a self-summarized learning code in the process of self-learning C language. It is explained step by step. It is suitable for zero-based programming learning and basic consolidation of memories. It is not suitable for masters to learn. The purpose of posting this is also to exchange the experience of learning C language. Everyone speaks C language. It is the basic language of all languages. It covers everything. I believe that countless experts have proven this topic before. C language is the cornerstone of all programming languages. When learning programming, I will not be stingy about staying up late to summarize the knowledge points. I hope it can help you who are reading.

Table of contents

Foreword:

1. One-dimensional array definition:

1.1Basic code case:

2. Detailed code explanation case:

2.1 Detailed code case + description

2.2 Typical case bubble sort detailed code + explanation

 2.3 Typical application code explanation—recursion:

2.4 Typical application code + detailed explanation:

3. Notes:


1. One-dimensional array definition:

To use an array in C language, it must be defined first.

A one-dimensional array is defined as: type specifier array name [constant expression].

Among them, the type specifier is any basic data type or constructed data type. Array name is a user-defined array identifier.

The constant expression in square brackets represents the number of data elements, also known as the length of the array.

1.1Basic code case:

int a[10]; /* 说明整型数组a,有10个元素 */

float b[10], c[20]; /* 说明实型数组b,有10个元素,实型数组c,有20个元素 */

char ch[20]; /* 说明字符数组ch,有20个元素 */

2. Detailed code explanation case:

#include <stdio.h>
#include <stdlib.h>
/*数组:
   1.数组具有相同的数据类型,且地址是连续的——为了存储需要
   2.数组是有序的——方便我们去使用(存储是按循序存储的)
   3.数组元素下标发个数叫做维度。根据维度不同,可以分为一维数组、二维数组、多维数组
   []有几个中括号就有几个维度

   定义:
   一维数组
   1)
   定义:类型符 数组名[常量表达式]
   类型符 — 指定数组存放什么样的数据
   数组名 — 可类似理解为变量名
   常量表达式 — 指定数组元素的个数
   2)数组名和变量名类似,都需要遵循标识符规则
   定义数组时我们需要指定数组中元素的个数,也就是方括号内需要有数字
   使用数组时可以是变量,也可以是常量
   数组中的元素可以类似于变量使用
   使用数组是不要越界(数组下标和常量相等,有多少个元素就有多少个数,下标从0开始)

*/
//定义整型一位数组,数组元素的下标从0开始
/*
  问题:int a[10],a[0]—a[9]依次赋值0-9,这个方式如何实现?
  int main()
{
int a[10];
int i;
for(i=0;i<10;i++){
    a[i] = i;
printf(" %d\n",a[i]);
}
for(i=9;i>=0;i--){
    a[i] = i;
  printf(" %d\n",a[i]);
}
    return 0;
}


*/
/*
  1)定义一维数组的同时进行赋值,叫数组初始化
  2)全局数组如果不初始化,系统默认认为对其赋值0
  3)对全部数组元素赋初值时,可以不指定数组长度
  4)局部初始化,前5个元素赋初值,后面的元素有系统默认给赋值为0


一维数组的初始化:
   初始化:即在定义的同时赋初值  如: int a=[10]={1,2,3,4,5,6,7,8,9,10}
*/
//1.数组全部初始化(全局初始化)

int main(){
   int a[10]= {1,2,3,4,5,6,7,8,9,10};
   int i;
   for(i=0;i<10;i++){
    printf("%d \n",a[i]);
   }

}//请多多指正,谢谢!

 

2.1 Detailed code case + description

#include <stdio.h>
#include <stdlib.h>

int main()
{
  /*
  //问题:
  1)给定一个一维数组,求数组中的最大值。
  //解决方式:“打擂台”
  比武招亲:1人站上擂台——其余上进行比试,


  2)实现数组的翻转
  1 2 3 4 5 6 7
  7 6 5 4 3 2 1


   3)如何实现数组的排序
   int a[6] {5,7,9,10,3,1} 1,3,5,7,9,10


  */
      /*
    //1.定义一维数组,同时需要初始化
  int a[10]={7,4,9,8,5,34,28,45,56,97};
  //2.确定擂主,给定初始最大值
  int max=a[0];
  //3.循环遍历,依次比较每个元素的值,
//如果元素值比max的值大,则max的值更新为这个数,否则不改变。
  int i;
  for(i=0;i<10;i++)
  {
      if(a[i]>max){
        max=a[i];
      }

  }
     printf("最大值为:%d\n",max);
  */

    //例二:易错方法:运用逆序输出(不可取):
    /*
    int a[6] = {1,2,3,4,5,6};
    //数组注意事项:a[]中括号里面表示有几个元素,
    //后面需要进行值定义,否则随即赋值,从“0”开始算第一个数
    int i;
    for(i=5;i>=0;i--){
        printf("%d ",a[i]);//输出:6 5 4 3 2 1

    }
    printf("\n");
    for(i=0;i<6;i++)
    {
        printf("%d ",a[i]);//输出:1 2 3 4 5 6
    }
    //此种方法只是改变了输出值的顺序,并没有实现1到6数值间值的翻转

    */

    //例二正确解决方法:数组的翻转可以通过元素的交换实现
    //1和6换,2和5换,3和4换,
    //1 2 3 4 5 6
    //6 5 4 3 2 1
    //a[0]和a[n-1]换 a

    /*
  sizeof() 是一种内存容量度量函数,功能是返回一个变量或者类型的大小(以字节为单位);
 在C语言中,sizeof()是一个判断数据类型或者表达式长度的运算符。
    */



    /*
    int a[6]= {1,2,3,4,5,6};
    int i=0;
    int tmp;//利用一个空闲变量来进行交换
    int j=sizeof(a)/sizeof(int)-1;//计算机中元素从0开始数,0是第一个元素
    while(i<j){


         tmp = a[i];
         a[i] = a[j];
         a[j] = tmp;
         i++;
         j--;
    }
    for(i=0;i<6;i++)
    {
        printf("%d ",a[i]);
    }
   printf("\n");
   */

   /*
   //3)如何实现数组的排序
   int a[6] {5,7,9,10,3,1} 1,3,5,7,9,10


   //采用冒泡排序
   */
   int a[6] =  {5,7,9,10,3,1};

   /*
   //冒泡排序流程,多趟比较大小
   int a[6] =  {5,7,9,10,3,1};
   1   5=n-1   5 7 9 3 1 10
   2   4=n-2   5 7 3 1 9 10
   3   3=n-3   5 3 1 7 9 10
   4   2=n-2   3 1 5 7 9 10
   5   5=n-1   1 3 5 7 9 10

   */


   int i;//排序趟数
   int j;//比较相邻两个数的值大小
   int tmp;//定义一个交换变量用于临时交换
   int n = sizeof(a)/sizeof(int);
   for(i=0;i<n;i++){
    for(j=0;j<n-1-i;j++)
    //元素的交换(前提是相邻的两个数,前者比后者大)
        {
        if(a[j]>a[j+1]){
            tmp = a[j];
            a[j] = a[j+1];
            a[j+1] = tmp;
        }
    }
   }
//输出结果
for(i=0;i<n;i++)
{
    printf("%d ",a[i]);
}
    return 0;
}
//如有不对请指正,谢谢。

2.2 Typical case bubble sort detailed code + explanation

#include<stdio.h>
void main()
{
 int n[10] = { 25,35,68,79,21,13,98,7,16,62 };//定义一个大小为10的数组
 int i, j,k,temp;
 for (i = 1; i <= 9; i++)//外层循环是比较的轮数,数组内有10个数,那么就应该比较10-1=9轮
 {
  for (j = 0; j <= 9 - i; j++)//内层循环比较的是当前一轮的比较次数,例如:第一轮比较9-1=8次,第二轮比较9-2=7次
  {
   if (n[j] > n[j + 1])//相邻两个数如果逆序,则交换位置
   {
    temp = n[j];
    n[j] = n[j + 1];
    n[j + 1] = temp;
   }
  }
  printf("第%d趟排序完成后的数据排序:\n",i);
  for (k = 0;k < 10; k++)
   printf("%-4d", n[i]);
  printf("\n");
 }
 printf("排序过后的数顺序:\n");
 for (i = 0; i < 10; i++)
  printf("%-4d", n[i]);
 printf("\n");
}
//请多多指正

 2.3 Typical application code explanation—recursion:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void PrintN(int N)
{
 if (N)
 {
 PrintN(N-1);
 printf("%d\n",N);
 }
 return;
}

void main()
{
 int N;
 clock_t start,finish;
 double duration;
 printf("please input N:\n");
 scanf("%d",&N);
 start = clock();
 PrintN(N);
 finish = clock();
 duration = (double)(finish - start) / CLOCKS_PER_SEC;
 printf("%f seconds\n",duration );
 return;
}
//请多多指正

2.4 Typical application code + detailed explanation:

#include <stdio.h>


#include <stdlib.h>
/*
   一维数组初始化
   初始化:就是在定义的同时赋初值,如
   int a[10] = {1,2,3,4,5,6,7,8,9,10}
   如果不赋值就会自动赋值

   一维数组的数组名
   1.数组名时常量
   2.数组名是数组元素的首地址
   3.sizeof(数组名)实际上计算的是数组的总大小

问题:
    1)给定一个数组,求数组的最大值
    解决方法:给定一个初值,比较大小
    定义:i 和 max的来进行比较

  int a[10] = {10,20,90,31,32,44,55,66,10,23};
    int max = a[0];
    int i;
    for(i=0;i<10;i++){
        if(a[i]>max){
        max = a[i];
    }
    }
    printf("数组中较大数为: %d\n",max);


    问题2 :实现数组翻转、
    利用交换的方式实现数组内的元素位置交换,数组翻转。

*/


int main()
{
int a[10] = {1,2,3,4,5,6,7,8,9,10};
int i=0;
int tmp;
int j = sizeof(a)/sizeof(int)-1;
while (i<j){
    tmp  = a[i];
    a[i] = a[j];
    a[j] = tmp;
    i++;
    j--;
}
for(i=0;i<10;i++){
    printf("%d ",a[i]);
}



    return 0;
}

If you like the author's article after reading it, please leave a comment and we will continue to update it.

A refined programming development learning companion.

3. Notes:

The following points should be noted when describing array types:

  • The type of an array actually refers to the value type of the array elements.
  • For the same array, the data type of all its elements is the same.
  • The writing rules for array names should conform to the writing rules for identifiers.
  • The array name cannot be the same as other variable names.

C language learning starts with arrays and gradually becomes more difficult. Good programmers = talent + hard practice

Less passionate pursuit and more down-to-earth persistence.

Guess you like

Origin blog.csdn.net/m0_48565215/article/details/125995669