指针符号和数组

Pointer Notation and Array

本文关键字:数组 符号 指针      更新时间:2023-10-16

(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 ordern";
for (i = 0; i < size; i++)
{
    cout << *(myArray + i) << endl;
}
delete [] myArray;
}

它没有显示地址;由于您有:

,它可能显示出垃圾值
cin >> myArray[size];

而不是

cin >> myArray[i];