Bonjour,
J'ai un petit exercice de C++ à faire où on me demande de créer un array A avec certaines valeurs, puis de créer un deuxième array B. Il faut transposer A dans B puis trier à l'intérieur de B. Enfin, il faut créer un pointeur C sur B, et utiliser l'arithmétique des pointeurs pour afficher le contenu de B.
Mon code tourne et s'affiche mais j'obtiens à la toute fin, une erreur: "Run-Time check failure#2 - Stack around the variable 'A' was corrupted".
Voici mon code:
Pouvez-vous m'aider à interpréter et corriger cette erreur?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 #include <iostream> using namespace std; int main() { //declare the new array and initialize it double A[10] = { 5.6,3.2,1.0,199,32,5.7,9.9,11.0,999,0.0001 }; //declare the new array B with 10 elements double B[10]; //declare a temporary variable to store before swap double sp_before; cout << "Exercise" << endl; //copy all the elements of A into B for (int i = 0; i<sizeof(A); i++) { B[i] = A[i]; } //Sort B using the bubblesort provided and translated into C++ code double n = sizeof(A)/sizeof(A[0]); //length of A bool swapped=true; while (swapped != false) { swapped = false; for (int i = 1; i<n; i++) { if (B[i - 1]>B[i]) { //we swap sp_before = B[i - 1]; B[i - 1] = B[i]; B[i] = sp_before; swapped = true; } } } //Loop to verify the elements in B... for (int i = 0; i<n ; i++) { cout << B[i] << endl; } //Create pointer C to B double *C = B; //create and assign pointer C to array B //display using pointer's arithmetic for (int i = 0; i < n; i++) { cout << "Value of the " << i << "th element of B is: " << *C << endl; C++; } return 0; }
D'avance merci.
Partager