如何反转 Cpp 的数组
How to reverse the array for Cpp
我正在尝试命令我的程序反转一组随机数组数字。我让我的程序想出 10 位数字(范围从 60-100),当生成随机数组时,我让它给我 4 个选项;前任:
6675 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);
相关文章:
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 不同大小的向量数组的大小 cpp
- 如何循环访问 cpp 中的函数返回的字符指针数组
- 为什么我的 cpp 数组大小是 4 的倍数?
- 如何在 llvm 的 cpp api 中使用 CreateInBoundsGEP 来访问数组的元素?
- 如何使用 PHP-CPP 在 PHP 类中添加静态数组
- cpp 数组 - 分配常量索引有效,而非常量索引不起作用
- 将特定索引处的字符数组内容与字符文字 - cpp 进行比较
- 为什么当我在 CPP 的初始化中指定多维数组时,它必须有边界
- 通过矢量下标访问 Fortran 数组,相当于 cpp
- 如何在 CPP 中将 Typedef basic_string字符串转换为<char>字节数组
- 在 GRPC 中使用字节数组 (cpp)
- 如何使用 cpp 消除 2d 数组中的边框元素
- cpp 初始值设定项太多 - 数组中的结构中的结构
- 2D 数组在 CPP 中查找行的总和
- 具有不同构造函数参数的 CPP 对象数组
- 检查 CHAR 多维数组 CPP 中的值
- 将字符串转换为字节(无符号字符)数组cpp
- CSV 文件转换为带换行符的 2D 数组 CPP