我有一系列数字,我试图扭转.我相信我的代码中的函数是正确的,但我无法得到正确的输出.
输出显示:10 9 8 7 6.为什么我不能得到另一半的数字?当我从计数中删除"/ 2"时,输出显示为:10 9 8 7 6 6 7 8 9 10
void reverse(int [], int);
int main ()
{
const int SIZE = 10;
int arr [SIZE] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
reverse(arr, SIZE);
return 0;
}
void reverse(int arr[], int count)
{
int temp;
for (int i = 0; i < count/2; ++i)
{
arr[i] = temp;
temp = arr[count-i-1];
arr[count-i-1] = arr[i];
arr[i] = temp;
cout << …Run Code Online (Sandbox Code Playgroud) 我必须编写一个C++代码来查找数组的中位数和模式.我被告知在数字排序后找到阵列模式要容易得多.我对功能进行了排序,但仍无法找到该模式.
int counter = 0;
for (int pass = 0; pass < size - 1; pass++)
for (int count = pass + 1; count < size; count++) {
if (array [count] == array [pass])
counter++;
cout << "The mode is: " << counter << endl;
Run Code Online (Sandbox Code Playgroud) 初学者在这里试图理解函数的基本原理,传递我的引用和向量/数组.我的代码将大数据文件读入矢量.然后我不知何故需要将矢量转换为数组,对数组进行排序,并读取输出.我相信我的问题在于我尝试将矢量转换为数组.
using namespace std;
//function prototype
int readInput(vector<int> &vect);
void sort(int[], int);
void showArray(int[], int);
int main()
{
vector<int> values;
int sum, avg;
sum = readInput(values);
const int SIZE = values.size(); //ISSUE LIES HERE
int arr[SIZE]; //and here
sort(arr, SIZE);
showArray(arr, SIZE);
avg = sum / values.size();
//cout << "The average is: " << avg;
return 0;
}
int readInput(vector<int> &vect)
{
int count;
int total = 0;
ifstream inputFile("TopicFin.txt"); //open file
if(!inputFile)
{
return 0; // if file is …Run Code Online (Sandbox Code Playgroud)