(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;
}
最佳答案
它不显示地址;它可能显示垃圾值,因为您有:
cin >> myArray[size];
代替
cin >> myArray[i];
关于c++ - 指针符号和数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21372212/