少女祈祷中...

错误的函数在于修改&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>

: http://www.deuxmille.org/archives/939

本文相关评论 - 1条评论都没有呢

还没有评论呢。

  • :em14:
  • :em04:
  • :em11:
  • :em32:
  • :em08:
  • :em05:
  • :em17:
  • :em19:
  • :em24:
  • :em00:
  • :em31:
  • :em33:
  • :em06:
  • :em26:
  • :em27:
  • :em03:
  • :em28:
  • :em30:
  • :em13:
  • :em23:
  • :em21:
  • :em16:
  • :em20:
  • :em15:
  • :em07:
  • :em29:
  • :em02:
  • :em12:
  • :em18:
  • :em10:
  • :em01:
  • :em09:
  • :em22:
  • :em25:

Additional comments powered by BackType