Array experiment

C language design lab report

Pilot project: an array experiments

Name: Huang Cheng experiment Location: 514 classroom experiment Time: May 29

1, experimental purposes and requirements

  • Consolidate students' understanding of this one-dimensional array data structure, enhanced programming capabilities.
  • How to define and use a two-dimensional array;
  • How to calculate the median circulating dimensional array
  • Using a cyclic sequentially outputs one-dimensional array of elements

    Second, the experimental content

    A brief description of the problem:

    7.3.1-1 a write function, a 10 integer random number generated by the rain in ascending order of mention sequence

    flow chart

    Experiment Code
#include <stdio.h>
#include "stdlib.h"
#include "time.h"
void sort1(int s[],int n)
{
    int j,i;

    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;//产生1~100的随机数
    }
    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]);
}
operation result

problem analysis

Here using bubble sort function and generates a random number, wherein i refers to the number of laps, j is the number of times required for each array. temp here are intermediate variables from memory effect, i.e. the number of values ​​switching two, the third application value.

2 brief description of the problem:

7.3.1-2, a write function, an integer of 10 random numbers generated by the rain in ascending order mentioned order (in ascending order, with the selected sorting method)

flow chart

Experiment Code
#include <stdio.h>
#include "stdlib.h"
#include "time.h"
void sort2(int s[],int n)
{
    int j,i,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((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");
           sort2(a,10);
    printf("排序后的结果\n");
    for(i=0;i<10;i++) 
    printf("%d ",a[i]);
}
operation result

problem analysis

Here is selection sort, select Sort principle of law is that every single out the minimum number, just in front of him. Similar to the bubble sort, is a maximum number of pick next row, a smallest number is picked forward rows.

A brief description of the problem 3

7.3.2-1, Pascal's triangle specified number of output lines.

flow chart

Experiment Code
#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("%2d ",a[i][j]);
        }
        printf("\n");
    }
}
operation result

problem analysis

The problem is Pascal's Triangle is solved, let the right-angled triangle or vertical edge and bevel are all filled out a. Therefore, the first row is a second row, the i is the beginning of the three, are as a hypotenuse, that j is less than or equal to i-1. The first few lines there are a few numbers thing. i is the row number, j is the column number. Then it is filled with. Pascal's Triangle is characterized by a number equal to his number plus the number just above his left oblique above, there is a number (a [i] [j]) is equal to him directly above (a [i-1] [j] ) plus obliquely upward (a [i-1] [j-1)

A brief description of the problem 4

7.3.2-2, given a certain period of a day, which was converted into the first few days of the year and output.

flow chart

Experiment Code
#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 sum(int a,int b,int c)
{   int s=0,i,j;
    if(a/4==0&&a/100!=0)
    i=1;
    i=0;
    for(j=1;j<b;j++){
        s=s+day_tab[i][j];
    }
    s=s+c;
    return s;   
}
main()
{   int a,b,c;
    printf("输入年月日\n");
    scanf("%d%d%d",&a,&b,&c);
    printf("是这年的第%d天\n",sum(a,b,c));
}
operation result

problem analysis

Two things to solve the problem 1, judgment is not a leap year if (a / 4 == 0 && a / 100! = 0), and adding all values ​​of the array

for(j=1;j<b;j++){
        s=s+day_tab[i][j];
    }

Is a leap year on the election of the group of the array, not, then do not choose that group. Incidentally, the result of judgment is only true (1) and false (0), the final judgment result is 0 and 1.

A brief description of the problem 5

7.3.3, for determining whether a string of characters is not "palindrome" palindrome is read from left to right or from right to left are the same string.

flow chart

Experiment Code
#include"stdio.h"
#include "string.h"
#define N 40
main()
{
    char str[N],ch='Y';
    int i=0;
    int len;
    printf("请输入一个字符串\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); 
    else 
    printf("%s不是回文",str); 
 } 
operation result

problem analysis

这个题目只有一个问题要解决,判断是不是回文,怎么判断呢,其实方法是让第一个与最后一个比较,第二个与倒数第二比较。一旦判断是错误,就结束循环。

实验小结

此次实验为数组实验,主要考了一维数组和二为数组,数组的好处在于可以将数据存储起来,并且实现调用。实验1描述的是数组内部的数据排序,要注意的问题为数组下标,二是冒泡排序和选择排序的理解。实验二杨辉三角讲的是二维数组,二维数组讲的是行和列的为题,二维数组是行和列的问题。如杨辉三角和闰年的那个实验。
回文实验说明,数组里不仅可以是数字也可以是字符。回文实验有一个标志位的用法,他只是一个判断的标志,如1为真,0为假。

Guess you like

Origin www.cnblogs.com/gudaonihao/p/10949925.html