【发布时间】:2018-10-02 09:22:38
【问题描述】:
我真的对这背后的理论感到困惑。不知道如何从我的 isAscending 函数返回数组,以便我可以在 main 中打印出来。
#include <iostream>
#include <string>
using namespace std;
// Implement printArray here
void printArray(int array[], int n){
for (int i = 0; i < n; ++i )
cout << array[i] << endl;
};
// Implement isAscending here
int isAscending(int array[], int n){
for(int i = 0; i <= n; ++i){
for(int j = 0; j <= n; ++j){
if(array[i] > array[j+1]){
int temp = array[j+1];
array[j+1] = array[j];
array[j] = temp;
}
}
}
return printArray(array, n);
};
// DO NOT CHANGE MAIN FUNCTION BELOW
int main() {
int myarray[100];
cout << "Enter number of integers : ";
int n;
cin >> n;
cout << "Enter " << n << " integers" << endl;
for (int i = 0; i < n; i++)
cin >> myarray[i];
cout << "Contents of array : ";
printArray(myarray, n);
cout << "Output of isAscending: " << isAscending(myarray, n) << endl;
}
我应该使用指针来传递数组中的元素吗?
【问题讨论】:
-
您不能在 C++ 中使用
return数组。我猜这是作业,因为关于不更改main的评论,所以你可能只想拥有isAscendingreturnbool(在你的情况下,0 或 1);这将使isAscending成为谓词函数。 -
为什么要更改
isAscending函数中的数组 -
作业到底是什么?逐字。你应该对数组进行排序吗?
-
返回数组使用
std::array。 -
我正在尝试将数组传递给 isascending() 函数对其进行排序以升序打印出内容。