2009-11
21
你可能也对这些感兴趣
点我收起
错误的函数在于修改&mi, &ma这两个地址,同时指向了nombre[0],所以函数中修改*max和*min的值,也就是修改max和min两个指针指向的值的时候,实际上修改的是nombre[0]的值,mi和ma这两个int变量一直没有赋值。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | void fonction_right(int t[], int n, int *min, int *max){ *min=t[0]; *max=t[0]; for(int i=1; i<n;i++){ if(t[i]>*max){ *max=t[i]; } if(t[i]<*min){ *min=t[i]; } } } void fonction_wrong(int t[], int n, int *min, int *max){ min=t; max=t; for(int i=1; i<n;i++){ if(t[i]>*max){ *max=t[i]; } if(t[i]<*min){ *min=t[i]; } } } void Exo8(){ int n; cout<<"n = ?"<<endl; do{ cin>>n; }while(n<1); int *nombre= new int [n]; cout<<"Entrez "<<n<<" nombres pour les stocker dans un tableau."<<endl; for(int i=0;i<n;i++){ if(i==0) cout<<"Le 1er nombre est?"<<endl; else cout<<"Le "<<i+1<<"eme nombre est?"<<endl; cin>>nombre[i]; } int mi,ma; fonction_wrong(nombre,n,&mi,&ma); cout<<"Le plus grand entier dans le tableau est "<<mi<<endl; cout<<"Le plus petit entier dans le tableau est "<<ma<<endl; delete [] nombre; } |
我主要来测试一下wp-syntax这个插件。嘿嘿,不错。
<pre lang="cpp" line="1">中间写code,评论中也可以使用</pre>
Additional comments powered by BackType

还没有评论呢。