Seventh test report
experimental project:
1. Application of one-dimensional array of
application 2. The two-dimensional array of
application 3. The array of characters
Name: Yuan Cheng experiment Venue: Teaching Building 514 classroom experiment Time: May 16
1, experimental purposes and requirements
1. one-dimensional array of applications
Define a one-dimensional array of integers, and its size is 10, i.e., it can store 10 data;
Use loop, generating an array of 10 integers stored in the random function;
· SORT1 write ordering function ();
Use loop , the number of sorted 10 sequentially outputs.
2. Application of two-dimensional array
* How to define and use a two-dimensional array;
what * is calculated using a two-dimensional array of values in circulation;
Use cycle sequentially output two-dimensional array of elements;
· determine if statements in accordance with the conditions of a character is a capital letter.
3. Application of the array of characters
· Using scanf () function, a character string stored in the input from the keyboard character array ;;
-determined length of the string;
* comparative sequence for loop, the loop half of the length of the final value;
· a set identifier ch, the initial value for '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.
Dian two experimental content
7.3.1 Lab Exercises
1. Problem Description
A: programming, using a random function to generate an integer of 10, in ascending order output.II: Write a function of an integer randomly generated 10 in ascending order of sorting (selection sorting method implemented).
2. Experiment Code
A flow chart:
#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];
printf("随机产生10个整数:\n");
srand((unsigned) time(NULL));
for(i=0;i<10;i++)
{
a[i]=rand()%100;
printf("%d ",a[i]);
}
sort1(a,10);
printf("输出排序后的结果:\n");
for(i=0;i<10;i++)
{
printf("%d ",a[i]);
}
}
Results Figure:
Two Flowchart:
#include "stdio.h"
#include "stdlib.h"
#include "time.h"
void 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];
printf("随机产生10个整数:\n");
srand((unsigned) time(NULL));
for(i=0;i<10;i++)
{
a[i]=rand()%100;
printf("%d ",a[i]);
}
sort2(a,10);
printf("输出排序后的结果:\n");
for(i=0;i<10;i++)
{
printf("%d ",a[i]);
}
}
Results Figure:
3. Analysis
Question: experiment, the function for generating random numbers do not know how to use.
Workaround: srand and rand function is used in combination.
7.3.2 Lab Exercises
1. Problem Description
A: programming from a number of keyboard rows, the specified number of rows output Pascal triangle.Two: Write a program from the keyboard respectively Enter the year, month, day, calculated from the day the first few days of this year.
2. Experiment Code
A flow chart:
#include "stdio.h"
main()
{
int a[50][50],i,j,n,y,x;
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(x=1;x<=n;x++)
{
for(y=1;y<=n;y++)
{
if(y<=x)
{
printf("%d ",a[x][y]);
}
}
printf("\n");
}
}
Results Figure:
Two Flowchart:
#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(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("请分别输入年、月、日:");
scanf("%d%d%d",&y,&m,&d);
printf("是这年的第%d天",day(y,m,d));
}
Results Figure:
3. Analysis
Issue: The experiment did not have a problem.
Dian two experimental content
7.3.3 Lab Exercises
1. Problem Description
A: programming, a character string input from the keyboard, determines whether it is a palindrome.2. Experiment Code
A flow chart:
#include "stdio.h"
#include "string.h"
#define N 40
main()
{
char str[N],ch='Y';
int i;
int len;
printf("Input a string:");
scanf("%s",&str);
len=strlen(str);
for(i=0;i<=len;i++)
{
if(str[i]!=str[len-1-i])
{
ch='N';
break;
}
}
if(ch=='Y')
{
printf("%s是回文",str);
}
else
{
printf("%s不是回文",str);
}
}
Results Figure:
3. Analysis
Problem: The results will be running against each other, should not be a palindrome is a palindrome.
Solution: Add i <= len, on the for loop.
Third, test summary
Harvest: The experiment greatest achievement is to understand the usage of srand function, as well as methods for the use of arrays easier to clear.
Inadequate: this experiment, I found that I do not understand for commonly used functions is not enough, not flexible to use, but also need more strengthened.