📄 xt4.htm
字号:
<TD><A href="http://www.nuist.edu.cn/JSJ/syjx/sjdg.htm"><IMG height=32
src="xt4.files/sy2.gif" width=70 border=0></A></TD></TR></TBODY></TABLE></DIV>
<TABLE cellSpacing=0 cellPadding=0 width=777 border=0><!--DWLayoutTable-->
<TBODY>
<TR>
<TD vAlign=top colSpan=3 height=148><IMG height=148
src="xt4.files/maintop.gif" width=777></TD></TR>
<TR>
<TD vAlign=top colSpan=3 height=32><IMG height=32
src="xt4.files/hard_03.gif" width=778 useMap=#Map border=0> </TD></TR>
<TR>
<TD vAlign=top width=181 height=164>
<TABLE cellSpacing=0 cellPadding=0 width="100%" align=right border=0><!--DWLayoutTable-->
<TBODY>
<TR>
<TD vAlign=top colSpan=2 height=30><IMG height=30
src="xt4.files/gai_02.gif" width=177></TD></TR>
<TR>
<TD width=41 height=4></TD>
<TD width=134></TD></TR>
<TR>
<TD height=26>
<DIV align=right><IMG height=15 src="xt4.files/article_common.gif"
width=9></DIV></TD>
<TD>
<DIV align=left><A class=mb14
href="http://www.nuist.edu.cn/JSJ/syjx/sbcl.htm"> 申报材料</A></DIV></TD></TR>
<TR>
<TD height=26>
<DIV align=right><IMG height=15 src="xt4.files/article_common.gif"
width=9></DIV></TD>
<TD>
<DIV align=left><A class=mb14
href="http://www.nuist.edu.cn/JSJ/syjx/szll.htm"> 师资力量</A></DIV></TD></TR>
<TR>
<TD height=26>
<DIV align=right><IMG height=15 src="xt4.files/article_common.gif"
width=9></DIV></TD>
<TD>
<DIV align=center>
<P align=left><A class=mb14
href="http://www.nuist.edu.cn/JSJ/syjx/jxcg.htm"> 教学成果</A></P></DIV></TD></TR>
<TR>
<TD height=26>
<DIV align=right><IMG height=15 src="xt4.files/article_common.gif"
width=9></DIV></TD>
<TD>
<DIV align=left><A
href="http://www.nuist.edu.cn/JSJ/syjx/jxkh.htm"> 教学考核</A></DIV></TD></TR>
<TR>
<TD height=26>
<DIV align=right><IMG height=15 src="xt4.files/article_common.gif"
width=9></DIV></TD>
<TD>
<DIV align=left><A class=mb14
href="http://www.nuist.edu.cn/JSJ/syjx/zwpj.htm"> 自我评价</A></DIV></TD></TR>
<TR>
<TD height=26>
<DIV align=right><IMG height=15 src="xt4.files/article_common.gif"
width=9></DIV></TD>
<TD>
<DIV align=left><A class=mb14
href="http://www.nuist.edu.cn/JSJ/syjx/jsgh.htm"> 建设规划</A></DIV></TD></TR>
<TR>
<TD height=26>
<DIV align=right><IMG height=15 src="xt4.files/article_common.gif"
width=9></DIV></TD>
<TD>
<DIV align=left><A class=mb14
href="http://www.nuist.edu.cn/JSJ/syjx/jcxy.htm"> 教材选用</A></DIV></TD></TR>
<TR>
<TD height=2></TD>
<TD></TD></TR></TBODY></TABLE></TD>
<TD vAlign=top colSpan=2>
<TABLE class=bg height=241 cellSpacing=0 cellPadding=0 width="100%"
border=0>
<TBODY>
<TR>
<TD width="11%" height=33><IMG height=34 src="xt4.files/homedha.gif"
width=55><IMG height=34 src="xt4.files/homedhbg.gif" width=8></TD>
<TD
width="89%"><!-- InstanceBeginEditable name="EditRegion4" --><SPAN
class=s14><STRONG>习题四</STRONG></SPAN><!-- InstanceEndEditable --></TD></TR>
<TR>
<TD colSpan=2><!-- InstanceBeginEditable name="EditRegion5" -->
<TABLE cellSpacing=1 cellPadding=1 width="100%" border=1>
<TBODY>
<TR>
<TD>
<DIV align=center>第 4 章 数组、指针和字符串 </DIV></TD></TR>
<TR>
<TD><SPAN class=xt12>4.1什么是数组?<BR>答:数组是若干个同类数据元素的集合。 </SPAN>
<P
class=xt12>4.2一维数组与二维数组在内存中是如何存储的?<BR>答:一维数组a[n]定义后,系统分配n*k个连续存储单元,用于存放数组元素值,其中k=元素占用字节数。<BR>二维数组a[m][n]定义后,系统分配m*n*k个连续存储单元,用于存放数组元素值,其中k=元素占用字节数。<BR>存放方式为按行存放,即先存第一行元素,再存第二行元素,依次把各行元素存入一串连续的存储单元中。</P>
<P class=xt12>4.3有如下数组定义:<BR>int
a[20];<BR>指出该数组的数组名、数组元素类型、数组元素个数、第一个数组元素的下标值和最后一个数组元素的下标值。<BR>答:数组名为a,数组元素类型为整型,数组元素个数为20,第一个数组元素的下标值为0和最后一个数组元素的下标值为19。</P>
<P class=xt12>4.4写出下列程序的运行结果:<BR>#include
<iostream.h><BR>void main(void)<BR>{ int
i,k,a[10],p[3];<BR> k=5;<BR> for (i=0;i<10;i++) //
a[0]=0,…,a[9]=9<BR> a[i]=i;<BR> for
(i=0;i<3;i++)<BR> p[i]=a[i*(i+1)]; //
p[0]=a[0]=0,p[1]=a[2]=2,p[2]=a[6]=6,<BR> for
(i=0;i<3;i++)<BR> k=k+p[i]*2; //
k=5+0*2+2*2+6*2=21<BR> cout<<k<<endl;<BR>}<BR>答案:
k=21</P>
<P class=xt12>4.5写出下列程序的运行结果:<BR>#include
<iostream.h><BR>void main(void)<BR>{ int
a[6][6],i,j;<BR> for (i=1;i<6;i++)<BR> for
(j=1;j<6;j++)<BR> a[i][j]=(i/j)*(j/i); //当i<j时i/j=0
当j<i时j/i=0 所以只有当I=j时 a[I][j]=1<BR> for
(i=1;i<6;i++)<BR> { for
(j=1;j<6;j++)<BR> cout<<a[i][j]<<'\t';<BR> cout<<endl;<BR> }<BR>}<BR>答:<BR>a[i][j]=(i/j)*(j/i);
//当i<j时i/j=0 当j<i时j/i=0 所以<BR>a[i][j]=1,
i=j<BR>a[i][j]=0, i≠j<BR>即矩阵对角线上元素为1,其它元素为0。输出结果为对角线矩阵:<BR>1 0
0 0 0 0<BR>0 1 0 0 0 0<BR>0 0 1 0 0 0<BR>0 0 0 1 0 0<BR>0 0 0
0 1 0<BR>0 0 0 0 0 1</P>
<P class=xt12>4.6写出下列程序的运行结果:<BR>#include
<iostream.h><BR>#include <string.h><BR>void
main(void)<BR>{ char str[80];<BR> int
i,j,k;<BR> cout<<"Input
string:";<BR> cin>>str;<BR> for
(i=0,j=strlen(str)-1;i<j;i++,j--)<BR> {
k=str[i];<BR> str[i]=str[j];<BR> str[j]=k;<BR> }<BR> cout<<str<<endl;<BR>}<BR>运行时输入:abcdef(回车)<BR>答:fedcba</P>
<P
class=xt12>4.7某班有30个学生,进行了数学考试,编写程序将考试成绩输入一维数组,并求数学的平均成绩及不及格学生的人数。<BR>解:<BR>#include
<iostream.h><BR>#define N 10<BR>void main(void)<BR>{
float math[N],sum,ave;<BR> int i,count;<BR> cout<<"Input
math score:";<BR> sum=0;<BR> count=0;<BR> for(i=0;
i<N;i++)<BR> cin>>math[i];<BR> for(i=0;i<N;i++)<BR> {
sum=sum+math[i];<BR> if (math[i]<60)
count++;<BR> }<BR> ave=sum/N;<BR> cout<<"ave="<<ave<<'\t'<<"count="<<count<<endl;<BR>}
</P>
<P
class=xt12>4.8设有一个数列,它的前四项为0、0、2、5,以后每项分别是其前四项之和,编程求此数列的前20项。用一维数组完成此操作。(提示:a[i]=a[i-1]+a[i-2]+a[i-3]+a[i-4];)<BR>解:<BR>#include
<iostream.h><BR>#include <iomanip.h><BR>#define N
20<BR>void main(void)<BR>{ long
i,a[N+1];<BR> a[1]=a[2]=0;<BR> a[3]=2;<BR> a[4]=5; <BR> for
(i=5;i<=N;i++)<BR> a[i]=a[i-1]+a[i-2]+a[i-3]+a[i-4];<BR> for
(i=1;i<=N;i++)<BR> {
cout<<setw(8)<<a[i];<BR> if (i%5==0)
<BR> cout<<endl;<BR> }<BR>}<BR><BR>4.9某班有30个学生,进行了数学考试,编写程序将考试成绩输入一维数组,并将数学成绩按由高到低的顺序排序后输出。(提示:实验时输入10个学生成绩为好)<BR>解:方法一:冒泡法<BR>#include
<iostream.h><BR>#include <iomanip.h><BR>#define N
10<BR>void main(void)<BR>{ float math[N];<BR> int
i,j,temp;<BR> cout<<"Input math
score:"<<endl;<BR> for(i=0;i<N;i++)<BR> cin>>math[i]
;<BR> for(i=1;i<=N-1;i++)<BR> for(j=0;j<=N-i;j++)<BR> {
if (math[j]<math[j+1])<BR> {
temp=math[j];<BR> math[j]=math[j+1];<BR> math[j+1]=temp;<BR> }<BR> }<BR> for
(i=0;i<N;i++)<BR> {
cout<<setw(6)<<math[i]<<endl;<BR> }<BR>}<BR>方法二:选择法<BR>#include
<iostream.h><BR>#include <iomanip.h><BR>#define N
10<BR>void main(void)<BR>{ float math[N+1];<BR> int
i,j,temp;<BR> cout<<"Input math
score:"<<endl;<BR> for(i=1;
i<=N;i++)<BR> cin>>math[i];<BR> for(i=1;i<=N-1;i++)<BR> for(j=i+1;
j<=N;j++)<BR> { if (math[i]<math[j]) <BR> {
temp=math[i];<BR> math[i]=math[j];<BR> math[j]=temp;<BR> }<BR> }<BR> for
(i=1;i<=N;i++)<BR> cout<<setw(6)<<math[i]<<endl;<BR>}</P>
<P
class=xt12>4.10.已有一按从小到大次序排序好的数组,现输入一数,要求按原来排序的规律将它插入到数组中。(习题课内容,提示:先定位、向后移,再插入)<BR>解:<BR>#include
<iostream.h><BR>#include <iomanip.h><BR>#define N
10<BR>void main(void)<BR>{ float a[N];<BR> int
i,b,j;<BR> cout<<"Input sort array
a[9]:"<<endl;<BR> for(i=0;i<N-1;i++)<BR> cin>>a[i];<BR> cout<<"Input
number b:";<BR> cin>>b;<BR> i=0;<BR> while (a[i]<b)
i++; <BR> for(j=N-1;j>i;j--)
a[j]=a[j-1];<BR> a[i]=b;<BR> for
(i=0;i<N;i++)<BR> cout<<setw(6)<<a[i]<<endl;<BR>}<BR>输入:1
2 3 4 5 6 7 8 10 <BR>插入:9<BR>输出 :1 2 3 4 5 6 7 8 9 10 </P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -