Reputation: 1
I am trying to command my program to reverse a set of random array numbers. I get my program to come up with 10 digits, (ranging from 60-100) and when the random array is generated, I get it to give me 4 options; ex:
66 75 84 93 82 61 66 99 85 93
R - for reverse. (this will reverse the array set to[93 85 99 66...84 75 66])
S - for search. (this will prompt you to search a number and read what line it'll be located in. )
E - for exit. (to exit the program )
A - for add.. (this will add up all the random array number set.)
Everything works great but the only issue I am having is that it won't reverse the random generated array set. I thought I promoted the correct command. I need help with this, please keep in mind I am new to 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;
}
Edited: Ok I just posted the entire code so you can see a little bit better in debt. That was my fualt. sorry.
Upvotes: 0
Views: 315
Reputation:
Use the std::reverse() function to reverse the order:
#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;
}
Or make a simple function:
#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;
}
You should consider using std::vector or some other sequential container instead.
Upvotes: 2
Reputation: 76
To reverse array, you have to #include <algorithm>
.
The syntax looks like this: reverse(arrayName, arrayName + arraySize);
In your case, it should be written like this: reverse(arr, arr + 10);
Upvotes: 0
Reputation: 2790
You dont print ith element of array which is arr[i]
, you just print dashes.
cout << endl << endl << "------------------------------" << endl;
Also you handle lower and upper case differently:
case 'R':
case 'r':
which 'r' is not mentioned in your help option.
Upvotes: 0