实验5 数组和指针

实验五 数组和指针

§ 1.实验结论

1. 二分查找

实现方式1:形参是数组,实参是数组名,使用数组元素直接访问方式实现 程序源码文件ex1_1.cpp

以下是补全程序ex1_1.cpp之后的程序源码↓

// 练习:使用二分查找,在一组有序元素中查找数据项
//  形参是数组,实参是数组名
#include  <stdio.h>
const int N=5;
int binarySearch(int x[], int n, int item);
int main() {
    int a[N]={36,67,78,81,427};
    int i,index, key;
    
    printf("数组a中的数据:\n");
    for(i=0;i<N;i++)
        printf("%d ",a[i]);
    printf("\n");
    
    printf("输入待查找的数据项: ");
    scanf("%d", &key);
    
    // 调用函数binarySearch()在数组a中查找指定数据项item,并返回查找结果给index
    // 补足代码①
    
    index = binarySearch( a, N, key) ;
    
    if(index>=0)
        printf("%d在数组中,下标为%d\n", key, index);
    else
        printf("%d不在数组中\n", key);
    
    return 0;
}


//函数功能描述:
//使用二分查找算法在数组x中查找特定值item,数组x大小为n
// 如果找到,返回其下标
// 如果没找到,返回-1
int binarySearch(int x[], int n, int item) {
    int low, high, mid;
    
    low = 0;
    high = n-1;
    
    while(low <= high) {
        mid = (low+high)/2;
        
        if (item == x[mid])
            return mid;
        else if(item<x[mid])
            high = mid - 1;
        else
            low = mid + 1;
    }
    
    return -1;
}

运行结果截图↓

实现方式2:形参是指针变量,实参是数组名,使用指针变量间接访问方式实现 程序源码文件ex1_2.cpp

以下是补全程序ex1_2.cpp之后的程序源码↓

// 练习:使用二分查找,在一组有序元素中查找数据项
//  形参是指针变量,实参是数组名
#include  <stdio.h>
const int N=5;
int binarySearch(int *x, int n, int item);
int main() {
    int a[N]={20,19,5,27,84};
    int i,index, key;
    
    printf("数组a中的数据:\n");
    for(i=0;i<N;i++)
        printf("%d ",a[i]);
    printf("\n");
    
    printf("输入待查找的数据项: ");
    scanf("%d", &key);
    
    // 调用函数binarySearch()在数组a中查找指定数据项item,并返回查找结果
    // 补足代码①
    
    index = binarySearch( a,N,key) ;
    
    if(index>=0)
        printf("%d在数组中,下标为%d\n", key, index);
    else
        printf("%d不在数组中\n", key);
    
    return 0;
}

//函数功能描述:
//使用二分查找算法在x指向的数据项开始的n个数据中,查找item
// 如果找到,返回其位置
// 如果没找到,返回-1
int binarySearch(int *x, int n, int item) {
    int low, high, mid;
    
    low = 0;
    high = n-1;
    
    while(low <= high) {
        mid = (low+high)/2;
        
        if (item == *(x+mid))
            return mid;
        else if(item<*(x+mid))
            high = mid - 1;
        else
            low = mid + 1;
    }
    
    return -1;
}

运行结果截图↓ 

ο这里比较有趣的是当数组a中的数据包含0时,输入其他数据结果会不同(●°u°●)​ 」↓

2. 选择法排序

用选择法排序对一组数据由小到大排序。

补足程序源码文件ex2_2.cpp,使用选择法对字符串按字典序排序。

以下是补全程序ex2_2.cpp之后的程序源码↓

// 练习:使用选择法对字符串按字典序排序
#include <stdio.h>
#include <string.h>
void selectSort(char str[][20], int n ); // 函数声明,形参str是二维数组名
int main() {
    char name[][20] = {"Mark", "Elliot", "Zuckerberg", "Buffett", "Gates"};
    int i;
    
    printf("输出初始名单:\n");
    for(i=0; i<5; i++)
        printf("%s\n", name[i]);
    
    selectSort(name, 5);  // 调用选择法对name数组中的字符串排序
    
    printf("按字典序输出名单:\n");
    for(i=0; i<5; i++)
        printf("%s\n", name[i]);
    
    return 0;
}

// 函数定义
// 函数功能描述:使用选择法对二维数组str中的n个字符串按字典序排序
void selectSort(char str[][20], int n)
{
    // 补足代码
    int i, j, k;
    char temp[20];
    for(i=0;i<n-1;i++)
    {
        k=i;
        
        for(j=i+1;j<n;j++)
            if(str[j][0] < str[k][0])
                k=j;
        
        for(j=i+1;j<n;j++)
            strcpy(temp,str[i]);
        if(strcmp(str[j],str[k])<0)
            k=j;
        strcpy(temp,str[j]);
        
        if(k!=i)
        {
            strcpy(temp,str[i]);
            strcpy(str[i],str[k]);
            strcpy(str[k],temp);
        }
        
    }
}

 运行结果截图↓

§ 2.实验总结和体会

实验内容1:二分查找算法

♦ 数组名作为参数 vs. 指针变量作为参数,在形参、实参写法,以及函数实现中数组元素表示的差异

(以存放整形数据一维数组为例)

 (1)数组名:形参 int a[] ,实参 int a[i],数组元素 a[1]

 (2)指针变量:形参 int *p,实参 int a[i], *p,p = a,数组元素 *(a+1)、*(p+1)、p[1]

♦注意:修改数组元素值时,要确保数组元素值是有序的,这样才能使用二分查找算法

实验内容2:选择法排序

♦使用选择法对字符串排序时注意事项

选择排序是不断的记录下标,保留最小数或者最大数的下标,在里层循环结束后再进行交换,故需要注意里层和外层循环的临界条件不同 

实验内容3:使用指针变量对字符串进行处理

♦注意事项总结:

(1)字符串的比较和赋值,不能直接使用关系运算符和赋值运算符,要借助字符串处理函数。

    ¹字符串长度   size_t strlen(char *s); 求出s指向的字符串的长度

    ²字符串比较    char *strcmp(char *s1,char *s2); 比较s1指向的字符串和s2指向的字符串的大小关系。

    ³字符串复制    char *strcpy(char *s1,char *s2); 将s2指向的字符串复制到s1指向的数组中。

(2)指针对字符串复制操作时,关注结尾的“\0”是否已经复制,若没有则需要添加。

体会:

读懂一段程序明白他的意思和作用应该是最基本的了,只有在明白了整个大程序框架的思路,才可进行后来的改错填空和补充。

现在对程序填空和程序改错的这类感觉还可以,因为都是比较简短而且明确了位置整个程序的思路算法基本都是很明了的,

填空改错其实就是在考察对细节对临界条件这种易错点的考察,而补充一段程序就需要自己构建好思路算法,同时还要把握细节

但补充一段程序相较于构建整个程序而言是更容易的,所以c语言的学习任重而道远,每一个环节都需要认真对待。

§ 3.互评博客页面地址

https://www.cnblogs.com/yzxazj/p/10901305.html

https://www.cnblogs.com/plutocharon/p/10934443.html

https://www.cnblogs.com/ssyxs/

 ...结束 @ 'ェ' @ ...

猜你喜欢

转载自www.cnblogs.com/Anna0708/p/10934717.html
今日推荐