Home > Back-end >  Why devc debugging results and operation results are inconsistent
Why devc debugging results and operation results are inconsistent

Time:01-06

Given N (long range) integers, required output after the smallest to the results,

Subject to test various sorting algorithm under the condition of all kinds of data, each test data characteristics are as follows:

The data 1: only one element;
Data and different integers, basic accuracy test;
Data 3:10 three random integer;
He four random integer data;
Data 5:10 five random integer;
Data 6:10 five order integer;
Data 7:10 5 reverse integer;
Integer data sired five basic order;
5 random positive integer data 9:10, every number less than 1000,
Input format:
Enter the first line gives positive integer N (10 or less
? 5
?? N), then a line given (long range) integers, separated by Spaces,

The output format:
Output grew up in a row after the sorting result, Numbers separated with a space, at the end of the line can not have surplus space,

Input the sample:
11.
4 981 17 0-10 to 20 and 50 8 43-5
The output sample:
17-5 0-20-4 8 October 29 43 50 981

I wrote a merge sort code, the code run directly if there is no problem, but when I in hebin function of k=0; I=left; J=center + 1; Set breakpoints, the next step, the program will sometimes collapse, sometimes the result of the output error, and then I watch the change of each variable in a function, found no change in accordance with the procedures I write ah, is this why??
In general, this code is run directly no problem, when somewhere to set breakpoints, and debugging, constantly click next will appear problem,
You can take
3
3 2 1
The small white try, c language, a great god help me,
 # include & lt; stdio.h> 
Void the sort (int * a, int left, int right);
Void the merge (int * a, int left, int center, int right);
Int main () {
Int n, I;
scanf("%d", & N);
Int the number [n].
for(i=0; iSort (number, 0, n - 1);
Printf (" % d ", the number [0]).
for(i=1; ireturn 0;
}
Void the sort (int * a, int left, int right) {
If (left==right) return;
Int center;
Center=(left + right)/2;
Sort (a, left, center);
Sort (a, center + 1, right);
Merge (a, left, center, right);
}
Void the merge (int * a, int left, int center, int right) {
Int I, j, k;
K=0; I=left; J=center + 1;
Int n=right - left + 1;
Int re [n].
While (1) {
If (a [I] Re=[k++] a [i++];
If (I==center + 1) break;
} else {
Re=[k++] a [j++];
If (j + 1)==right break;
}
}
If (I==center + 1) {
for(; J} else {
for(; i
}
For (I=0, j=left; iA [j++]=re [i++];
}
}


CodePudding user response:

 # include & lt; stdio.h> 
# include & lt; Stdlib. H>

Void the sort (int * a, int left, int right);
Void the merge (int * a, int left, int center, int right);

Int main ()
{
Int n, I;

scanf("%d", & N);
Int * number;

Number=(int *) malloc (sizeof (int) * n);
if (! Number)
return -1;

for(i=0; iscanf("%d", & The number [I]);
Sort (number, 0, n - 1);
//printf (" % d ", number [0]).
//for(i=1; ifor(i=0; iPrintf (" % d ", the number [I]);

Free (number);
return 0;
}

Void the sort (int * a, int left, int right)
{
If (left==right) return;
Int center;
Center=(left + right)/2;
Sort (a, left, center);
Sort (a, center + 1, right);
Merge (a, left, center, right);
}
Void the merge (int * a, int left, int center, int right)
{
Int I, j, k;
K=0; I=left; J=center + 1;
Int n=right - left + 1;
//int re [n].
Int * re;

Re=(int *) malloc (sizeof (int) * n);
if (! Re)
exit(0);

While (1) {
If (a [I]
Re=[k++] a [i++];
If (I==center + 1) break;
} else {
Re=[k++] a [j++];
If (j + 1)==right break;
}
}
If (I==center + 1) {
for(; J} else {
for(; i
}
For (I=0, j=left; iA [j++]=re [i++];
}

Free (re);
}

For your reference ~


Test the temporary not found the problem ~ try the original poster can be more testing

CodePudding user response:

Thank you, I use your code under test, can appear the same problem, that is to say, there is no problem if I run the program directly, but if I were in your program's 42nd set a breakpoint, click next again, the k value is not zero, sometimes equal to 1 k, sometimes equal to 2, also sometimes normal equal to zero, when k is not equal to zero, the subsequent k++ also will not let k plus one at a time, k will be irregular growth up, and sometimes I even did not click next (I just stared at the screen, without any operation), k value will grow up,,, this kind of situation will be the result of the output error, or the program crashes, and you can then try this line 42 set breakpoints, not only I appear this kind of circumstance ,
Emphasis that the program can run normally, only after setting a breakpoint somewhere would be a strange situation,
  • Related