I am having trouble ascending and descending an array with an insertion code. I know the while loop displays it in ascending order but i cannot get it to display from the original, ascending or descending. It just keeps repeating the first element in the array. Any help will be appreciated. The first code is the original and the other code is my attempt at the problem.

const int len = 5;
int i,j;
int elem; // Temporary hold for each element.
int A[len] = {9, 1, 3, 6, 5};
// Pick out each element in turn.
for (j=1; j<len; j++)
{
elem = A[j];
i=j-1;
// Look for a place to insert it.
while (i>=0 && A[i] > elem)
{
A[i+1]=A[i];
i--;
}
A[i+1]=elem; // Insert it.
}



// My attempt at the code

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    const int len = 5;
    int i,j;
    int elem; // Temporary hold for each element.
    int A[len] = {9, 1, 3, 6, 5}; 
  cout << "BEFORE SORT: ";
   for (j=1; j<len; j++)
   
   {
       elem = A[j];
       i=j-1; // 
        cout << A[i] <<end1; 
         
        while (i>=0 && A[i] > elem)
        {
              A[i+1]=A[i];
              i--;
              }
              A[i+1]=elem; // 
              }
              
              system("pause");
              return 0;
}

Recommended Answers

All 4 Replies

Just trying to compile what you have written I immediately get 12 error.

You need to change the section prior to your main(). All the variables and loops need to be placed in main, or at the least in a function.

After deleting the loops and variable declarations etc as mentioned above and fixing a quick syntax error I have found a 'running' program.

The reason why you keep getting 9 is because 9 is the largest number in the array (you sorted it that way, ascending order). "For the most part", you have it correct. Just get rid of your cout in the middle of your for/while loop. Make a for-loop at the end of your program that outputs the array and you will find that you have an array with same elements but in ascending order.

The reason why the code keeps repeating the first value all the time is because the first value "9" is the largest one, so it will be moving up the array as you are traversing it.

If you want to print the original array, make a separate loop for that. Then, repeat the loop after the sorting to see the result.

ok. I fixed it that it displays the original array and in ascending order with intermediate steps. How can i fix it for descending order?

Oops! I forgot to display my code

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
const int len = 5;
static int p=1;
int i,j;
int elem; // Temporary hold for each element.
int A[len] = {9, 1, 3, 6, 5};
cout << "BEFORE SORT: ";
for (j=0; j<len; j++)
cout << A[j] <<" ";
cout<<endl;
for (j=1; j<len; j++)
{
elem = A[j];
i=j-1; //
while (A > elem && i>=0 )
{
A[i+1]=A;
i=i-1;
}
A[i+1]=elem;
cout << "step " << (p++) << ":";
for (int k=0; k<len; k++)
cout << A[k] <<" ";
cout<<endl;
}
cout << "AFTER SORT: ";
for (j=0; j<len; j++)
cout << A[j] <<" ";
cout<<endl;
system("pause");
return 0;
}

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.