我正在尝试命令我的程序反转一组随机数组。 我让我的程序得到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 - for add ..(这将添加所有随机数组编号。)
一切都很好,但我遇到的唯一问题是它不会反转随机生成的数组。我以为我提升了正确的命令。 我需要帮助,请记住我是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 << "\n\nR[reverse]\t[S]search\t[E]exit\t\t[A]add\nSelect 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 found\n";
cout << endl << endl << "----------------------" << endl;
main();
case 'E':
case 'e':
break;
}
return 0;
}
编辑: 好的,我刚发布了整个代码,所以你可以看到更好的债务。那是我的意思。抱歉。
答案 0 :(得分:2)
使用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或其他顺序容器。
答案 1 :(得分:0)
你不打印 i 数组arr[i]
的元素,你只需要打印破折号。
cout << endl << endl << "------------------------------" << endl;
你也可以区别对待大写和小写:
case 'R':
case 'r':
其中&#39; r&#39;在您的帮助选项中未提及。
答案 2 :(得分:0)
要反转数组,您必须#include <algorithm>
。
语法如下所示:reverse(arrayName, arrayName + arraySize);
在您的情况下,它应该写成:reverse(arr, arr + 10);