70,023
社区成员




#include "stdafx.h"
#include <stdio.h>
int main()
{
int i,j,temp;
int a[10];
for(i=0;i<=9;i++)
{
printf("please enter a number\n");
scanf_s("%d",&a[i]);
}
for(j=0;j<=9;j++)
{
for(i=0;i<=9;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
printf("排序后:\n");
for(i=0;i<=9;i++)
printf("%d",a[i]);
return 0;
这个程序在运行时提示数组a[i]被破坏Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
[/quote]上改后的代码[/quote]#include "stdafx.h"
#include <stdio.h>
int main()
{
int i,j,temp;
int a[10];
for(i=0;i<=9;i++)
{
printf("please enter a number\n");
scanf_s("%d",&a[i]);
}
for(j=0;j<=9;j++)
{
for(i=0;i<=9;i++)
{
if(a[i]>a[i-1])
{
temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}
}
}
printf("排序后:\n");
for(i=0;i<=9;i++)
printf("%d",a[i]);
return 0;
}
[/quote]
#include <stdio.h>
int main(int argc, char **argv)
{
int i,j,temp;
int a[10];
for(i=0; i<=9; i++)
{
printf("please enter a number\n");
scanf("%d",&a[i]);
}
for(j=0; j<=9; j++)
{
for(i=0; i<9-j; i++)
{
if(a[i] > a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
printf("排序后:\n");
for(i=0;i<=9;i++)
{
printf("%d",a[i]);
}
return 0;
}
[/quote]请问我的错在哪里,我的目的是要搞清楚哪里错了,避免下次发生类似错误[/quote]
for(i=0;i<=9;i++)
{
if(a[i]>a[i-1])
{
temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}
}
在使用数组时第一要注意的就是你做循环有没有越界产生。 a[i-1] 当i=0 的也是越界了#include "stdafx.h"
#include <stdio.h>
int main()
{
int i,j,temp;
int a[10];
for(i=0;i<=9;i++)
{
printf("please enter a number\n");
scanf_s("%d",&a[i]);
}
for(j=0;j<=9;j++)
{
for(i=0;i<=9;i++)
{
if(a[i]>a[i-1])
{
temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}
}
}
printf("排序后:\n");
for(i=0;i<=9;i++)
printf("%d",a[i]);
return 0;
}
I correct it,but it also can run.Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted
[/quote]
for(i=0;i<=9;i++)
{
if(a[i]>a[i-1])
{
temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}
}
i=0时,a[0]>a[-1][/quote]解决了,谢谢你if(a[i]>a[i-1])
改成这样了,还是有点问题
“冒泡排序.exe”(Win32): 已加载“C:\Users\I love u\Desktop\新建文件夹\冒泡排序\Debug\冒泡排序.exe”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\ntdll.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\kernel32.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\KernelBase.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\msvcr110d.dll”。已加载符号。
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
程序“[7592] 冒泡排序.exe”已退出,返回值为 0 (0x0)。
[/quote]
...因为等于0时,a[i-1] = a[-1],下标错误,细心点哟[/quote]改好了,哎,我太粗行了#include "stdafx.h"
#include <stdio.h>
int main()
{
int i,j,temp;
int a[10];
for(i=0;i<=9;i++)
{
printf("please enter a number\n");
scanf_s("%d",&a[i]);
}
for(j=0;j<=9;j++)
{
for(i=0;i<=9;i++)
{
if(a[i]>a[i-1])
{
temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}
}
}
printf("排序后:\n");
for(i=0;i<=9;i++)
printf("%d",a[i]);
return 0;
}
I correct it,but it also can run.Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted
[/quote]
for(i=0;i<=9;i++)
{
if(a[i]>a[i-1])
{
temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}
}
i=0时,a[0]>a[-1]if(a[i]>a[i-1])
改成这样了,还是有点问题
“冒泡排序.exe”(Win32): 已加载“C:\Users\I love u\Desktop\新建文件夹\冒泡排序\Debug\冒泡排序.exe”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\ntdll.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\kernel32.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\KernelBase.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\msvcr110d.dll”。已加载符号。
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
程序“[7592] 冒泡排序.exe”已退出,返回值为 0 (0x0)。
[/quote]
...因为等于0时,a[i-1] = a[-1],下标错误,细心点哟Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
[/quote]上改后的代码[/quote]#include "stdafx.h"
#include <stdio.h>
int main()
{
int i,j,temp;
int a[10];
for(i=0;i<=9;i++)
{
printf("please enter a number\n");
scanf_s("%d",&a[i]);
}
for(j=0;j<=9;j++)
{
for(i=0;i<=9;i++)
{
if(a[i]>a[i-1])
{
temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}
}
}
printf("排序后:\n");
for(i=0;i<=9;i++)
printf("%d",a[i]);
return 0;
}
[/quote]
#include <stdio.h>
int main(int argc, char **argv)
{
int i,j,temp;
int a[10];
for(i=0; i<=9; i++)
{
printf("please enter a number\n");
scanf("%d",&a[i]);
}
for(j=0; j<=9; j++)
{
for(i=0; i<9-j; i++)
{
if(a[i] > a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
printf("排序后:\n");
for(i=0;i<=9;i++)
{
printf("%d",a[i]);
}
return 0;
}
[/quote]请问我的错在哪里,我的目的是要搞清楚哪里错了,避免下次发生类似错误Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
[/quote]上改后的代码[/quote]#include "stdafx.h"
#include <stdio.h>
int main()
{
int i,j,temp;
int a[10];
for(i=0;i<=9;i++)
{
printf("please enter a number\n");
scanf_s("%d",&a[i]);
}
for(j=0;j<=9;j++)
{
for(i=0;i<=9;i++)
{
if(a[i]>a[i-1])
{
temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}
}
}
printf("排序后:\n");
for(i=0;i<=9;i++)
printf("%d",a[i]);
return 0;
}
[/quote]
#include <stdio.h>
int main(int argc, char **argv)
{
int i,j,temp;
int a[10];
for(i=0; i<=9; i++)
{
printf("please enter a number\n");
scanf("%d",&a[i]);
}
for(j=0; j<=9; j++)
{
for(i=0; i<9-j; i++)
{
if(a[i] > a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
printf("排序后:\n");
for(i=0;i<=9;i++)
{
printf("%d",a[i]);
}
return 0;
}
#include "stdafx.h"
#include <stdio.h>
int main()
{
int i,j,temp;
int a[10];
for(i=0;i<=9;i++)
{
printf("please enter a number\n");
scanf_s("%d",&a[i]);
}
for(j=0;j<=9;j++)
{
for(i=0;i<=9;i++)
{
if(a[i]>a[i-1])
{
temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}
}
}
printf("排序后:\n");
for(i=0;i<=9;i++)
printf("%d",a[i]);
return 0;
}
I correct it,but it also can run.Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted
[/quote]
#include <stdio.h>
int main(int argc, char **argv)
{
int i,j,temp;
int a[10];
for(i=0; i<=9; i++)
{
printf("please enter a number\n");
scanf("%d",&a[i]);
}
for(j=0; j<=9; j++)
{
for(i=0; i<9-j; i++)
{
if(a[i] > a[i+1])
{
temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
}
}
}
printf("排序后:\n");
for(i=0;i<=9;i++)
{
printf("%d",a[i]);
}
return 0;
}
/* j < 9 - i即可,因为9-i后面的数都是已经排好序的较大的数,不用再次比较 */
有道理Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
[/quote]上改后的代码[/quote]#include "stdafx.h"
#include <stdio.h>
int main()
{
int i,j,temp;
int a[10];
for(i=0;i<=9;i++)
{
printf("please enter a number\n");
scanf_s("%d",&a[i]);
}
for(j=0;j<=9;j++)
{
for(i=0;i<=9;i++)
{
if(a[i]>a[i-1])
{
temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}
}
}
printf("排序后:\n");
for(i=0;i<=9;i++)
printf("%d",a[i]);
return 0;
}
#include <stdio.h>
int main(void)
{
int i, j, temp;
int a[10];
int swapped = 1;
for (i = 0; i <= 9; i++) {
printf("please enter a number\n");
scanf("%d", &a[i]);
}
/* 冒泡排序 */
for (i = 0; swapped; i++) {
/* 如果一轮检查下来没有一次交换,则说明数据已经排序排好,不用继续比较 */
swapped = 0;
/* j < 9 - i即可,因为9-i后面的数都是已经排好序的较大的数,不用再次比较 */
for (j = 0; j < 9 - i; j++) {
if (a[j] > a[j + 1]) {
temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
swapped = 1;
}
}
}
printf("排序后:\n");
for (i = 0; i <= 9; i++)
printf("%d ", a[i]);
return 0;
}
if(a[i]>a[i-1])
改成这样了,还是有点问题
“冒泡排序.exe”(Win32): 已加载“C:\Users\I love u\Desktop\新建文件夹\冒泡排序\Debug\冒泡排序.exe”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\ntdll.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\kernel32.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\KernelBase.dll”。已加载符号。
“冒泡排序.exe”(Win32): 已加载“C:\Windows\SysWOW64\msvcr110d.dll”。已加载符号。
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
程序“[7592] 冒泡排序.exe”已退出,返回值为 0 (0x0)。
#include "stdafx.h"
#include <stdio.h>
int main()
{
int i,j,temp;
int a[10];
for(i=0;i<=9;i++)
{
printf("please enter a number\n");
scanf_s("%d",&a[i]);
}
for(j=0;j<=9;j++)
{
for(i=0;i<=9;i++)
{
if(a[i]>a[i-1])
{
temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}
}
}
printf("排序后:\n");
for(i=0;i<=9;i++)
printf("%d",a[i]);
return 0;
}
I correct it,but it also can run.Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted
Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
[/quote]上改后的代码Run-Time Check Failure #2 - Stack around the variable 'a' was corrupted.
a[i]>a[i+1]
这个条件调用了越界的小标,当i=9时