实验4 数组

数组名作为函数参数时 形参时要加【】,实参时直接写数组名。函数调用scan(“%d”,&x【i】);使用数组的元素x【i】的地址作为scanf函数的参数。当i=2时,数组元素x【2】的地址传递到scanf函数中,通过scanf函数将输入设备上获取到数据存入x【2】中。在调用函数int时,函数的参数由实参数组名、数组的长度和存储到数组中的值组成。在形参数组时,如果不想在函数中改变实参数组的值,可以使用const关键字修饰形参数组。

#include <stdio.h>
int findMax(int a[], int n); 
const int N=5;
int main() {
    int a[N];
    int max, i;
    printf("输入%d个整数: \n", N);
    for(i=0;i<5;i++)
    scanf("%d",&a[i]);
    max=findMax(a,N);
    printf("数组a中最大元素值为: %d\n\n", max); 
 return 0;
}


int findMax(int a[],int n){
int i,max;
max=a[0];
for (i=0;i<n;i++){
if (a[i]>max)
max=a[i];
}
return max
}

 

#include <stdio.h>
const int N=4;
void output(char x[], int n); 
void bubbleSort(char x[],int n);
int main() 
{
    char string[N] = {'2','0','1','9'};
    int i;
    printf("排序前: \n");
    output(string, N);
    bubbleSort(string,N);
    printf("\n排序后: \n");
    output(string, N);
    printf("\n");
    return 0;
}
void output(char x[], int n) 
{
    int i;
    for(i=0; i<N; i++)
    printf("%c", x[i]); 
}
void bubbleSort(char x[],int n)
{
    int i,j;
    char t;
    for(i=0;i<n-1;i++)
    {
    for(j=0;j<n-1-i;j++)
    {
    if(x[j]<x[j+1])
    {
    t=x[j];
    x[j]=x[j+1];
    x[j+1]=t;
    }
     }
      } 
 }

 

posted @ 2019-04-25 23:06  Soledad·QX  阅读(119)  评论(1编辑  收藏  举报