0

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.

3
  • Where are you trying to reverse the array? Looks like you run a loop that prints out what is supposed to happen after the reverse. Also, if you are new to C++, great time to learn to use std::array<int, 10> or std::vector<int> and en.cppreference.com/w/cpp/algorithm/reverse Commented Jan 13, 2017 at 16:41
  • 2
    did you mean to cout << arr[i] where you are printing the reversed array? Commented Jan 13, 2017 at 16:42
  • Do you want to actually reverse the array, or just print it out in reverse order? Commented Jan 13, 2017 at 16:57

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This seems like a case where the OP is probably learning how to reverse an array, perhaps as part of a class assignment. If that's the case, using a library function is easy but not educational.
@Caleb I think so too. I have also included a simple user defined function.
0

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.

2 Comments

that's how you handle two cases the same
@KennyOstrom well there are some cases linuxcommand.org/man_pages/ls1.html
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.