Chapter VII of the Experimental Report

C Programming lab report

Lab Exercise:

  7.3.1.1, a write function, an integer of 10 random function generated in ascending order (ascending order, implemented using bubble sort)

  7.3.1.2, a write function of an integer randomly generated 10 in ascending order (ascending order, to achieve with the selected sorting method)

  7.3.2.1, output specified number of lines of Pascal's Triangle

  7.3.2.2, given a certain day a year, it is loaded into the first few days of the year and output

  7.3.3., Writing programs, determine a string is not "palindrome" Palindrome from left to right or right to left to read is the same string

Name: Zhong Junmin experiment Location: experimental classroom teaching building 514 Time: May 29

7.3.1.1, a write function, an integer of 10 random function generated in ascending order (ascending order, implemented using bubble sort)

● definition of a one-dimensional array of integers of size 10, i.e. it can store 10 data
● loop used to produce the array 10 utilized in the integers stored random function.
● write ordering function SORT1 ()
● using the number of loop 10, the sorted sequentially outputs
● array defined as a global or local array array
● in the main function, the element 10 generates a random number function into an array
● the sort (1) into the function part main function

7.3.1.2, a write function of an integer randomly generated 10 in ascending order (ascending order, to achieve with the selected sorting method)

● definition of a one-dimensional array of integers of size 10, i.e. it can store 10 data
● loop used to produce the array 10 utilized in the integers stored random function.
● write ordering function SORT1 ()
● using the number of loop 10, the sorted sequentially outputs
● array defined as a global or local array array
● in the main function, the element 10 generates a random number function into an array
● the sort (1) into the function part main function

7.3.2.1, output specified number of lines of Pascal's Triangle

● how to define and use a two-dimensional array
● how to calculate the value of two-dimensional array with a circulating
● using cyclic sequentially outputs the two-dimensional array of elements (note line feed)
● The if statement determines whether a condition with an uppercase character

7.3.2.2, given a certain day a year, it is loaded into the first few days of the year and output

● Use scanf () function Enter the year, month, day
● use a two-dimensional array of storage leap years and non-leap years the number of days per month
● use logical expressions to determine whether a given year is a leap year
● use a variable day for the first few months of the number of days accumulated
● several days strengthening exercises, students will practice without the use of a two-dimensional array to store each month
● use an if statement for the year, month, day to verify the correctness of the
two-dimensional array to store the number of days in each month ● Description for the local array

7.3.3, writing programs, determine a string is not "palindrome" Palindrome from left to right or right to left to read is the same string

● using scanf () function, a character string stored in the input from the keyboard character array
● determined length of the string
● sequentially for comparison loop, the final value of half cycle length
● setting a flag ch, the initial value is 'Y', if not equal to a character, it is set to 'N'
● the ch is 'Y' or 'N', the output whether the string is a palindrome
● strengthening exercise is not used strlen () request the length of the string
● the required number of palindromic independent part, a function compiled

Second, the experimental content

1, laboratory exercises (7.3.1.1):

1. brief description of the problem:

Programming, using a random function generating 10 random numbers in ascending order output.

2. The flow chart is as follows:

 

3. experiment code:

#include"stdio.h"
#include"stdlib.h"
#include"time.h"
void sort1(int s[],int n)
{
    int i,j,temp;
for(i=0;i<n-1;i++)
        {
            for(j=9;j>=i+1;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;
    for(i=0;i<10;i++)
       printf("%d  ", A [I]); 
       the printf ( " \ n- " ); 
    SORT1 (A, 10 ); 
       the printf ( " results sorted: \ n- " );
     for (I = 0 ; I < 10 ; I ++ ) 
       the printf ( " D%   " , A [I]);     
}

 

3. Analysis:

a, major issues: First, the main problem with this program is to call a function of the problem.

      Solution: a core algorithm is the bubble sort program, which is a position transducer and the comparison between the numbers to be sorted by selecting two numbers, and the value exchange between the two numbers is the introduction of a third variable, the algorithm It is probably the case.

There is defined for the parameters, such as parameter int s [], at the back when I should be used with the A [], instead of s, otherwise this 10 number is not arranged.

b, the program runs as follows:

 

2, laboratory exercises (7.3.1.2):

1. brief description of the problem:

Write function, using the random function generating 10 random numbers in ascending order output.

2. The process flow chart:

 

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];
                         s[i]=temp;
                         }
        }
}
main()
{
    int i,a[10];
    srand(time(NULL));
    printf("随机产生10个整数:\n");
    for(i=0;i<10;i++)
       a[i]=rand()%100;
    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.问题分析:

a、这个程序中的main函数和上面的那个程序中的一样,主要是它的排序方法变了,使用的是选择排序法,主要按照流程图来,并不是很难。

b、程序运行如下:

3、实验练习(7.3.2.1):

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");
            }
            printf("\n");
    
}

3.问题分析:

a、主要的问题:怎么样将数字能够按照书上这么排列

      解决方法:该程序用了两个嵌套的for循环,其中的第一个和最后一个是输入和输出数组,而其中最核心的算就是 a[i][j]=a[i-1][j-1]+a[i-1][j],其意义就是下一项目是上面两项的和,让后在用换行就行

b、程序运行如下:

4、实验练习(7.3.2.2):

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;
        printf("%d",i);
        for(j=1;j<m;j++)
           {
               s=s+day_tab[i][j];
           }
           s=s+d;
           return s;
    }
    main()
    {
        int y,m,d,n;
        printf("input year_month_day:\n");
        scanf("%d%d%d",&y,&m,&d);
        n=day_year(y,m,d);
        printf("是这年的第%d天\n",n);
    }

 

4.问题分析:

a、这个程序是但是问题最大的一个题目了,首先是不知道书上提示的统计函数的头部怎么编写,这个问题是通过看书上的内容自行解决,也就是最year,month,day进行整形就行,应为这几个变量都只能是整数。第二个问题就是运行的结果一直都是347天,而正确的答案是346天,这个问题是通过老师的帮助完成的,老师让我输出一下 i 的值,结果 i 的值一直都是1,而其应该是0,所以这才是错误的根源。而我这个程序的最大的问题就是搞清楚if中的判断条件,不应该用i,而是用year,通过year来判断 i 的取值。

b、程序运行如下:

5、实验练习(7.3.3):

1.问题的简单描述:

编写 程序,从键盘输入一个字符串,判断其是否为回文数。

2.程序流程图:

 

 

3.实验代码:

#include"stdio.h"
#include"string.h"
#define n 40
main()
{
    char str[n],ch='y',s;
    int i;
    int len;
    printf("请输入一段字符:");
    scanf("%s",str);
    len=strlen(str);
    for(i=0;i<=len/2;i++)
    {
        if(str[i]!=str[len-i-1])
        {
            ch='n';
            break;
        } 
    }
    
        if(ch=='y')
        printf("%s是一个回文数\n",str);
        else
        printf("该字符串不是回文数");                       
}

4.问题分析:

a、这个程序最大的问题就是理解这个算法,以及我一直出错的地方,if中的判断语句。其实很多时候都是应为这种小的问题,而导致整个程序无法运行。回文数从左至右和从右至左一致,所以只需判断前一半与后一半对应数是否一致,所以无需判断所有的字符。

b、程序运行如下:

 

三、实验小结

这次实验课总共完成了5个实验,我觉得这次的几个实验都比较有难度。这是我在这次实验课上的收获:1、#include "stdio.h",这种形式用于引用用户头文件。它在包含当前文件的目录中搜索名为 stdio 的文件,而其就是一个输入和输出函数。2、选择排序法和冒泡排序法的异同点,相同点:¹都是通过n-1组排出具有n个数的顺序  ²都是通过逐个比较,调整位置。不同点:冒泡排序法是最值在其中和其他值交换。选择排序法确是假定一个最值,所以最值和其他值得交换就发生在假定最值得地方。3、rand()函数的作用是随机产生数,而其最大的缺陷就是会产生重复的随机数,在rand函数后面加上%100的意思就是100以内的随机数。4、%md指的是以宽度m输出整数,数据宽度不足m时,左补空格。杨辉三角之间的空格就可以用%5d来实现。

 

Guess you like

Origin www.cnblogs.com/zjm956/p/10948381.html