#include <iostream>
using namespace std;
int main()
{
int i,sum=0,n;
int a[10];
float avg;
cout<<"Enter how many numbers you want ";
cin>>n;
if (n>10)
n=10;
cout<<"Enter the numbers" << endl;
for (i=0;i<n;i++)
cin>>a[i];
for (i=0;i<n;i++)
{
sum=sum+a[i];
}
avg=sum/n;
cout<<"sum of array elements "<<sum << endl;
cout<<"average of array elements " <<avg << endl;
int temp;
for (int i =0; i<n; i++)
{
for (int j=1; j<n; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
cout << "The numbers in ascending order are:" << endl;
for (int i =0; i<n; i++)
{
cout << a[i] << endl;
}
return 0;
}
当我运行此程序时,数字会以不同的顺序打印出来。
如果我使用数字1 2 3 4 5.它们打印为1 5 4 3 2。
其他一切都运行良好。如何解决此错误?
答案 0 :(得分:2)