Chapter VII array experiments

C Programming lab report

Pilot project: an array experiments

Name: Wang Yuqing experiment Location: 514 Things laboratory experiments Time: May 29, 2019

A pilot project

  - 7.3.1一维数组的应用
  - 7.3.2二维数组的应用
  - 7.3.3字符数组的应用

1, experimental purposes, and requirements
a. Method definition master reference and array elements and multi-dimensional array.
b. understand the one-dimensional arrays and multidimensional array initialization method.
c. to learn the basic algorithms and multi-dimensional arrays.
d. reference method to master the array of characters defined initialization methods and elements.
E. grasp the basic function library of C language processing string provided.

Second, the experimental content

A lab exercises, bubble sort method to sort random numbers

1. Description of the problem: programming, using a random number function generator 10, the output sorted in ascending order.
2, the algorithm flow shown below:

3, experiment code:

#include "stdio.h"
#include "stdlib.h"
#include "time.h"
void sort1(int s[],int n)
{
    int i,j;
    int temp;
    for(i=0;i<n-1;i++)
    for(j=9;j>=i;j--)
        if(s[j]<s[j-1])
        {
            temp=s[j];
            s[j]=s[j-1];
            s[j-1]=temp;
        }
}
 main()
{
    int i,a[10];
    srand(time(NULL));//给出随机种子// 
    printf("随机产生10个整数:\n");
    for(i=0;i<10;i++)
    a[i]=rand()%100;//随机产生10个整数//
    for(i=0;i<10;i++)
    printf("%d ",a[i]);
    printf("\n");
    sort1(a,10);
    printf("排序后的结果:\n");
    for(i=0;i<10;i++)
    printf("%d ",a[i]);
}

4, Analysis
bubble sort are more exposed, with a random number srand (time (NULL)); a [i] = rand (% 100); rand ()% 100 data would allow smaller, less than 100 in the control.
An array, use a double loop. Array name is the first address of the storage array, so you can address the array name as the amount. So when you call the sort function arguments directly write the name of the array a. The cards for a long time, but in fact the class teacher has stressed that they still use.
operation result:

Laboratory exercises Second, select Sort to sort random integer

1, a brief description of the problem: the arrangement of 10 random integers in descending order of randomly generated.
2, as shown in the flowchart in FIG:

3, experiment code

#include "stdio.h"
#include "stdlib.h"
#include "time.h"
void sort2(int s[],int n)
{
    int i,j,k;
    int temp;
    for (i=0;i<n-1;i++)
    {
        k=i;
    for(j=i+1;j<=n-1;j++)
        {
        if(s[j]<s[k])
        k=j;      /*记录最小值*/
        }
    if(k!=i)
       {
        temp=s[k];/*因s[k]的值小于s[i],而k又大于i所以将二者的值进行交换*/
        s[k]=s[i];
        s[i]=temp;
       }
    }
}
main()
{
    int i,a[10];
    srand(time(NULL));//给出随机种子// 
    printf("随机产生10个整数:\n");
    for(i=0;i<10;i++)
    a[i]=rand()%100;//随机产生10个整数//
    for(i=0;i<10;i++)
    printf("%d ",a[i]);
    printf("\n");
    sort2(a,10);
    printf("排序后的结果:\n");
    for(i=0;i<10;i++)
    printf("%d ",a[i]);
}

4、问题分析
选择排序是先选定最小的,然后再逐个比较大小,它跟冒泡排序不一样,所以定义排序函数俩者也是有差异的。我在操作的时候,在实验室这个没有运行出来,我觉得没有错,可是出来的结果就是没有排序成功,就只是换了俩个数据,然后我拿别人的比较,还是没看出来,知道写实验报告我再试了试,才发现是我的循环中没有用{},所以出问题。所以说细节很重要, 程序不会报错,也很难找。
运行结果:

实验三、输出指定行数的杨辉三角

1、问题描述:编写程序,从键盘输入行数,输出指定行数的杨辉三角形。
2、算法流程图:

3、实验代码

#include "stdio.h"
 main()
{
    int a[50][50],i,j,n;
    printf("请输入杨辉三角形的行数:");
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        a[i][i]=1;
        a[i][1]=1;
    } 
        for(i=3;i<=n;i++)
        for(j=2;j<=i-1;j++)
        a[i][j]=a[i-1][j-1]+a[i-1][j];
        for(i=1;i<=n;i++)//输出杨辉三角// 
        {
            for(j=1;j<=i;j++)
            printf("%5d",a[i][j]);
            printf("\n");
        }
}

4、问题分析
我的理解是这个杨辉三角分俩地方循环,先是把所有的1输出,for(i=1;i<=n;i++){a[i][i]=1;a[i][1]=1;}这个地方就是把所有的1表示出来。for(i=3;i<=n;i++) for(j=2;j<=i-1;j++) a[i][j]=a[i-1][j-1]+a[i-1][j];这里就是算余下的值。当然,我这个题一开始就用了书上的i=1开始的,所以我这里a【1】【1】表示的是第一行第一列。
运行结果:

实验四:给定 年月日,将其转化为这一年的第几天,并输出。

1、问题描述:编写程序,从键盘分别输入年月日,计算该天是这年的第几天。
2、算法流程图:

3、实验代码

#include"stdio.h"
int day_tab[2][13]={
{0,31,28,31,30,31,30,31,31,30,31,30,31},
{0,31,29,31,30,31,30,31,31,30,31,30,31}};
int day_year(int y,int m,int d)//函数的定义// 
{
    int i,j,s=0;
    if(y%4==0&&y%100!=0||y%400==0)//判断是否为闰年// 
       i=1;
    else i=0;
    for(j=1;j<m;j++)
        s=s+day_tab[i][j];
        s=s+d;//加上该月的天数// 
        return s;
}
main()
{
    int y,m,d;
    printf("Input year_month_day:\n");
    scanf("%d%d%d",&y,&m,&d);
    day_year(y,m,d);
    printf("是这一年的第%d天\n",day_year(y,m,d));
}

4、问题分析
s=s+d;//加上该月的天数// 这里是我疏忽了的地方,我刚开始运行出来的,就是只有我指定天数前一个月的天数,就比如2005年12月12日;我少了这个,所以运行结果就少了12天。
运行结果:

实验五:判断字符串是否为回文数。

1、问题描述:编写程序,从键盘输入一个字符串,判断其是否为回文数。
2、算法流程图:

3、实验代码

#include "stdio.h"
#include "string.h"
#define N 40
main()
{
    char str[N],ch='Y';
    int i,len;
    printf("Input a string(请输入一个字符串): ");
    scanf("%s",&str);
    len=strlen(str);
    for(i=0;i<=len/2;i++)
    if(str[i]!=str[len-1-i])//判断前后是否相等,不相等则跳出循环// 
       {
          ch='N';
          break;
       }
    if(ch=='Y')
       printf("%s是回文数\n",str);
    else
       printf("%s不是回文数\n",str);
    return 0;
}

4、问题分析
这里还是细节的问题,
if(str[i]!=str[len-1-i])//判断前后是否相等,不相等则跳出循环//
{
ch='N';
break;
}
这里我一开始没有用{},所以无论我输入啥输出的都是 是回文数。所以还是细节。其次就是if(ch=='Y')这个问题也是出现过好多回了。==
运行结果:

三、实验小结

   数组实验编程题难度对我来说很大,我刚开始上手,一上午只写3个题,其中第二个还卡住了,之后我理清了一下思路,下午重新又写了一遍,比第一遍要顺畅很多,虽然第二个的我还是花了很久才看出问题所在,数组,一维数组二维数组都蛮难的,数组名是数组储存的首地址,所以可以把数组名看成地址量。所以在调用排序函数的时候,实参就直接写上数组名a。这个重点,要圈起来。然后就是细节,细节上一定要注意,==才是等于,=只是赋值,所以这个要特别注意,还有就是{}的使用!

Guess you like

Origin www.cnblogs.com/wangyuqing1126/p/10954229.html