如何反转 Cpp 的数组

How to reverse the array for Cpp

本文关键字:数组 Cpp 何反转      更新时间:2023-10-16

我正在尝试命令我的程序反转一组随机数组数字。我让我的程序想出 10 位数字(范围从 60-100),当生成随机数组时,我让它给我 4 个选项;前任:

66

75 84 93 82 61 66 99 85 93

R - 用于反向。(这将反转数组设置为 [93 85 99 66...84 75 66])

S - 用于搜索。(这将提示您搜索一个数字并读取它将位于哪一行。

E - 用于退出。(退出程序)

A - 用于添加。(这将把所有随机数组编号集相加。

一切正常,但我遇到的唯一问题是它不会反转随机生成的数组集。我以为我提升了正确的命令。我需要这方面的帮助,请记住我是C++新手。

#include <iostream>
#include <stdlib.h>
#include <time.h>
 using namespace std;
 int main ()
 {
  srand(time(0));
  int arr[10],i,sum;
  bool found;
  char choice;
  for (i = 0; i < 10; i++){
    arr[i] = rand() % 30 + 60;
   }

cout << "The random generated array is: n";
for (i = 0; i <10; i++){
    cout << arr[i] << "  ";
}
cout << "nnR[reverse]t[S]searcht[E]exittt[A]addnSelect an option: ";
cin >> choice;
switch (choice){
main ();
    case 'R':
    case 'r':
        cout << "nThe reversed array is: ";
            for (i = 9; i >=0; i--)
            cout << endl << endl << "------------------------------" << endl;
   main ();
       case 'A':
       case 'a':
        cout << "nThe sum of the array element is ";
        sum = 0;
            for (i = 0; i < 10; i++) sum += arr[i];
            cout << sum << "n";
            cout << endl << endl << "----------------------"    << endl;
   main ();
        case 'S':
        case 's':
        cout << "nPlease insert an element to find: ";
        int find;
        found=false;
        cin >> find;
            for (i = 0; i<10; i++){
                if(arr[i] == find){
                    if(i ==0)
            cout << "nThe number " << find << " has been found at the 1st position" << endl; 
                else if(i == 1) cout << "nThe number " << find << " has been found at the 2nd position" << endl;
                else if(i == 2) cout << "nThe number "  << find << " has been found at the 3rd positon" << endl;
                else if(i == 3) cout << "nThe number " << find << " has been found at the 4th position" << endl;
                else if(i == 4) cout << "nThe number " << find << " has been found at the 5th position" << endl;
                else if(i == 5) cout << "nThe number " << find << " has been found at the 6th position" << endl;
                else if(i == 6) cout << "nThe number " << find << " has been found at the 7th position" << endl;
                else if(i == 7) cout << "nThe number "  << find << " has been found at the 8th position" << endl;
                else if(i == 8) cout << "nThe number " << find << " has been found at the 9th position" << endl;
                else if(i == 9) cout << "nThe number " << find << " has been found at the 10th position" << endl;
                found = true;
                }
            }
            if(found) cout << "nElement not foundn";
            cout << endl << endl << "----------------------" << endl;
        main();
        case 'E':
        case 'e':
            break;
}
return 0;
   }

编辑:好的,我刚刚发布了整个代码,这样您就可以更好地看到债务。那是我的遗言。不好意思。

使用 std::reverse() 函数颠倒顺序:

#include <iostream>
#include <algorithm>
int main(){
    int myArray[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    std::reverse(std::begin(myArray), std::end(myArray));
    for (const auto& arr : myArray) {
        std::cout << arr << std::endl;
    }
    return 0;
}

或者做一个简单的函数:

#include <iostream>
void reverseArray(int a[], int n)
{
    int temp;
    for (int i = 0; i < n / 2; i++)
    {
        temp = a[n - i - 1];
        a[n - i - 1] = a[i];
        a[i] = temp;
    }
}
int main(){
    int myArray[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    reverseArray(myArray, 10);
    for (const auto& arr : myArray) {
        std::cout << arr << std::endl;
    }
    return 0;
}

您应该考虑改用 std::vector 或其他一些顺序容器。

你不打印数组的第 i个元素,这是arr[i],你只打印破折号。

cout << endl << endl << "------------------------------" << endl;

此外,您以不同的方式处理小写和大写:

case 'R':
case 'r':

您的帮助选项中未提及哪个"r"。

要反转数组,您必须#include <algorithm> .语法如下所示:reverse(arrayName, arrayName + arraySize);

在您的情况下,它应该这样写:reverse(arr, arr + 10);