I have made a program which the user will input the total inputted numbers and record it as an array, sort the biggest and smallest number, post the original inputted number, reverse the original's order, and lowest to high order. I have done the recording, sorting, however, when the program post the original number, reverse order, and high to low, it is not complete. It is not posting the whole arrays. Please check my program and if it is possible point out the errors. Thank you so much.

#include <iostream>
#include <iomanip>

using namespace std;
void r3(double[], int&);
void Reverse(double[], int&);

int main()
{
    int j;

    cout << "Enter how many numbers to input : " ;
    cin  >> j;
    
    double r1[j], temp[j];
    
       for ( int i=0 ; i<j ; i++)
           {
            cout << "Enter #" << i + 1 << " : "; 
                 cin >> r1[i] ;
                 temp[i]=r1[i];
                     }  
          
          cout<< endl;          
          r3(temp, j);
          cout <<  endl;
          Reverse(r1, j);
  
           cout << "\nFrom Lowest To Highest" << endl ;
     while (j-1>=0)
     {cout << "      " <<setw(5) << fixed << setprecision(1) << r1[j] << endl;
     j--;}
           
    system("pause");
    return 0; 
}
void r3(double r1[], int& j)
    
    {
    bool restart;      
    double highest, lowest, temp;             
    int i;          
   
    do                           
    {
    restart = false;
    
    for ( i = 0 ; i < j; i++ )
    {
    
    if ( r1[i] < r1[i+1] ) 
    {
    temp = r1[i];            
    r1[i] = r1[i+1];         
    r1[i+1] = temp;        
    
    restart = true;           
    }                         
    }                                            
    }                          
    
    while ( restart == true ); 
    highest = r1[0];           
    lowest = r1[j-1];    
    {

cout << "Highest inputted number : " << highest << endl;
cout << "\nLowest inputted number : " << lowest << endl;

}
}
void Reverse(double r1[], int& j)
{
     int k = 0;
     double r2[j];
    cout << "    Original\tReverse" << endl;     
     for (;k<=j+1;j--)
     {
     r2[k]=r1[j-1];
   
    cout << "    " <<setw(5) << fixed << setprecision(1) << r1[k]
         << "       " <<setw(5) << fixed << setprecision(1) << r2[k] << endl ;
         k++;
         }    
}

In your reverse function, even though you're passing r1, it isn't changed. You're only making changes to r2, which is local to your reverse function.

Edited 5 Years Ago by Chilton: n/a

Why on earth do you need a second array for reversing an array...!!!

Below is a better approach to do it:

int i,j;
for(i=0, j=arr_len-1;i<j;i++, j--)/*arr_len is the len of array*/
{
    /*  swap arr[i], arr[j]  */
}

Ah I see, good point. Thank you for that. But my other problem is that when I run the program, how come it is not posting all the numbers in the array I inputted. Lets say I inputted 11 numbers, but only 5 or 6 numbers shows on the screen?

Hello tinee,
You have given while(j-1>=0). It should have been j>=0 (note that j=j-1 should be given just before the while loop in the main) because every time after decrementing the loop checks j-1>=0(which is like decrementing twice though not decremented actually) and you get only half the values you entered.

Edited 5 Years Ago by Arbus: n/a

This article has been dead for over six months. Start a new discussion instead.