首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++课本上的一道例题,能运行,却提示“内存不能Read”,为哪般?该怎么处理

2012-06-11 
C++课本上的一道例题,能运行,却提示“内存不能Read”,为哪般?求帮助!!![codeC/C++][/code]//运用直接 选择

C++课本上的一道例题,能运行,却提示“内存不能Read”,为哪般?

求帮助!!!

[code=C/C++][/code]
//运用直接 选择排序法对整数数组元素按照从小到大顺序排序
#include<iostream.h>
#define SIZE 8
int main()
{
  int a[SIZE]={18,35,36,61,9,112,77,12};
   
  //输出原数组
  for(int i=0;i<=SIZE-1;i++)
  cout<<a[i]<<endl;
  //对数组进行排序 
  for(int pass=0;pass<=SIZE-1;pass++)
  {
   
  for(int j=pass+1;pass<=SIZE-1;j++)
  if(a[pass]>a[j])
  {
  int hold;  
  //交换  
  hold=a[pass];
  a[pass]=a[j];  
  a[j]=hold;
   
  }  
  //输出当前的结果
  cout<<"After NO."<<pass+1<<"scan:";
  for(int index=0;index<=SIZE-1;index++)
  if(index==pass+1) //index的值是输出元素位置,pass是输出的行数。 
  cout<<"\t"<<"["<<a[index]; 
   
  else
  cout<<"\t"<<a[index];
   
  cout<<"]"<<endl;
   
   
  }  
   
  cout<<"After sorting:\t";
   
  for(int index=0;index<=SIZE-1;index++)
  cout<<"\t"<<a[index];
  cout<<endl;
   
  return 0;  

}

[解决办法]
数组越界了
[解决办法]

C/C++ code
//对数组进行排序      for(int pass=0;pass<=SIZE-1;pass++)    {                for(int j=pass+1;pass<=SIZE-1;j++)//这个地方应该是 j<=SIZE-1
[解决办法]
C/C++ code
  //对数组进行排序    for(int pass=0;pass<=SIZE-1;pass++)  {      for(int j=pass+1;pass<=SIZE-1;j++)//没仔细看, pass <= SIZE-1改为,j <= SIZE-1  if(a[pass]>a[j])  {  int hold;     //交换     hold=a[pass];  a[pass]=a[j];     a[j]=hold;      }
[解决办法]
“多一少一”问题占程序员常犯错误的10%以上!

[解决办法]
呵呵,千万注意

热点排行