
帮忙看下这个程序为何运行出错 [有高分]
这个程序要求:对两个数组(都是10个int型的),先进行选择排序,再进行归并排序.下面代码可以运行出结果,但出结果的同时也错现DebugError,错误提示为Theval...
这个程序要求:对两个数组(都是10个int型的),先进行选择排序,再进行归并排序.
下面代码可以运行出结果,但出结果的同时也错现Debug Error,错误提示为
The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
我没找出哪里有错啊,请您指正!!
代码如下:
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
void generate(int array1[],int array2[],int n)
{
int i=0,j=0;
srand((unsigned)time(NULL));
while(j++<20)
{
if (j==10)
i=0;
if (j<10)
array1[i++]=rand()%100;
else
array2[i++]=rand()%100;
}
}
void display(int array[],int n)
{
for(int i=0;i<n;i++)
cout<<array[i]<<"\t";
}
void sort(int array[],int n)//选择排序法将归并前的两个数组排序
{
int i=0,j=0,miniindex=0;
int temp=0;
for(i=0;i<n-1;i++)
{
miniindex=i;
for(j=i+1;j<n;j++)
if (array[j]<array[miniindex])
miniindex=j;
temp=array[miniindex];
array[miniindex]=array[i];
array[i]=temp;
}
}
void guibing(int b1[],int b2[],int count1,int count2)
{
int b3[20];
int i=0,j=0,k=0; //i记b1[],j记b2[],k记b3[]
while(i!=count1&&j!=count2)
{ if (b1[i]>=b2[j])
{
b3[k]=b2[j];
++j;
++k;
}
else
{
b3[k]=b1[i];
++i;
++k;
}
}
if(i==count1)
while(j<=count2)
{
b3[k]=b2[j];
++k;
++j;
}
if(j==count2)
while(i<=count1)
{
b3[k]=b1[i];
++k;
++i;
}
cout<<"result:";
for(i=0;i<20;i++)
cout<<b3[i]<<" ";
cout<<endl;
}
void main()
{
const int N=10;
int A[N]={0};
int B[N]={0};
int i=0,j=0;
generate(A,B,N);
cout<<"array1:"<<endl;
display(A,10);
cout<<"array2:"<<endl;
display(B,10);
sort(A,N);
sort(B,N);
guibing(A,B,N,N);
}
哦还真是,这是根据以前的一个程序改写的,忽略了这点.
还有一点不明,就是生成随机数那个函数,若写成现在这样,第一个数组最后一个数总是0,这哪是"随机啊",为什么?若写成下面倒是真的随机了,这是什么道理?
void generate(int array1[],int array2[],int n)
{
int i=0,j=0;
srand((unsigned)time(NULL));
while(j<20) //有改动j++<20改为j<20
{
if (j==10)
i=0;
if (j<10)
array1[i++]=rand()%100;
else
array2[i++]=rand()%100;
j++;//放到这里
}
}
re:知道补充问题的原因了,写成while(j++<20)时,循环里j的值从1到20,分开写时j从0到19(不等价于while(++j<20))
这样就对了
void generate(int array1[],int array2[],int n)
{
int i=0,j=0;
srand((unsigned)time(NULL));
while(j++<20)
{
if (j==11)
i=0;
if (j<11)
array1[i++]=rand()%100;
else
array2[i++]=rand()%100;
}
} 展开
下面代码可以运行出结果,但出结果的同时也错现Debug Error,错误提示为
The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
我没找出哪里有错啊,请您指正!!
代码如下:
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
void generate(int array1[],int array2[],int n)
{
int i=0,j=0;
srand((unsigned)time(NULL));
while(j++<20)
{
if (j==10)
i=0;
if (j<10)
array1[i++]=rand()%100;
else
array2[i++]=rand()%100;
}
}
void display(int array[],int n)
{
for(int i=0;i<n;i++)
cout<<array[i]<<"\t";
}
void sort(int array[],int n)//选择排序法将归并前的两个数组排序
{
int i=0,j=0,miniindex=0;
int temp=0;
for(i=0;i<n-1;i++)
{
miniindex=i;
for(j=i+1;j<n;j++)
if (array[j]<array[miniindex])
miniindex=j;
temp=array[miniindex];
array[miniindex]=array[i];
array[i]=temp;
}
}
void guibing(int b1[],int b2[],int count1,int count2)
{
int b3[20];
int i=0,j=0,k=0; //i记b1[],j记b2[],k记b3[]
while(i!=count1&&j!=count2)
{ if (b1[i]>=b2[j])
{
b3[k]=b2[j];
++j;
++k;
}
else
{
b3[k]=b1[i];
++i;
++k;
}
}
if(i==count1)
while(j<=count2)
{
b3[k]=b2[j];
++k;
++j;
}
if(j==count2)
while(i<=count1)
{
b3[k]=b1[i];
++k;
++i;
}
cout<<"result:";
for(i=0;i<20;i++)
cout<<b3[i]<<" ";
cout<<endl;
}
void main()
{
const int N=10;
int A[N]={0};
int B[N]={0};
int i=0,j=0;
generate(A,B,N);
cout<<"array1:"<<endl;
display(A,10);
cout<<"array2:"<<endl;
display(B,10);
sort(A,N);
sort(B,N);
guibing(A,B,N,N);
}
哦还真是,这是根据以前的一个程序改写的,忽略了这点.
还有一点不明,就是生成随机数那个函数,若写成现在这样,第一个数组最后一个数总是0,这哪是"随机啊",为什么?若写成下面倒是真的随机了,这是什么道理?
void generate(int array1[],int array2[],int n)
{
int i=0,j=0;
srand((unsigned)time(NULL));
while(j<20) //有改动j++<20改为j<20
{
if (j==10)
i=0;
if (j<10)
array1[i++]=rand()%100;
else
array2[i++]=rand()%100;
j++;//放到这里
}
}
re:知道补充问题的原因了,写成while(j++<20)时,循环里j的值从1到20,分开写时j从0到19(不等价于while(++j<20))
这样就对了
void generate(int array1[],int array2[],int n)
{
int i=0,j=0;
srand((unsigned)time(NULL));
while(j++<20)
{
if (j==11)
i=0;
if (j<11)
array1[i++]=rand()%100;
else
array2[i++]=rand()%100;
}
} 展开
4个回答
展开全部
程序访问数组时越界了,改写了程序的代码段,结果出错了
修正如下,错误处有注释说明:
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
void generate(int array1[],int array2[],int n)
{
int i=0,j=0;
srand((unsigned)time(NULL));
while(j++<20)
{
if (j==10)
i=0;
if (j<10)
array1[i++]=rand()%100;
else
array2[i++]=rand()%100;
}
}
void display(int array[],int n)
{
for(int i=0;i<n;i++)
cout<<array[i]<<"\t";
}
void sort(int array[],int n)//选择排序法将归并前的两个数组排序
{
int i=0,j=0,miniindex=0;
int temp=0;
for(i=0;i<n-1;i++)
{
miniindex=i;
for(j=i+1;j<n;j++)
if (array[j]<array[miniindex])
miniindex=j;
temp=array[miniindex];
array[miniindex]=array[i];
array[i]=temp;
}
}
void guibing(int b1[],int b2[],int count1,int count2)
{
int b3[20];
int i=0,j=0,k=0; //i记b1[],j记b2[],k记b3[]
while(i!=count1&&j!=count2)
{
if (b1[i]>=b2[j])
{
b3[k]=b2[j];
++j;
++k;
}
else
{
b3[k]=b1[i];
++i;
++k;
}
}
if(i==count1)
while(j<count2) //这里j最多只到19
{
b3[k]=b2[j];
++k;
++j;
}
if(j==count2)
while(i<count1) //这里i最多只到19
{
b3[k]=b1[i];
++k;
++i;
}
cout<<"result:";
for(i=0;i<20;i++)
cout<<b3[i]<<" ";
cout<<endl;
}
void main()
{
const int N=10;
int A[N]={0};
int B[N]={0};
int i=0,j=0;
generate(A,B,N);
cout<<"array1:"<<endl;
display(A,10);
cout<<"array2:"<<endl;
display(B,10);
sort(A,N);
sort(B,N);
guibing(A,B,N,N);
}
---------------------------------------
修改后效果等价于while(++j<20) //j先自增再比较
这样最后一个元素没有赋值,可能你的编译器里将没有赋值的地方用0填充了
修正如下,错误处有注释说明:
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
void generate(int array1[],int array2[],int n)
{
int i=0,j=0;
srand((unsigned)time(NULL));
while(j++<20)
{
if (j==10)
i=0;
if (j<10)
array1[i++]=rand()%100;
else
array2[i++]=rand()%100;
}
}
void display(int array[],int n)
{
for(int i=0;i<n;i++)
cout<<array[i]<<"\t";
}
void sort(int array[],int n)//选择排序法将归并前的两个数组排序
{
int i=0,j=0,miniindex=0;
int temp=0;
for(i=0;i<n-1;i++)
{
miniindex=i;
for(j=i+1;j<n;j++)
if (array[j]<array[miniindex])
miniindex=j;
temp=array[miniindex];
array[miniindex]=array[i];
array[i]=temp;
}
}
void guibing(int b1[],int b2[],int count1,int count2)
{
int b3[20];
int i=0,j=0,k=0; //i记b1[],j记b2[],k记b3[]
while(i!=count1&&j!=count2)
{
if (b1[i]>=b2[j])
{
b3[k]=b2[j];
++j;
++k;
}
else
{
b3[k]=b1[i];
++i;
++k;
}
}
if(i==count1)
while(j<count2) //这里j最多只到19
{
b3[k]=b2[j];
++k;
++j;
}
if(j==count2)
while(i<count1) //这里i最多只到19
{
b3[k]=b1[i];
++k;
++i;
}
cout<<"result:";
for(i=0;i<20;i++)
cout<<b3[i]<<" ";
cout<<endl;
}
void main()
{
const int N=10;
int A[N]={0};
int B[N]={0};
int i=0,j=0;
generate(A,B,N);
cout<<"array1:"<<endl;
display(A,10);
cout<<"array2:"<<endl;
display(B,10);
sort(A,N);
sort(B,N);
guibing(A,B,N,N);
}
---------------------------------------
修改后效果等价于while(++j<20) //j先自增再比较
这样最后一个元素没有赋值,可能你的编译器里将没有赋值的地方用0填充了
展开全部
自己多冻手 问是不行的 编译调试出错在编辑靠自己的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;//!!don't forget this ....^_^
#define NumberOfArray 10 //this part is equal to the const int N =10;
// this function will get the values of the Array numbers from the computer in random;
void generate(int array1[],int array2[])
{
int i=0;
int j=0;
srand((unsigned)time(NULL));
while(j<(2*NumberOfArray))
{
if (j==NumberOfArray)
i=0;
if (j<NumberOfArray) {
array1[i]=rand()%100;
i++;
j++;
}
else {
array2[i]=rand()%100;
i++;
j++;
}
}
}
void display(int array[],int n)
{
for(int i=0;i<n;i++)
cout <<array[i]<<"\t";
}
//the numbers of the array are in increase order
void sort(int array[],int n)//选择排序法将归并前的两个数组排序
{
int i,j;
int temp;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++){
if(array[j]>array[j+1]){
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
void guibing(int b1[],int b2[],int count1,int count2)
{
int b3[2*NumberOfArray];
int i=0,j=0,k=0; //i记b1[],j记b2[],k记b3[]
while(i!=count1&&j!=count2) // Either of the two array end the while will end
{
if (b1[i]>=b2[j])
{
b3[k]=b2[j];
j++;
k++;
}
else
{
b3[k]=b1[i];
i++;
k++;
}
}
if(i==count1)
// while(j<=count1) j can equal to 10 ;but b1[10]is not the element of the array,max 9
while(j<count2)
{
b3[k]=b2[j];
++k;
++j;
}
if(j==count2)
// while(i<=count1) i can equal to 10 ;but b1[10]is not the element of the array,max 9
while(i<count1)
{
b3[k]=b1[i];
++k;
++i;
}
cout<<"result:";
for(i=0;i<20;i++)
cout<<b3[i]<<" ";
cout<<endl;
}
int main() //the type of the main function's return value should be int
{
// const int N=10;
int A[NumberOfArray]={0}; //creat a int Array filled with 0
int B[NumberOfArray]={0}; //creat a int Array filled with 0
// int i=0,j=0; useful?
generate(A,B); // this function will get the values of the Array numbers from the computer in randrom;
cout<<"array1:"<<endl;
display(A,NumberOfArray);
cout<<"array2:"<<endl;
display(B,NumberOfArray);
sort(A,NumberOfArray); //the numbers of the array are in increase order
//display(A,NumberOfArray); just for the test
sort(B,NumberOfArray); //the numbers of the array are in increase order
//display(B,NumberOfArray); just for the test
guibing(A,B,NumberOfArray,NumberOfArray); //why don't return a int pointer pointed to space containing the guibed array,..heihei ...^-^
system("pause");
}
如果 还有 问题,。。在 问题补充 中 说明
#include <stdlib.h>
#include <time.h>
using namespace std;//!!don't forget this ....^_^
#define NumberOfArray 10 //this part is equal to the const int N =10;
// this function will get the values of the Array numbers from the computer in random;
void generate(int array1[],int array2[])
{
int i=0;
int j=0;
srand((unsigned)time(NULL));
while(j<(2*NumberOfArray))
{
if (j==NumberOfArray)
i=0;
if (j<NumberOfArray) {
array1[i]=rand()%100;
i++;
j++;
}
else {
array2[i]=rand()%100;
i++;
j++;
}
}
}
void display(int array[],int n)
{
for(int i=0;i<n;i++)
cout <<array[i]<<"\t";
}
//the numbers of the array are in increase order
void sort(int array[],int n)//选择排序法将归并前的两个数组排序
{
int i,j;
int temp;
for(i=0;i<n;i++)
for(j=0;j<n-i-1;j++){
if(array[j]>array[j+1]){
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
void guibing(int b1[],int b2[],int count1,int count2)
{
int b3[2*NumberOfArray];
int i=0,j=0,k=0; //i记b1[],j记b2[],k记b3[]
while(i!=count1&&j!=count2) // Either of the two array end the while will end
{
if (b1[i]>=b2[j])
{
b3[k]=b2[j];
j++;
k++;
}
else
{
b3[k]=b1[i];
i++;
k++;
}
}
if(i==count1)
// while(j<=count1) j can equal to 10 ;but b1[10]is not the element of the array,max 9
while(j<count2)
{
b3[k]=b2[j];
++k;
++j;
}
if(j==count2)
// while(i<=count1) i can equal to 10 ;but b1[10]is not the element of the array,max 9
while(i<count1)
{
b3[k]=b1[i];
++k;
++i;
}
cout<<"result:";
for(i=0;i<20;i++)
cout<<b3[i]<<" ";
cout<<endl;
}
int main() //the type of the main function's return value should be int
{
// const int N=10;
int A[NumberOfArray]={0}; //creat a int Array filled with 0
int B[NumberOfArray]={0}; //creat a int Array filled with 0
// int i=0,j=0; useful?
generate(A,B); // this function will get the values of the Array numbers from the computer in randrom;
cout<<"array1:"<<endl;
display(A,NumberOfArray);
cout<<"array2:"<<endl;
display(B,NumberOfArray);
sort(A,NumberOfArray); //the numbers of the array are in increase order
//display(A,NumberOfArray); just for the test
sort(B,NumberOfArray); //the numbers of the array are in increase order
//display(B,NumberOfArray); just for the test
guibing(A,B,NumberOfArray,NumberOfArray); //why don't return a int pointer pointed to space containing the guibed array,..heihei ...^-^
system("pause");
}
如果 还有 问题,。。在 问题补充 中 说明
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
程序访问数组时越界了,改写了程序的代码段,结果出错了
修正如下,错误处有注释说明:
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
void
generate(int
array1[],int
array2[],int
n)
{
int
i=0,j=0;
srand((unsigned)time(NULL));
while(j++<20)
{
if
(j==10)
i=0;
if
(j<10)
array1[i++]=rand()%100;
else
array2[i++]=rand()%100;
}
}
void
display(int
array[],int
n)
{
for(int
i=0;i<n;i++)
cout<<array[i]<<"\t";
}
void
sort(int
array[],int
n)//选择排序法将归并前的两个数组排序
{
int
i=0,j=0,miniindex=0;
int
temp=0;
for(i=0;i<n-1;i++)
{
miniindex=i;
for(j=i+1;j<n;j++)
if
(array[j]<array[miniindex])
miniindex=j;
temp=array[miniindex];
array[miniindex]=array[i];
array[i]=temp;
}
}
void
guibing(int
b1[],int
b2[],int
count1,int
count2)
{
int
b3[20];
int
i=0,j=0,k=0;
//i记b1[],j记b2[],k记b3[]
while(i!=count1&&j!=count2)
{
if
(b1[i]>=b2[j])
{
b3[k]=b2[j];
++j;
++k;
}
else
{
b3[k]=b1[i];
++i;
++k;
}
}
if(i==count1)
while(j<count2)
//这里j最多只到19
{
b3[k]=b2[j];
++k;
++j;
}
if(j==count2)
while(i<count1)
//这里i最多只到19
{
b3[k]=b1[i];
++k;
++i;
}
cout<<"result:";
for(i=0;i<20;i++)
cout<<b3[i]<<"
";
cout<<endl;
}
void
main()
{
const
int
N=10;
int
A[N]={0};
int
B[N]={0};
int
i=0,j=0;
generate(A,B,N);
cout<<"array1:"<<endl;
display(A,10);
cout<<"array2:"<<endl;
display(B,10);
sort(A,N);
sort(B,N);
guibing(A,B,N,N);
}
---------------------------------------
修改后效果等价于while(++j<20)
//j先自增再比较
这样最后一个元素没有赋值,可能你的编译器里将没有赋值的地方用0填充了
修正如下,错误处有注释说明:
#include<iostream.h>
#include<stdlib.h>
#include<time.h>
void
generate(int
array1[],int
array2[],int
n)
{
int
i=0,j=0;
srand((unsigned)time(NULL));
while(j++<20)
{
if
(j==10)
i=0;
if
(j<10)
array1[i++]=rand()%100;
else
array2[i++]=rand()%100;
}
}
void
display(int
array[],int
n)
{
for(int
i=0;i<n;i++)
cout<<array[i]<<"\t";
}
void
sort(int
array[],int
n)//选择排序法将归并前的两个数组排序
{
int
i=0,j=0,miniindex=0;
int
temp=0;
for(i=0;i<n-1;i++)
{
miniindex=i;
for(j=i+1;j<n;j++)
if
(array[j]<array[miniindex])
miniindex=j;
temp=array[miniindex];
array[miniindex]=array[i];
array[i]=temp;
}
}
void
guibing(int
b1[],int
b2[],int
count1,int
count2)
{
int
b3[20];
int
i=0,j=0,k=0;
//i记b1[],j记b2[],k记b3[]
while(i!=count1&&j!=count2)
{
if
(b1[i]>=b2[j])
{
b3[k]=b2[j];
++j;
++k;
}
else
{
b3[k]=b1[i];
++i;
++k;
}
}
if(i==count1)
while(j<count2)
//这里j最多只到19
{
b3[k]=b2[j];
++k;
++j;
}
if(j==count2)
while(i<count1)
//这里i最多只到19
{
b3[k]=b1[i];
++k;
++i;
}
cout<<"result:";
for(i=0;i<20;i++)
cout<<b3[i]<<"
";
cout<<endl;
}
void
main()
{
const
int
N=10;
int
A[N]={0};
int
B[N]={0};
int
i=0,j=0;
generate(A,B,N);
cout<<"array1:"<<endl;
display(A,10);
cout<<"array2:"<<endl;
display(B,10);
sort(A,N);
sort(B,N);
guibing(A,B,N,N);
}
---------------------------------------
修改后效果等价于while(++j<20)
//j先自增再比较
这样最后一个元素没有赋值,可能你的编译器里将没有赋值的地方用0填充了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询