I have this program,how to find reverse numbers?

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

const int array_size = 10;

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//function to enter the number choise1/// to "&" dieuthinsi noumerou
void get_numbers(int num_arr[array_size], int& numbers){
    int count;
    cout << "How many numbers do you want to enter? "<<endl;
    cin >> numbers;
    cout << endl;

    for (count = 0; count < numbers; count++){
        cout << "Enter number " << count + 1 << ": ";
        cin >> num_arr[count];
    }
}
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//function to display the list of numbers
void display_numbers(int num[array_size], int numbers){
    int count;
    for (count = 0; count < numbers; count++)
        cout << "Number " << count + 1 << " is: " << num[count] << endl;

}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//function to save the numbers in a file
void savenumbers(int num_arr[array_size], int numbers){
    int count;
    ofstream myfilenumbers("numbers.txt");

    if (!myfilenumbers)
    {
        cout << "file could not be open for writing ! " << endl;

    }
    for (count = 0; count < numbers; count++)
    {
        myfilenumbers << num_arr[count] <<endl;
    }
    myfilenumbers.close();

}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//function to read from a file
void readfile(int nums_arr[array_size], int& num_of_nums){

num_of_nums = -1;

    ifstream functionfile("numbers.txt");
    if (!functionfile)
    {
        cout << "Error reading from file ! " << endl;
    }

    while (!functionfile.eof())
    {
        num_of_nums++;
        functionfile >> nums_arr[num_of_nums] ;
        cout << nums_arr[num_of_nums] << endl;
    }
    num_of_nums++;
    functionfile.close();
}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//function to search for a number
void search(int num[array_size]){
    int count;
    int numberforsearch;
    int count2=0;
    bool flag = false;
    cout << "Please enter the number you want to find:";
    cin >> numberforsearch;
    for (count = 0; count <10; count++)
    {
        if (num[count] == numberforsearch)
        {
            flag = true;    // turn flag on
            count2++;
            cout << "The number " << numberforsearch << " is found at position " << count + 1 <<  "." << endl;
        }
    }
    if (flag){
        cout << "The number " << numberforsearch << " is found  "  << count2 << "   time(s)." << endl;
}
    else cout << "The number is not in the list" << endl;

}

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//function to reversethe array
void reverse(int num[array_size])
{

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    ////////////////////////////////
    void main(){
        bool flist = false;
        int choice;

        int nums_array[array_size];
        int nums, count,numbers=0,s,avr,result = 0;

        do{
            system("CLS");
            cout << "------------------------Menu----------------------------" << endl;
        cout << "1. Read a list of numbers." << endl;
        cout << "2. Display list." << endl;
        cout << "3. Save list to File." << endl;
        cout << "4. Load list from file." << endl;
        cout << "5. Search for a given number in the list." << endl;
        cout << "6. Reverse list." << endl;
        cout << "7. Calculate the sum of the list" << endl;
        cout << "8. Calculate the average of the list." << endl;
        cout << "9. Find the largest number in the list" << endl;
        cout << "10. Find the smallest number in the list." << endl;
        cout << "11. Exit." << endl << endl;
        cout << "Please select a choice" << ": ";
        cin >> choice;
            switch (choice)
            {
            case 1:flist = true;

                    get_numbers(nums_array,numbers);

                break;
            case 2:if (flist)
                display_numbers(nums_array,numbers);
                     else{
                         cout << "The list is empty" << endl;
                     }
                     break;
            case 3:savenumbers(nums_array,numbers);
                cout << " then numbers is save" <<endl;
                break;
            case 4: readfile(nums_array,numbers);
                    flist = true;

                break;
            case 5:search(nums_array);
                break;
            case 6:reverse(nums_array);
                break;
            case 7:sum(nums_array);
                cout << "The sum of the numbers is " << s << endl;
                break;
            case 8:average(nums_array);
                //cout << "The average of the numbers is " << avr << endl;
                break;
            case 9:cout << "";
                break;
            case 10:cout << "";
                break;
            case 11:
                break;
            default:cout << "Please enter a valid choise." << endl;
            }
            system ("pause");   
        }

        while (choice != 11);


    }

do you want to swap the array around or simply print it backwards ?

Just start from the function you used to print your numbers in the right order, but reverse it. Start from the back and move to the front. The code is really similar all you have to do is change your starting point.

Member Avatar for ArashVenus

Andreas reverse array can be a function like this (It might be a little bit more complicated than his but the algorithm is still the same):

int array2[array_size];
int j=0;
for (int i=array_size-1 ; i>=0 ; i--)
{
    array2[j]=array[i]
    j++
}

array is your array (or list) up there ,
array2 is the array having reverse numbers .

If you don't need the reverse numbers in a new array , you could just print them in that loop .

Andreas, Personally prefer: array2[j++]=array[i];

In your search function you have hardcoded 10. What happens when there are 5 items in the array? What happens when there are 20 and the 15'th has the number you are looking for?

Also, in search put a bool in your loop to exit your search when you find the number you are looking for.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.