c++编程!题目:用new运算符动态分配一个数组,给该数组赋初值,然后将该数组排序并输出。 下面哪里错了?

#include <iostream>
using namespace std;
int main( )
{
int *str[10] = {NULL };
str[10] = new int [10];
srand(time(0)); //改变随机数序列
for ( int i=0; i<10; ++i)
{
*str[i]=rand()%101; //生成10个0-100之间的随机数
}
cout << "排列前得数组为:";
for(int i = 0; i < 10 ; ++i)
{
cout << *str[i] << " ";
}
cout << endl;
for( int i = 0; i < 9 ; ++i)
{
for( int j = i+1; j < 10 ; ++j)
{
if( *str[i] < *str[j])
{
int temp = *str[j];
*str [j] = *str[i];
*str[i] = temp;
}
}
}
cout << "排列后的数组为:";
for( int i = 0; i < 10 ; ++i)
{
cout << *str[i] << " ";
}
return 0;
}
我一运行就出现程序错误!

#include <iostream>
using namespace std;
int main( )
{
int *str = NULL ;
str = new int [10];
srand(time(0)); //改变随机数序列
for ( int i=0; i<10; ++i)
{
str[i]=rand()%101; //生成10个0-100之间的随机数
}
cout << "排列前得数组为:";
for(int i = 0; i < 10 ; ++i)
{
cout << str[i] << " ";
}
cout << endl;
for( int i = 0; i < 9 ; ++i)
{
for( int j = i+1; j < 10 ; ++j)
{
if( str[i] < str[j])
{
int temp = str[j];
str [j] = str[i];
str[i] = temp;
}
}
}
cout << "排列后的数组为:";
for( int i = 0; i < 10 ; ++i)
{
cout << str[i] << " ";
}
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2011-05-19
关键在这里开始赋值错:*str[i]=rand()%101; //生成10个0-100之间的随机数追问

按你说的改不行吖!
我的程序没提示错误,只是一运行 整个平台就错误了!
是不是有逻辑错误吖?!

追答

#include
#include
#include
using namespace std;
int main( )
{
int str[10] = {NULL };
//str[10] = new int [10];
srand(time(0)); //改变随机数序列
for ( int i=0; i<10; ++i)
{
str[i]=rand()%101; //生成10个0-100之间的随机数
}
cout << "排列前得数组为:";
for(int i = 0; i < 10 ; ++i)
{
cout << str[i] << " ";
}
cout << endl;
for( int i = 0; i < 9 ; ++i)
{
for( int j = i+1; j < 10 ; ++j)
{
if( str[i] < str[j])
{
int temp = str[j];
str [j] = str[i];
str[i] = temp;
}
}
}
cout << "排列后的数组为:";
for( int i = 0; i < 10 ; ++i)
{
cout << str[i] << " ";
}
return 0;
}

本回答被提问者采纳

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网