用C++对一组数据排序并输出每个元素在原数组中的下标
#include<iostream>#include<iomanip>usingnamespacestd;voidapp(int*a,int*b,intn);intmai...
#include<iostream>#include<iomanip>using namespace std;void app(int*a, int*b, int n);int main(){ int n, i=0; cout << "请输入数组元素个数" << endl; cin >> n; int*a = new int[n]; int*b = new int[n]; cout << "请输入各元素" << endl; while (i <= n) cin >> a[i]; b[i] = i; app(a ,b,n); cout << "排序后:" << endl; for (i = 0; i<n; i++) cout << setw(5) <<a[i]; cout << endl; for (i = 0; i<n; i++) cout << setw(5) << b[i]; cout << endl; system("pause"); }void app(int*a, int*b, int n){ int i, j,t, temp; for (int i = 0; i < n - 1;i++) { t = i; for (int j = i + 1; j < n; j++) if (a[j] < a[t]) t = j; if (t != i) { temp = a[i]; a[i] = a[t]; a[t] = temp; temp = b[i]; b[i] = b[t]; b[t] = temp; } } return;}
这是我编写的,程序运行之后
为什么会之后的程序运行不了啊?????QAQ求高手帮忙,在线等 展开
这是我编写的,程序运行之后
为什么会之后的程序运行不了啊?????QAQ求高手帮忙,在线等 展开
3个回答
展开全部
#include<iostream>
#include<iomanip>
using namespace std;
void app(int*a, int*b, int n);
int main()
{
int n, i=0;
cout << "请输入数组元素个数" << endl;
cin >> n;
int*a = new int[n];
int*b = new int[n];
cout << "请输入各元素" << endl;
while (i < n)
{
cin >> a[i];
b[i] = i;
i++;
}
app(a ,b,n);
cout << "排序后:" << endl;
for (i = 0; i<n; i++)
cout << setw(5) <<a[i];
cout << endl;
for (i = 0; i<n; i++)
cout << setw(5) << b[i];
cout << endl;
delete [] a;
delete [] b;
system("pause");
return 0;
}
void app(int*a, int*b, int n)
{
int i, j, t, temp;
for (int i = 0; i < n - 1;i++)
{
t = i;
for (int j = i + 1; j < n; j++)
if (a[j] < a[t]) t = j;
if (t != i)
{
temp = a[i];
a[i] = a[t];
a[t] = temp;
temp = b[i];
b[i] = b[t];
b[t] = temp;
}
}
return;
}
修改如上!
while循环有点问题,还有new了之后要delete,main需要return。基本上就这些吧。。。
2014-12-03
展开全部
你的基础上改的
#include<iostream>
#include<iomanip>
using namespace std;
void app(int*a, int*b, int n);
int main()
{
int n, i=0;
cout << "请输入数组元素个数" << endl;
cin >> n;
int*a = new int[n];
int*b = new int[n];
cout << "请输入各元素" << endl;
while (i < n)
{
cin >> a[i];
b[i] = i;
i++;
}
app(a,b,n);
cout << "排序后:" << endl;
for (i = 0; i<n; i++)
cout << setw(5) <<a[i];
cout << endl;
for (i = 0; i<n; i++)
cout << setw(5) << b[i];
cout << endl;
system("pause");
return 0;
}
void app(int*a, int*b, int n)
{
int i, j,t, temp;
for (i = 0; i < n - 1;i++)
{
t = i;
for (j = i + 1; j < n; j++)
if (a[j] < a[t]) t = j;
if (t != i)
{
temp = a[i];
a[i] = a[t];
a[t] = temp;
temp = b[i];
b[i] = b[t];
b[t] = temp;
}
}
}
你的主函数是int的没有返回值 赋值过程也错了 app函数变量i跟j重定义了。你参考参考吧
#include<iostream>
#include<iomanip>
using namespace std;
void app(int*a, int*b, int n);
int main()
{
int n, i=0;
cout << "请输入数组元素个数" << endl;
cin >> n;
int*a = new int[n];
int*b = new int[n];
cout << "请输入各元素" << endl;
while (i < n)
{
cin >> a[i];
b[i] = i;
i++;
}
app(a,b,n);
cout << "排序后:" << endl;
for (i = 0; i<n; i++)
cout << setw(5) <<a[i];
cout << endl;
for (i = 0; i<n; i++)
cout << setw(5) << b[i];
cout << endl;
system("pause");
return 0;
}
void app(int*a, int*b, int n)
{
int i, j,t, temp;
for (i = 0; i < n - 1;i++)
{
t = i;
for (j = i + 1; j < n; j++)
if (a[j] < a[t]) t = j;
if (t != i)
{
temp = a[i];
a[i] = a[t];
a[t] = temp;
temp = b[i];
b[i] = b[t];
b[t] = temp;
}
}
}
你的主函数是int的没有返回值 赋值过程也错了 app函数变量i跟j重定义了。你参考参考吧
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
死循环了啊,下面这句:
while (i <= n)
cin >> a[i];
i一直小于n,就一直在输入状态,卡死了。应该加上
while (i++<= n)
{
cin >> a[i];
b[i] = i;
}
把下面的语句括起来,应该就可以了。
while (i <= n)
cin >> a[i];
i一直小于n,就一直在输入状态,卡死了。应该加上
while (i++<= n)
{
cin >> a[i];
b[i] = i;
}
把下面的语句括起来,应该就可以了。
追问
QAQ还是不行哇
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |