c program test report

c program test report

Name: Xiongcheng Qi experiment Time: April 30 pilot projects:    1, write a function, to 10 integers generated by random function in ascending order of sort (bubble sort)    2, write a function of the random function 10 generated integers in ascending order of sorting (selection sort)    3, the output of the specified number of lines triangle



   4, given a certain day a year, converting it into the first few days of the year and output
   5, write a program to determine a character is not a "palindrome", from left to right or Palindrome read from right to left are the same character

 

1, experimental purposes and requirements

 

7.3.1.1. Write a function of 10 integers generated by a random function in ascending order (ascending order, to achieve 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. Write a function of 10 integers generated by a random function in ascending order (ascending order, realized by selection 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 the array
● the sort1 function into the main part of the function

 

7.3.2.1. Pascal triangle output of the specified number of rows

● 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 wrap)
● The conditional statement is used if a character is

 

7.3.2.2. Given a certain day a year, converting it 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.1. Write a program to determine a character is not a "palindrome" Palindrome from left to right or from right to left to read is the same character

 

● 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. exercises: 7.3.1.1

1. Problem Description:

Programming, using a random function generating 10 random numbers in ascending order output. (Bubble Sort)

2. A flowchart

3. Experiment Code

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void sort1(int s[],int n)
{
    int i,j,k;
    int 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((unsigned int)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");
    sort1(a,10);
    printf("输出排序后的10个整数\n");
    for(i=0;i<10;i++)
    printf("%d ",a[i]);
}

  4.运行效果

5.收获感悟

1.随机数种子是一个srand((unsigned int)time(NULL))函数,生成随机数需要用到rand()函数才能随机生成数。

2.运行的结果出现很大的数时可以用%100限定(保证随机取数在100以内)。

6.问题发散

能否设置一个检测是否含有相同随机数的回合,若产生相同数则重新选则随机数,这个设想我认为可以用冒泡排序实现设置一个if函数进行判断。

 

2.实验练习:7.3.1.2

 

1.问题描述:

 

编写函数,利用随机函数产生10个随机数,按升序排列输出.(选择排序)

 

2.流程图

3.实验代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
sort2(int s[],int n)
{
    int i,j,k,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.运行效果

5.收获感悟

1.就是用for函数来进行排序,找出最小或者最大的放在下标为0的这个位置;第二次从下标为1的开始比较;查询剩下的最大或者最小值;放在下标为1的位置;以此类推。

2.选则排序法之所以要分成内循环的排序和外循环的排序这两个排序,则是外循环之后找到极值然后内循环将各个数与极值进行比较。

3.实验练习:7.3.2.1

1.问题描述:

编写程序,从键盘输入行数,输出指定行数的杨辉三角形。

2.流程图

 

3.实验代码

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

4.运行效果

 

5.收获感悟

1.输出结果时,加入%4d,%4d指的是以宽度为4输出整数,而这样就可以分开更好观察

若不分开则

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 year,int month,int day)
{
	int i,j,s=0;
	if(year%4==0&&year%100!=0||year%400==0)
	i=1;
	else
	i=0;
	for(j=1;j<month;j++)
	s=s+day_tab[i][j];
	s=s+day;
	return s;
}
main()
{
	int y,m,d;
	printf("输入年,月,日:");
	scanf("%d,%d,%d",&y,&m,&d);
	printf("是这年的第%d天\n",day_year(y,m,d)); 
}

  

 

4.运行效果

5.收获感悟

1.先判断年份是否为闰年,判断的方法:被四整除但不被一百整除或者被四百整除

代码表示就是:year%4==0&&year%100!=0||year%400==0

2.scanf("%d,%d,%d",&y,&m,&d); scanf("%d%d%d",&y,&m,&d);

两种表达方式区别是:前者输入逗号,后者不能输入逗号,采用打空格代替逗号。

5.实验练习:7.3.3.1

1.问题描述:

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

2.流程图

 

 

3.实验代码

#include<stdio.h>
#include<string.h>
#define N 40
main()
{
    char str[N],ch='Y';
    int i;
    int len;
    printf("input a string:\n");
    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);
}

  

4.运行效果

5.收获感悟

1.回文数的定义:回文数从左至右和从右至左一致

所以判断前一半与后一半对应数是否一致 ,所以用i<=len/2 来判断前后是否相等。

三、实验小结

1.随机数种子是一个srand((unsigned int)time(NULL))函数,生成随机数需要用到rand()函数才能随机生成数

2.rand()后面加%100指的是获得一个100以内的随机数,范围在0-99之间,若为%100+1则是指区间左右都+1,范围在1-100

3.选择排序:选则排序法之所以要分成内循环的排序和外循环的排序这两个排序,则是外循环之后找到极值然后内循环将各个数与极值进行比较

4.%md指的是以宽度m输出整数是%d的变种,数据宽度不足m时,左补空格,故此在辉三角一题中数字排版就不会拥挤

5.判断闰年的方式:(y%4==0&&!y%100==0)||(y%400==0)

6.scanf("%d,%d,%d",&y,&m,&d); scanf("%d%d%d",&y,&m,&d);两种表达方式区别是:前者输入逗号,后者不能输入逗号,采用打空格代替逗号

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/xiongchengqi/p/10958243.html