(C ++)我的程序应该让用户输入数字,这些数字将决定数组的大小以及它的元素。然后我需要按升序排列元素。我的问题是每当我运行我的程序并显示已排序的数字;相反,它显示地址(如果我是正确的。)
#include <iostream>
using namespace std;
int main()
{
int size;
int i,x,tempVar;
cout << "Enter how many students took the test: ";
cin >> size;
int *myArray = new int [size];
for ( i = 0; i < size; i++)
{
cout << "Enter a score: ";
cin >> myArray[size];
}
for ( i = 0; i < size; i++)
{
for ( x = 0; x < size; x++)
{
if (myArray[i] < myArray[x])
{
tempVar = myArray[i];
myArray[i] = myArray[x];
myArray[x] = tempVar;
}
}
}
cout << "The scores have been sorted out in an ascending order\n";
for (i = 0; i < size; i++)
{
cout << *(myArray + i) << endl;
}
delete [] myArray;
}
答案 0 :(得分:1)
它没有显示地址;它可能显示垃圾值,因为你有:
cin >> myArray[size];
而不是
cin >> myArray[i];