Chapter VII Array Experiments

Posted by c815902 on Sat, 01 Jun 2019 20:01:02 +0200

C Program Experiment Report

Experiments:

1. Application of one-dimensional arrays

2. Application of two-dimensional arrays

3. Application of Character Array

Name: Chen Fuzhou Experimental Place: 514 Classroom of Teaching Building Experimental Time: 2019.5.29

1. Experimental Purpose and Requirements

1. Master the definition of one-dimensional and multidimensional arrays and how to reference array elements.

2. Understand one-dimensional and multidimensional array initialization methods.

3. Learn basic algorithms for one-dimensional and multidimensional arrays.

4. Master the definition of arrays, initialization methods, and reference methods of elements.

5. Master the basic function library provided by the c language for string processing.

 

2. Experimental Contents

Applications of one-dimensional arrays (7.3.1)

Experiment Exercise 1: (1) A simple description of the problem: Write a program that uses 10 numbers generated by a random function to sort the output in ascending order.

Flow chart:

Experimental 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+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("Randomly generate 10 integers:\n");
    for(i=0;i<10;i++)
        a[i]=rand()%100;
    for(i=0;i<10;i++)
        {
        printf("%d",a[i]);
        printf(" ");
        }
        printf("\n");
        sort1(a ,10);
        printf("Sorted results:\n");
    for(i=0;i<10;i++)
        {
        printf("%d",a[i]);
        printf(" ");
        }
}

 

Running result;

Problem analysis: First sorting function has indeed seen more in the learning process, i control the number of comparisons, j control the number of arrays, each time compared with other arrays, internal and external loop.

Then inside the main function, it refers to the application of the "srand()" function. First, it is declared, then random selection of columns with "rand()", which is a common problem encountered in compilation.However, I see some good output formats that add a "" space to each printf() output, which makes the results look neat.Notice that the result of the final run is still a for loop output from the first one in turn.(

2.(7.3.1.2)

1. Description of the problem:

Write a function that produces 10 random numbers in ascending order using a random function. (Select Sort)

2. Flowchart:

Experimental 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++)
    {
        for(j=i+1;j<=n-1;j++)
        {
        if(s[j]<s[i])
            {
            temp=s[i];
            s[i]=s[j];
            s[j]=temp;
            }
        }
    }
}
main()
{
    int i,a[10];
    srand(time(NULL));
    printf("Randomly generate 10 integers:\n");
    for(i=0;i<10;i++)
    a[i]=rand( )%100;
    for(i=0;i<10;i++)
    {
    printf("%d",a[i]);
    printf(" ");
    }
    printf("\n");
    sort2(a ,10);
    printf("Sorted results:\n");
    for(i=0;i<10;i++)
    {
    printf("%d",a[i]);
    printf(" ");
    }
}

Run result:

Problem analysis: The idea is basically the same as the above question.

Use of two-dimensional arrays (7.3.2)

Experiment Exercise 1: (1) Simple description of the problem: Write a program to input the number of lines from the keyboard and output Yang Hui triangle of the specified function.

Flow chart:

Experimental code:

#include<stdio.h>
main()
{
    int a[50][50],i,j,n;
    printf("Please enter the number of rows of Yang Hui triangle:");
    scanf("%d",&n);
    for(i=0;i<=n;i++)
    {
        a[i][i]=1;
        a[i][0]=1;
    }
    for(i=2;i<n;i++)
    {
      for(j=1;j<=i-1;j++)
       {
         a[i][j]=a[i-1][j-1]+a[i-1][j];
       }
    }
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=i;j++)
        {
            printf(" ");
            printf("%d",a[i][j]);    
        }
        printf("\n");

    }
}

Run result:

Problem Analysis: Because this is a two-dimensional array, i, j represent the rows and columns of the two-dimensional array, respectively.And Yang Hui Triangle is 1 in the first column and diagonal line, so there is a[i][i],a[i][0]=0, and then there is its core part: from the second line each number equals the sum of two numbers above it.The output is also a for loop, since it is a two-dimensional array with internal and external loops.That's the general idea, but I still encounter a variety of details when I operate, such as forgetting to use the for loop when I output.

Experiment Exercise 2: (1) Simple description of the problem: Write a program, enter the year, month and day from the keyboard, and calculate the day is the day of the year.

Flow chart:

 

Experimentation 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 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];
        return s+day;        
    }
main()
{
    int y,m,d;
    printf("Input year_month_day:\n");
    scanf("%d%d%d",&y,&m,&d);
    day_year(y,m,d);
    printf("This year%d day\n",day_year(y,m,d));
}

Run result:

Problem Analysis:

This two-dimensional array has been given an argument to add up the number of days of a month and day of that year for leap years.In this case,'i'controls whether the year is a leap year, but it cannot be used to redeem it when determining whether it is a leap year.Note that the function header cannot be the same as the array name, then run the for loop to define the number of days in all months before the month and return the value to "s" for each calculated month.

Character Array Application (7.3.3)

(1) A simple description of the problem: Write a program to enter a string from the keyboard to determine if it is palindrome number.

Flow chart:

Experimental code:

#include "stdio.h"
#include "string.h"
#define N 40
main()
{
    char str[N],ch='Y';
    int i;
    int len;
    printf("Please input a strings:");
    scanf("%s",&str);
    len=strlen(str);
    printf("\n");
    printf("String length is%d",len);
    printf("\n");
    for(i=0;i<=len/2;i++)
    {
        if(str[i]!=str[len-1-i])
        {
            ch='N';
            break;
        }
    }
        if(ch=='Y')
            printf("%s Is a palindrome number\n",str);
        else
        printf("The string is not palindrome"); 
}

Run result:

Problem Analysis: The difference here is the number of cycles it takes, len/2.Although it is still correct to halve its constraints in compilation, the idea is to understand.During the compilation process, I didn't expect to break out of the loop, maybe it's too long to be familiar with.

 

Summary: Through practice with arrays, you find that your compilation is significantly weaker than before, because for loops are nested frequently here, and branch structures are often used as well.I think it's important to understand what each parameter represents and the limitations can be varied, based on a deep understanding of the meaning of the for loop parameter. Flowcharts are a tool for linking ideas, but in the future they will gradually get rid of the situation where there is no clue without flowcharts.

 

 

 

 

Topics: Delphi C