第八章 指针答案

合集下载

操作系统--精髓与设计原理(第八版)第八章复习题答案

操作系统--精髓与设计原理(第八版)第八章复习题答案

操作系统--精髓与设计原理(第⼋版)第⼋章复习题答案8.操作系统--精髓与设计原理(第⼋版)第⼋章复习题答案8.1 简单分页与虚拟内存分页有何区别?进程运⾏时,简单分页的所有页必须都在内存中,除⾮使⽤了覆盖技术,虚存分页并⾮所有页都须在内存页框中,仅在需要时才读⼊页,把⼀页读⼊内存可能需要把另⼀页写出到磁盘。

8.2 什么是抖动?当操作系统读取⼀块内存时,它必须把另⼀块换出。

如果⼀块正好在将要⽤到之前换出,操作系统就不得不很快地把它取回。

这类操作通常会导致⼀种称为系统抖动( thrashing)的情况。

这样会使处理器的⼤部分时间都⽤于交换块⽽⾮执⾏指令。

8.3 为何在使⽤虚拟内存时,局部性原理⾄关重要?局部性原理描述了⼀个进程中程序和数据引⽤的集簇倾向。

因此,假设在很短的时间内仅需要进程的⼀部分块是合理的。

同时,还可以对将来可能会访问的块进⾏猜测,从⽽避免系统抖动。

局部性原理表明虚拟内存⽅案是可⾏的。

8.4 哪些元素是页表项中能找到的典型元素?简单定义每个元素。

页号: 虚拟地址的页号部分。

进程标志符:使⽤该页的进程。

页号和进程标志符共同标志-个特定进程的虚拟地址空间的⼀页。

控制位: 该域包含⼀些标记,⽐如有效、访问和修改,以及保护和锁定信息。

链指针: 若某项没有链项,则该域为空(或⽤⼀个单独的位来表⽰)。

否则,该域包含链中下⼀项的索引值(0~2^m -1之间的数字)。

8.5 转换检测缓冲区的⽬的是什么?原则上,每次虚存访问都可能会引起两次物理内存访问:⼀次取相应的页表项,另⼀次取需要的数据。

因此,简单的虚拟内存⽅案会导致内存访问时间加倍。

为克服这个问题,⼤多数虚拟内存⽅案都为页表项使⽤了⼀个特殊的⾼速缓存,通常称为转换检测缓冲区(TranslationLookaside Buffer, TLB)。

8.6 简单定义两种可供选择的页⾯读取策略。

请求分页,只有当访问到某页中的⼀个单元时才将该页取⼊内存。

c语言第八章课后题答案剖析

c语言第八章课后题答案剖析
void main()
{
int score[10];
Readscore(score);
Sort(score);
8-6、
#include <stdio.h>
#define N 40
int Readscore(int score[], long num[]);
int FindMax(int score[],long num[],int n);
void main()
{
int score[N];
long num[N];
maxPos=minPos=0;
for(n=0; n<10; n++)
{
if(a[n]>max)
{
max=a[n];
maxPos=n;
}
else if(a[n]<min)
{
min=a[n];
minPos=n;
}
}
printf("max=%d,pos=%d\n",max,maxPos);
printf("min=%d,pos=%d\n",min,minPos);
for(i=2; i<n; i++)
{
f[i]=f[i-1]+f[i-2];
}
}
(3)
#include <stdio.h>
int main()
{
int a[10],n,max,min,maxPos,minPos;
for(n=0; n<10; n++)
{
scanf("%d",&a[n]);

C语言全国计算机二级等级考试教程d第八章地址和指针(章节带习题)

C语言全国计算机二级等级考试教程d第八章地址和指针(章节带习题)

D1
D 5,-2,-7
习题
a[0] 10 a[1] 20 a[2] 30 a[3] 40 a[4] 50 8.4 若有以下程序: C p #include <stdio.h> main() {int k=2,m=4,n=6,*pk=&k,*pm=&m,*p; *(p=&n)=*pk*(*pm); printf("%d\n",n); } 程序的输出结果是 A4 B6 C8 D 10 8.5 若指针p已正确定义并指向如图所示存储单元: B 则执行语句*p++; 后,*p的值是 A 20 B 30 C 21 D 31 8.6 若指针p已正确定义并指向如图所示存储单元,则*++p的值是 A 20 B 30 C 21 D 31 8.7 若指针p已正确定义并指向如图所示存储单元,则++*p的值是 A 20 B 30 C 21 D 31
j=*p+1;
p
*p+=1; 或 ++*p; 或 若有以下定义和语句: int **p,*s,k=20; p s=&k; p=&s;
s
k 20
8.4 对指针变量的操作
8.4.1 通过指针来引用一个存储单元
程序举例: 例8.1 用指针指向两个变量,通过指针运算选出值最小的那个数。 #include <stdio.h> main() scanf(“%d%d”,&a,&b {int a,b,min,*pa,*pb,*pmin; ); pa=&a;pb=&b;pmin=&min; scanf(“%d%d”,pa,pb); /*输入的值依次放入pa、pb所指存储单元中*/ printf(“a=%d b=%d\n”,a,b); *pmin=*pa; min=a; if(*pa>*pb) *pmin=*pb; printf(“min=%d\n”,min); } if(a>b) min=b; 比如输入: 41 32<CR> pa pb pmin a b min 32 41

Java语言程序设计第九版第八章答案

Java语言程序设计第九版第八章答案

Chapter 8 Objects and Classes1.See the section "Defining Classes for Objects."2.The syntax to define a classis public class ClassName {}3.The syntax to declare a reference variable foran object isClassName v;4.The syntax to create an object isnew ClassName();5.Constructors are special kinds of methods that arecalled when creating an object using the new operator.Constructors do not have a return type—not even void.6. A class has a default constructor only if theclass does not define any constructor.7.The member access operator is used to access adata field or invoke a method from an object.8.An anonymous object is the one that does not havea reference variable referencing it.9. A NullPointerException occurs when a null referencevariable is used to access the members of an object.10.An array is an object. The default value for theelements of an array is 0 for numeric, false for boolean,‘ u0000’ for char, null for object element type.11.(a) There is such constructor ShowErrors(int) in theShowErrors class.The ShowErrors class in the book has a defaultconstructor. It is actually same aspublic class ShowErrors {public static void main(String[] args) {ShowErrors t = new ShowErrors(5);}public ShowErrors () {}}On Line 3, new ShowErrors(5) attempts to create aninstance using a constructor ShowErrors(int), but theShowErrors class does not have such a constructor.That is an error.(b) x() is not a method in the ShowErrors class.The ShowErrors class in the book has a defaultconstructor. It is actually same aspublic class ShowErrors {public static void main(String[] args) {ShowErrors t = new ShowErrors();t.x();}public ShowErrors () {}}On Line 4, t.x() is invoked, but the ShowErrors classdoes not have the method named x(). That is an error.(c)The program compiles fine, but it has aruntime error because variable c is null when theprintln statement is executed.(d)new C(5.0) does not match any constructors in classC. The program has a compilation error because classC does not have a constructor with a double argument.12.The program does not compile because new A() is used inclass Test, but class A does not have a defaultconstructor. See the second NOTE in the Section,“Constructors.”13.falsee the Date’s no -arg constructor to create a Date forthe current time. Use the Date’s toString() method to display a string representation for the Date.15. Use the JFrame ’s no -arg constructor to create JFrame. Use thesetTitle(String) method a set a title and use the setVisible(true)method to display the frame.16.Date is in java.util. JFrame and JOptionPane are injavax.swing. System and Math are in ng.17.System.out.println(f.i);Answer: CorrectSystem.out.println(f.s);Answer: Correctf.imethod();Answer: Correctf.smethod();Answer: CorrectSystem.out.println(F.i);Answer: IncorrectSystem.out.println(F.s);Answer: CorrectF.imethod();Answer: IncorrectF.smethod();Answer: Correct18.Add static in the main method and in the factorialmethod because these two methods don ’t need referenceany instance objects or invoke any instance methods in theTest class.19. You cannot invoke an instance method or reference aninstance variable from a static method. You can invoke astatic method or reference a static variable from aninstance method? c is an instance variable, which cannot beaccessed from the static context in method2.20.Accessor method is for retrieving private data value andmutator method is for changing private data value. The namingconvention for accessor method is getDataFieldName() for non-boolean values and isDataFieldName() for boolean values. Thenaming convention for mutator method is setDataFieldName(value).21.Two benefits: (1) for protecting data and (2) for easyto maintain the class.22.Not a problem. Though radius is private,myCircle.radius is used inside the Circle class. Thus, itis fine.23.Java uses “pass by value ” to pass parameters to amethod. When passing a variable of a primitive type to amethod, the variable remains unchanged after themethod finishes. However, when passing a variable of areference type to a method, any changes to the objectreferenced by the variable inside the method arepermanent changes to the object referenced by thevariable outside of the method. Both the actualparameter and the formal parameter variables referenceto the same object.The output of the program is as follows:count 101times 024.Remark: The reference value of circle1 is passed to xand the reference value of circle2 is passed to y. The contents ofthe objects are not swapped in the swap1 method. circle1 andcircle2 are not swapped. To actually swap the contents of these objects, replace the following three linesCircle temp = x;x=y;y=temp;bydouble temp = x.radius;x.radius = y.radius;y.radius = temp;as in swap2.25. a. a[0] = 1 a[1] = 2 b.a[0] = 2 a[1] = 1c. e1 = 2 e2 = 1d. t1 ’s i = 2 t1t2 ’s i = 2 t2’s j = 1’s j = 126.(a) null(b)1234567(c)7654321(d)123456727.(Line 4 prints null since dates[0] is null. Line 5 causes a NullPointerException since it invokes toString() method from the null reference.)。

C++第8章指针课件

C++第8章指针课件
• 指向非常量的常量指针 调用: int x; f(&x) void f(int * const ptr) { 可以通过引用改变x的值; //*ptr=3; 不可以改变ptr本身的值; // ptr++; }; 此时, ptr是常量, x可以通过ptr的引用方式来 改动
const修饰参数的四种方式 const修饰参数的四种方式
–用引用运算符 “*” 解决。如 *a 表示的是 a 指向的这 个单元的内容。因此*也称为间接引用或间接运算符 –在对a 使用引用运算之前,必须先对 a 赋值
指针的定义及使用
• 指针变量可以指向不同的变量。如上例中intp指向 x,我们可以通过对intp的重新赋值改变指针的指 向。如果想让intp指向y,只要执行intp=&y就可以 了。这时,intp与x无任何关系。 同类的指针变量之间可相互赋值,表示二个指针 指向同一内存空间。 空指针
//元素个数 元素个数
//方法 方法2 方法
• iPtr=iArray; //此句不能省略,因为方法2修改了iPtr • for(n=0; n<size; n++) //方法3 • sum3 += *(iPtr+n); • • • • • iPtr=iArray; //此句可以省略,因为方法3没有修改iPtr for(n=0; n<size; n++) //方法4 sum4 += iPtr[n]; for(n=0; n<size; n++) //方法5 sum5 += *(iArray+n);
• 指向常量数据的非常量指针 调用: int x; f(&x) void f(const int *ptr) { 不可以通过引用改变x的值; //*ptr=3; 可以改变ptr本身的值; // ptr++; }; 此时, x是变量, ptr也是变量, 但不可以通过对 ptr的引用运算而onst修饰参数的四种方式

C语言实验八指针程序设计答案

C语言实验八指针程序设计答案

实验八指针的应用一、实验目的1.掌握指针变量的定义和引用。

2.掌握指针与变量的程序设计方法3.掌握指针与数组的程序设计方法4.掌握指针与字符串的程序设计方法5.掌握指针与函数的程序设计方法二、实验内容1.[目的]访问变量的地址。

[题目内容]已知,char a;int x;float p,q; 而且a=’A’;x=125;p=10.25;q=18.75;编写程序显示变量a,x,p,q的值及其地址。

[输入]无[输出]变量a,x,p,q的值及其地址[提示]使用运算符&,地址输出采用%u格式(因为内存地址是无符号的整数)。

#include <stdio.h>void main(){char a;int x;float p,q;a='A';x=125;p=10.25;q=18.75;printf("%c is stored at addr %u.\n",a,&a);printf("%d is stored at addr %u.\n",x,&x);printf("%f is stored at addr %u.\n",p,&p);printf("%f is stored at addr %u.\n",q,&q);}A is stored at addr 1245052.125 is stored at addr 1245048.10.250000 is stored at addr 1245044.18.750000 is stored at addr 1245040.Press any key to continue2.[目的]通过指针访问变量。

[题目内容]已知,int x,y;int *ptr; 而且x=10;ptr=&x;y=*ptr;,运行下列程序,使用运算符*来访问指针所指的值。

08-指针-程序设计基础-罗兵-清华大学出版社


}
被调函数通过指针访问调用函数的局部变量。
8.2 指针与一维数组
一维数组名就是该数组第一个元素的地址,或数组的首地址。 所以将其赋值给一个指针变量后,二者几乎等价,区别是数组名是 指针常量,指针变量是变量,可以再赋值。
int a[]={1,2,3,4},*p;
// 定义数组、指针变量
p=a;
// p为a数组的首地址、整型指针
*(p+i)=*(p+n-1-i); *(p+n-1-i)=m; } }
// 函数定义 // 交换
8.3 指针与二维数组
int a[3][4]; int (*pa)[4]; char **p2; char *(pd[3]);
// 函数声明,a、n均可省略 // 函数声明,p、n均可省略
上面声明的两个函数,它们的参数形式是完全一样的。
8.2.2 指针做函数参数
例8.7 编写函数,将整型数组的元素值逆序。(一维数组名做参数)
void invert1(int a[],int n); { int m,i;
for(i=0;i<n/2;i++) { m=a[i];
指针是一种数据类型,它长度为4字节,它有两重属性:
内存地址 该地址存储数据的数据类型
8.1.2 指针变量的定义
指针类型有指针变量和指针常量。 指针常量不显性表示,通过变量名加取地址运算符、数组名等表示。 指针变量需要先声明,后使用。需要间接寻址运算符 *
int a,b[5]; int *p; p=&a; p=b;
p1=&m;
// p1为变量m的地址、整型
p2=a;
// p2为数组a的首地址、整型

c程序设计第四版(谭浩强)第八章答案

#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>/*int main(){int *p1,*p2,*p3,*t,a,b,c;printf("enter three number:");scanf("%d%d%d",&a,&b,&c);p1=&a;p2=&b;p3=&c;if(a>b){t=p1;p1=p2;p2=t;}if(a>c){t=p1;p1=p3;p3=t;}if(b>c){t=p2;p2=p3;p3=t;}printf("sort number:%d %d %d\n",*p1,*p2,*p3); }*//*#define N20int main(){char *p1,*p2,*p3,*t,a[N],b[N],c[N];printf("enter three strings:");scanf("%s%s%s",a,b,c);p1=a;p2=b;p3=c;if(strcmp(a,b)>0){t=p1;p1=p2;p2=t;}if(strcmp(a,c)>0){t=p1;p1=p3;p3=t;}if(strcmp(b,c)>0){t=p2;p2=p3;p3=t;}printf("sort string:%s%s%s\n",p1,p2,p3);}*//*void input(int*p){int i;printf("enter10number:");for(i=0;i<10;i++)scanf("%d",p+i);void output(int*p){int i;printf("new10number:");for(i=0;i<10;i++)printf("%-2d",*(p+i));}void sort(int*p,int n){int i,t;for(i=0;i<n-1;i++){if(*(p+i)>*(p+i+1)){t=*(p+i);*(p+i)=*(p+i+1);*(p+i+1)=t;} }for(i=n-2;i>0;i--){if(*(p+i-1)>*(p+i)){t=*(p+i);*(p+i)=*(p+i-1);*(p+i-1)=t;} }}int main(){int a[10],*p=a;input(p);sort(p,10);output(p);putchar('\n');}*//*#define N10void houyi(int*p,int m){int i,b[N]={0};for(i=0;i<N-m;i++)b[i+m]=*(p+i);for(;i<N;i++)b[i-N+m]=*(p+i);for(i=0;i<N;i++)printf("%3d",b[i]);}int main()int a[N],i,m,*p=a;printf("enter number:");for(i=0;i<N;i++)scanf("%d",&a[i]);printf("enter m:");scanf("%d",&m);printf("new number:");houyi(p,m);putchar('\n');}*//*int main(){int count(int*p,int n);int n,*p,i;printf("enter number:");scanf("%d",&n);p=(int *)calloc(n,sizeof(int));for(i=0;i<n;i++)*(p+i)=0;printf("the last number is %d\n",count(p,n)+1);free(p);}int count(int*p,int n){int i,j=1,k=0;for(i=0;i<n;i++){if(*(p+i)!=3){*(p+i)=j;if(*(p+i)==3)j=1; //数到3 时将下一个重置为1elsej+=1;if(i==(n-1))i=-1; //轮到到最后一个人报数时,将i 重置为-1,执行完for 语句后自动加一变为0,从头开始k+=1; //报完一个数后加一}else{if(i==(n-1))i=-1; //检查到最后一个人是3时,将i 重置为-1,执行完for 语句后自动加一变为0,从头开始continue; //检查到这个人是3 时,跳过报数}if(k==3*(n-1)) //报数的总个数达到3*(n-1)时,就只剩一个人了break;elsecontinue;}for(i=0;i<n;i++)if(*(p+i)==3)continue;else return i;}*//*#define N20int length(char*p){int n=0;while(*p!=0){p++;n+=1;}return n;}int main(){char a[N],*p=a;printf("enter string:");gets(a);printf("字符串长度为:%d\n",length(p));}*//*#define N10int main(){void copyn(char*p,int m);char *p="abcdefghij";int m;printf("%s\nenter m:",p);scanf("%d",&m);copyn(p,m);}void copyn(char*p,int m){char b[N],i;for(i=m-1;*(p+i);i++)b[i-m+1]=*(p+i);b[i-m+1]='\0';printf("%s\n",b);}*//*#define N50void count(char*p){int d=0,x=0,k=0,s=0,o=0;while(*p) //不能写*(p++),否则计算出错,无法计算第一个字符{if(*p>='a'&&*p<='z')x+=1;else if(*p>='A'&&*p<='Z')d+=1;else if(*p>='0'&&*p<='9')s+=1;else if(*p==' ')k+=1;else o+=1;p++;}printf("大写字母:%d\n小写字母:%d\n空格:%d\n数字:%d\n 其他:%d\n",d,x,k,s,o);}int main(){char a[N],*p=a;printf("enter string:");gets(a);count(p);}*//*void zhuangzhi(int(*p)[3]){int i,j,t;for(i=0;i<3;i++)for(j=0;j<3;j++)if(i<j){t=*(*(p+i)+j); //*(*(p+i)+j)代表a[i][j]*(*(p+i)+j)=*(*(p+j)+i);*(*(p+j)+i)=t;}}int main(){int (*p)[3],a[3][3],i,j;p=a;printf("enter array:\n");for(i=0;i<3;i++)for(j=0;j<3;j++)scanf("%d",&a[i][j]);zhuangzhi(p);printf("new array:\n");for(i=0;i<3;i++)for(j=0;j<3;j++){printf("%-2d",a[i][j]);if(j==2)putchar('\n');}}*//*void central(int (*p)[5]){int i,j,l=0,r=0,t;//r 代表行,l 代表列for(i=0;i<5;i++)for(j=0;j<5;j++)if(*(*(p+i)+j)>*(*(p+r)+l)){r=i;l=j;}t=*(*(p+2)+2);*(*(p+2)+2)=*(*(p+r)+l);*(*(p+r)+l)=t; //最大元素放在中心l=0;r=0;for(i=0;i<5;i++)//四个角放最小元素,每次从第二个元素开始对比for(j=1;j<5;j++)if(*(*(p+i)+j)<*(*(p+r)+l)){r=i;l=j;//找出最小元素}t=*(*p);*(*p)=*(*(p+r)+l);*(*(p+r)+l)=t; //将最小元素与左上角元素交换,下同l=4;r=0;//换完重置r 和l,下同for(i=0;i<5;i++)for(j=1;j<5;j++)if(*(*(p+r)+l)>*(*(p+i)+j)){r=i;l=j;}t=*(*p+4);*(*p+4)=*(*(p+r)+l);*(*(p+r)+l)=t;r=4;l=0;for(i=0;i<5;i++)for(j=1;j<5;j++)if(i==0&&j==4) //遇到右上角,跳过对比continue;else if(*(*(p+r)+l)>*(*(p+i)+j)){r=i;l=j;}t=*(*(p+4));*(*(p+4))=*(*(p+r)+l);*(*(p+r)+l)=t;r=4;l=4;for(i=0;i<5;i++)for(j=1;j<5;j++)if(i==0&&j==4||i==4&&j==0) //遇到右上角和左下角,跳过对比continue;else if(*(*(p+r)+l)>*(*(p+i)+j)){r=i;l=j;}t=*(*(p+4)+4);*(*(p+4)+4)=*(*(p+r)+l);*(*(p+r)+l)=t;}int main(){int a[5][5],(*p)[5]=a,i,j;printf("enter array:\n");for(i=0;i<5;i++)for(j=0;j<5;j++)scanf("%d",*(p+i)+j);central(p);printf("new array:\n");for(i=0;i<5;i++){for(j=0;j<5;j++)printf("%-3d",*(*(p+i)+j));putchar('\n');}}*//*void sort(char*p[],int n) //指针数组处理{int i,j;char *k;for(i=1;i<n;i++)for(j=i;j<n;j++)if(strcmp(p[i-1],p[j])>0){k=p[i-1];p[i-1]=p[j];p[j]=k;}}int main(){char*p[10]={"gutsy","glory","gonad","girth","gaudy","gypsy","gusto","guppy","group","grope"}; //测试数据gutsy glory gonad girth gaudy gypsy gusto guppy group gropeint i;printf("enter strings:");for(i=0;i<10;i++)printf("%s ",p[i]);sort(p,10);printf("\nnew strings:");for(i=0;i<10;i++)printf("%s ",p[i]);putchar('\n');}*//*#define N20void sort(char*p,int n) //指针处理{int i,j;char k[N];for(i=1;i<n;i++)for(j=i;j<n;j++)if(strcmp(p+20*(i-1),p+20*j)>0) //不能写为p+i-1,否则出错{strcpy(k,p+20*(i-1));strcpy(p+20*(i-1),p+20*j);strcpy(p+20*j,k);}}int main(){char a[10][N];char *p=a[0]; //不能写为*p=a,这等价于是把行向量赋值给列向量int i;printf("enter strings:\n");for(i=0;i<10;i++)scanf("%s",&a[i]); //可写为gets(a[i]),这样写的话,每次输入字符串后按enter,或写为scanf("%s",&a[i]),每输一个字符后按空格即可;sort(p,10);printf("new strings:\n");for(i=0;i<10;i++)printf("%s ",a[i]);putchar('\n');}*//*#define n 1000 //矩形法求定积分,n 为区间分割数目double ding(double a,double b,double(*fun)(double)){double s=0;int i;for(i=1;i<n;i++)s+=(*fun)(i*(b-a)/n);return s*=(b-a)/n;}int main(){double a,b,s1,s2,s3;//定义积分下限,上限,用double 精度较高printf("输入积分下限,上限,分割区间数目:");scanf("%lf%lf",&a,&b);s1=ding(a,b,sin);s2=ding(a,b,cos);s3=ding(a,b,exp);printf("sin(x)从%f 到%f 的积分为%f\n",a,b,s1);printf("cos(x)从%f 到%f 的积分为%f\n",a,b,s2);printf("exp(x)从%f 到%f 的积分为%f\n",a,b,s3);}*//*#define N10void nixu(int*p){int i,m=N/2,t;for(i=0;i<m;i++){t=*(p+i);*(p+i)=*(p+N-1-i);*(p+N-1-i)=t;}}int main(){int a[N],*p=a,i;printf("enter %d numbers:\n",N);for(i=0;i<N;i++)scanf("%d",&a[i]);nixu(p);printf("new numbers:\n");for(i=0;i<N;i++)printf("%d ",a[i]);putchar('\n');}*//*void average1(float(*p)[6],int n){int i;float ave=0;for(i=0;i<n;i++)ave+=*(*(p+i)+1)/4;printf("第一门课程的平均分为:%4.2f\n",ave);}float average(float(*p)[6]){int i;float ave=0;for(i=1;i<6;i++)ave+=*(*p+i)/5;return ave;}void out(float(*p)[6],int n){int i,j,k,l,s=0;printf("两门课以上不及格的学生:\n");for(i=0;i<n;i++){k=0;for(j=1;j<6;j++){if(*(*(p+i)+j)<60)k+=1;else continue;}if(k>=2){s=1;printf("%4.0f",*(*(p+i)));for(l=1;l<6;l++)printf("%4.0f",*(*(p+i)+l));printf(" 平均分为:%4.2f\n",average(p+i));}}if(s==0) //没有符合条件的学生时输出“无”printf("无!\n");}void in(float(*p)[6],int n){int i,j,l;for(i=0;i<n;i++){for(j=0;j<6;j++){if(average(p+i)>=90){printf("平均分在90 分以上的学生:%4.0f",*(*(p+i)));for(l=1;l<6;l++)printf("%4.0f",*(*(p+i)+l));putchar('\n');break;}if(*(*(p+i)+j)>=85)continue;elsebreak;}if(j==6){printf("全部课程成绩在85 分以上的学生:%4.0f",*(*(p+i)));for(l=1;l<6;l++)printf("%4.0f",*(*(p+i)+l));putchar('\n');}}}int main(){int i,j;floata[4][6]={{1001,65,67,55,60,45},{1002,80,87,90,81,85},{1003,90,99,100,98,95},{1004,86,89,90,91, 85}};float (*p)[6]=a;for(i=0;i<4;i++){for(j=0;j<6;j++)printf("%4.0f ",a[i][j]);putchar('\n');}average1(p,4);out(p,4);in(p,4);}*//*#define N10 //方法一int num1(char*p) //将数字字符串转为整形数字的函数{int n=strlen(p),i,a=0;for(i=0;i<n;i++)a+=(int)((*(p+i)-48)*pow(10,n-1-i));return a;}void number(char*p){int i,num=0,j=1,k=-1,n=strlen(p); //j 用于计算数字长度,k 用于计算数字个数int a[N];//最多容纳N 个整数for(i=0;i<=n;i++){if(*p<'0'||*p>'9'){if(num==1){*p='\0';a[k]=num1(p-j);}num=0;}else if(num==0){num=1;j=1; //找到新数字时重置jk+=1;}elsej+=1;p++;}for(i=0;i<k+1;i++)printf("%d ",a[i]);putchar('\n');}*//*#define N10void number(char*p) //方法二{int i,num=0,j=1,k=-1,n=strlen(p); //j 用于计算数字长度,k 用于计算数字个数char a[N][10];//最多容纳N 个整数,每个整数最多十位数for(i=0;i<=n;i++){if(*p<'0'||*p>'9'){if(num==1){*p='\0';strcpy(a[k],p-j);}num=0;}else if(num==0){num=1;j=1; //找到新数字时重置jk+=1;}elsej+=1;p++;}for(i=0;i<k+1;i++)printf("%s ",a[i]);putchar('\n');}int main(){char a[100],*p=a; //最多可输入100个字符printf("enter strings:\n");gets(p);number(p);}*//*//字符串比较函数#define N20int str_cmp(char*p1,char*p2){int i,n=strlen(p1);for(i=0;i<=n;i++)if(*(p1+i)==*(p2+i))continue;else if(*(p1+i)>*(p2+i)){return*(p1+i)-*(p2+i);break;}else{return*(p1+i)-*(p2+i);break;}if(i==n+1)return0;}int main(){char a[N],b[N],*p1=a,*p2=b;printf("enter string1:");gets(p1);printf("enter string2:");gets(p2);printf("%d\n",str_cmp(p1,p2));}*//*void month(int*p){char*a[12]={"January","February","March","April","May","June","July","August","September","Octob er","Novenber","December"};int i;for(i=0;i<12;i++)if(*p==i+1)printf("英文:%s\n",a[i]);else continue;}int main(){int a,*p=&a;printf("enter month:");scanf("%d",p);month(p);}*//*#define SIZE1000char newbuf[SIZE]; //定义字符数组newbufchar *newp=newbuf;//定义指针变量newp,指向可存区的始端char *mynew(int n) //定义开辟存区的函数new,开辟存储区后返回指针{if(newp+n<=newbuf+SIZE) // 开辟区未超过newbuf数组的大小{newp+=n; // newp 指向存储区的末尾return(newp-n);} // 返回一个指针,它指向存区的开始位置elsereturn(NULL); // 当存区不够分配时,返回一个空指针}void free(char*p){if(p>=newbuf&&p<newbuf+SIZE)newp=p; // newp 指向存储区的开始位置}int main(){char *p=mynew(6);strcpy(p,"hello");printf("%s\n",p);free(p);}*//*#define N10 //定义每个字符串的最大字符数void sort(char*a[],int n){char **p=a;int i,j;char *k;for(i=1;i<n;i++)for(j=i;j<n;j++)if(strcmp(*(p+i-1),*(p+j))>0){k=*(p+i-1);*(p+i-1)=*(p+j);*(p+j)=k;}}int main(){char a[5][N];char *aa[5];int i;printf("enter strings:\n",);for(i=0;i<5;i++){scanf("%s",&a[i]);aa[i]=*(a+i);}sort(aa,5);printf("new strings:\n");for(i=0;i<5;i++)printf("%s ",aa[i]); //因为sort 函数改变的是aa[5]的排序,a[5][10]顺序并未改变,故输出的是aa[i],若写为a[i],输出顺序不变putchar('\n');}*/#define N5 //定义排序整数的个数void sort(int*a[],int n){int **p=a;int i,j,k;for(i=1;i<n;i++)for(j=i;j<n;j++)if(**(p+i-1)>**(p+j)){k=**(p+i-1);**(p+i-1)=**(p+j);**(p+j)=k;}}int main(){int a[N];int *aa[N];int i;printf("enter numbers:\n");for(i=0;i<N;i++){scanf("%d",&a[i]);aa[i]=a+i;}sort(aa,N);printf("new number:\n");for(i=0;i<N;i++)printf("%d ",a[i]); //因为sort 函数改变的是aa[N]的排序,也是a[N]的排序, 故输出的是a[i],也可写为*aa[i]putchar('\n');}。

c语言程序设计第五版课后答案谭浩强第八章课后答案

c语⾔程序设计第五版课后答案谭浩强第⼋章课后答案c语⾔程序设计第五版课后答案谭浩强习题答案第⼋章善于利⽤指针本章习题均要求使⽤指针⽅法处理。

1. 输⼊3个整数,要求按由⼩到⼤的顺序输出。

解题思路:先获取到三个变量的地址,然后获取三个数据,通过指针进⾏⽐较转换即可答案:#include <stdio.h>void swap(int *p_a, int *p_b){int temp = *p_a;*p_a = *p_b;*p_b = temp;}int main(){int a, b, c, *p_a = &a, *p_b = &b, *p_c = &c; // 获取每个变量空间的地址printf("Please enter three numbers:");scanf_s("%d%d%d", p_a, p_b, p_c);if (*p_a > *p_b) {swap(p_a, p_b);//通过指针进⾏指向空间内的数据交换}if (*p_a > *p_c) {swap(p_a, p_c);}if (*p_b > *p_c) {swap(p_b, p_c);}printf("%d %d %d\n", *p_a, *p_b, *p_c);system("pause");return 0;}2. 输⼊3个字符串,要求按由⼩到⼤的顺序输出。

解题思路:字符串的⽐较可以使⽤strcmp函数,返回值>0表⽰⼤于,返回值⼩于0表⽰⼩于,返回追等于0表⽰相同。

其他的⽐较排序思路与数字的排序交换没有区别,逐个进⾏⽐较先找出最⼤的,然后找出第⼆⼤的。

答案:#include <stdio.h>int main(){char str[3][32];char *p[3];printf("Please enter three strings:");for (int i = 0; i < 3; i++) {p[i] = str[i];scanf_s("%s", p[i], 32);//后边的数字限制缓冲区边界,防⽌缓冲区溢出访问越界}//让p[0]和p[1]/p[2]分别进⾏⽐较,找出最⼤的字符串,i+1之后,则让p[1]和p[2]进⾏⽐较,找出第⼆⼤//i循环总个数-1次,最后⼀个是不需要⽐较的for (int i = 0; i < 2; i++) {for (int j = i + 1; j < 3; j++) {if (strcmp(p[i], p[j]) > 0) {char *tmp = p[i]; p[i] = p[j]; p[j] = tmp;}}}printf("%s %s %s\n", p[0], p[1], p[2]);system("pause");return 0;}3. 输⼊10个整数,将其中最⼩的数与第⼀个数对换, 把最⼤的数与最后⼀个数对换。

第八章 指针答案

第八章指针习题答案8.1 输入3个整数,按由小到大的顺序输出。

参考程序如下:main(){ int a,b,c;void sort(int *p1,int *p2,int *p3);printf("\nInput three integar:");scanf("%d%d%d",&a,&b,&c);sort(&a,&b,&c);//地址传递}void sort(int *p1,int *p2,int *p3){ int *p;if(*p1>*p2) //如果p1的目标单元的值大于p2目标单元的值{p=p1;p1=p2;p2=p;} //交换p1和p2的指向,即p1指向较小值if(*p1>*p3){p=p1;p1=p3;p3=p;}if(*p2>*p3){p=p2;p2=p3;p3=p;} //通过3条if语句后,p1指向最小值,p3指向最大值printf("From small to large is:%d %d %d",*p1,*p2,*p3); //注意子函数结束后,实参} //a,b,c的值没有改变8.2 输入三个字符串,按由小到大的顺序输出。

参考程序如下:#include<string.h>main(){ char a[20],b[20],c[20];void sort(char *p1,char *p2,char *p3);printf("\nInput three strings:");gets(a);gets(b);gets(c);sort(a,b,c); //数组名做实参,仍是地址传递}void sort(char *p1,char *p2,char *p3) //p1指向数组a,p1是字符指针,p2、p3略{ //要注意字符数组和字符指针的区别,理论书有char *p;if(strcmp(p1,p2)) //strcmp(),字符串的比较函数{p=p1;p1=p2;p2=p;} //字符指针可以直接赋值,字符数组不能if(strcmp(p1,p3)){p=p1;p1=p3;p3=p;}if(strcmp(p2,p3)){p=p2;p2=p3;p3=p;}printf("\nFrom small to large is:%s\n%s\n%s\n",p1,p2,p3);//输出字符指针就是输出字符串}8.3 输入10个整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

第八章指针习题答案8.1 输入3个整数,按由小到大的顺序输出。

参考程序如下:main(){ int a,b,c;void sort(int *p1,int *p2,int *p3);printf("\nInput three integar:");scanf("%d%d%d",&a,&b,&c);sort(&a,&b,&c);//地址传递}void sort(int *p1,int *p2,int *p3){ int *p;if(*p1>*p2) //如果p1的目标单元的值大于p2目标单元的值{p=p1;p1=p2;p2=p;} //交换p1和p2的指向,即p1指向较小值if(*p1>*p3){p=p1;p1=p3;p3=p;}if(*p2>*p3){p=p2;p2=p3;p3=p;} //通过3条if语句后,p1指向最小值,p3指向最大值printf("From small to large is:%d %d %d",*p1,*p2,*p3); //注意子函数结束后,实参} //a,b,c的值没有改变8.2 输入三个字符串,按由小到大的顺序输出。

参考程序如下:#include<string.h>main(){ char a[20],b[20],c[20];void sort(char *p1,char *p2,char *p3);printf("\nInput three strings:");gets(a);gets(b);gets(c);sort(a,b,c); //数组名做实参,仍是地址传递}void sort(char *p1,char *p2,char *p3) //p1指向数组a,p1是字符指针,p2、p3略{ //要注意字符数组和字符指针的区别,理论书有char *p;if(strcmp(p1,p2)) //strcmp(),字符串的比较函数{p=p1;p1=p2;p2=p;} //字符指针可以直接赋值,字符数组不能if(strcmp(p1,p3)){p=p1;p1=p3;p3=p;}if(strcmp(p2,p3)){p=p2;p2=p3;p3=p;}printf("\nFrom small to large is:%s\n%s\n%s\n",p1,p2,p3);//输出字符指针就是输出字符串}8.3 输入10个整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换。

写3个函数:1)输入10个数;2)进行处理;3)输出10个数。

参考程序如下:#include<string.h>main(){ int a[10],i,max,min;void in(int *a,int n); //输入函数说明void sort(int *a,int n); //处理函数说明void out(int *a,int n); //输出函数说明in(a,10); //a—是地址传递,10是数值传递,注意二者的区别sort(a,10);out(a,10);}void in(int *a,int n) //a—整形指针,它的目标单元是主函数中的a[0],{ int i;for(i=0;i<n;i++){printf("\na[%d]:",i);//输出元素的下标scanf("%d",a+i); //a+i就是&a[i];}}void sort(int *a,int n) //a指针存放主函数中a[0]的地址{ int i,max,min,k; //如此一来,子函数中的a[i]就是主函数中的a[i] min=0;max=n-1;for(i=1;i<n;i++){ if(a[max]<a[i]) max=i;if(a[min]>a[i]) min=i;}if(min!=0){k=a[0];a[0]=a[min];a[min]=k;}if(max!=n-1){k=a[n-1];a[n-1]=a[max];a[max]=k;}}void out(int *a,int n) //a指针存放主函数中a[0]的地址{ int i; //如此一来,子函数中的a[i]就是主函数中的a[i] printf("After changed:");for(i=0;i<n;i++)printf("%d ",a[i]);}参考程序如下:8.4 有n个整数,使前面各数顺序向后移m个位置,最后m个数变成最前面m个数,见图10-47。

写一函数实现以上功能,在主函数中输入n个整数和输出调整后的n个数。

#define N 10main(){ int m,i;void aa(int *p,int m);int a[N];for(i=0;i<N;i++){printf("a[%d]:",i); scanf("%d",a+i);} printf("Input m:"); scanf("%d",&m);aa(a,m);printf("\nAfter changed:");for(i=0;i<N;i++)printf("%d ",a[i]);}void aa(int *p,int m){ int i,j,b[N];for(i=0;i<N-m;i++)b[i]=p[i];for(i=0;i<m;i++)p[i]=p[N-m+i];for(i=m,j=0;i<N;i++,j++)p[i]=b[j];}8.9、写一函数,将一个3*3的整形矩阵转置。

#include <stdio.h>int main(){ void move(int (*p)[3]);int a[3][3],(*p)[3],i,j;printf("input matrix:\n");for (i=0;i<3;i++)for(j=0;j<3;j++)scanf("%d",&a[i][j]);move(a);printf("Now,matrix:\n");for (i=0;i<3;i++){for(j=0;j<3;j++)printf("%4d",a[i][j]);printf("\n");}return 0;}void move(int (*p)[3]){ int i,j,t;for (i=0;i<3;i++)for (j=i;j<3;j++){t=p[i][j];p[i][j]=p[j][i];p[j][i]=t;}}8.20.用指针的方法,对5个字符串由小到大的顺序排序,然后输出。

#include<stdio.h>#include<string.h>sort(char (*p)[20]) /*冒泡法对5个字符串排序函数*/{ int i,j;char temp[20]; //i=0时,5个字符串未排序,需比较4次for (i=0;i<4;i++) //i=1时,4个字符串未排序,需比较3次for (j=0;j<4-i;j++) //i时,5-i个字符串未排序,需比较4-i次if (strcmp(p[j],p[j+1])>0) /*比较后交换字符串地址*/{strcpy(temp,p[j]);strcpy(p[j],p[j+1]);strcpy(p[j+1],temp);}return 0;}int main(){ int i;char str[5][20];for (i=0;i<5;i++)gets(str[i]); /*将第i个字符串的首地址赋予指针数组pstr 的第i个元素*/ sort(str);printf("strings sorted:\n");for (i=0;i<5;i++)printf("%s\n",str[i]);}8.21用指针的方法,对n个整数进行排序并输出。

要求将排序单独写成一个函数。

N个整数在主函数中输入,最后也在主函数中输出。

#include<stdio.h>#define N 10void sort(int *p)//冒泡法排序{ int i,j,t;for(i=0;i<N-1;i++)for(j=0;j<N-1-i;j++)if(p[j]>p[j+1]){t=p[j];p[j]=p[j+1];p[j+1]=t;}}int main(){ int a[N];int i;printf("请输入10个整数:");for(i=0;i<N;i++)scanf("%d",&a[i]);sort(a);printf("排序后的10个数:");for(i=0;i<N;i++)printf("%5d",a[i]);}。

相关文档
最新文档