Hello!

I need some help figuring out how to display my array. The program I am working on is one in which the user can enter any given 20 integers and it display them in descending order. I was able to get that part to work but cannot get the inputed array to display with the results. I am using Visual C++. Any help would be greatly appreciated.

here is what I have...

#include "stdafx.h"
#include <iostream>
using namespace std;
const int N=20;

int main()
{
    int a[N],i,nb,tmp;

    for(i=0;i<N;i++)
    {
        cout << "Please enter an integer: ";
        cin >> a[i];
    }
    do {
        nb=0;
        for (i=0;i<N-1;i++)
            if (a[i]>a[i-1])
            {
                tmp=a[i];a[i]=a[i-1];a[i-1]=tmp;
                nb++;
            }
    } while(nb!=0);

    cout << "The sorted array:" << endl;
    for (i=0;i<N;i++)
        cout << "iIntegers[" << i << "] = " << a[i] << endl;

    return 0;
}

I tried it, and I found only one very small problem.

#include <iostream>
using namespace std;

const int N = 20;

int main()
{
   int a[N], i, nb, tmp;

   for (i = 0; i < N; i++) {
      cout << "Please enter an integer: ";
      cin >> a[i];
   }
   do {
      nb = 0;
      for (i = 0; i < N [B]- 1[/B]; i++) {
         if (a[i] > a[i - 1]) {
            tmp = a[i];
            a[i] = a[i - 1];
            a[i - 1] = tmp;
            nb++;
         }
      }
   } while (nb != 0);

   cout << "The sorted array:" << endl;

   for (i = 0; i < N; i++) {
      cout << "iIntegers[" << i << "] = " << a[i] << endl;
   }

   cin.ignore();
   cin.ignore();

   return 0;
}

Delete the part highlighted in red, as this is skipping one of the integers in the array.

Hope this helps.

Thank you. That makes sense! Any clue on how to get the array of iputted data to display before the sorted result?

Thank you. That makes sense! Any clue on how to get the array of iputted data to display before the sorted result?

You only have one array, so sorting the array is going to overwrite the unsorted array. So if you want to display the unsorted array, you either need to display it BEFORE you sort the array, or if you want to display both the unsorted array and the sorted array AFTER you sort the array, you need to make a deep copy of the array BEFORE you sort it, then sort one of the arrays, then display both arrays. One array will be sorted, one won't.

Ive run your program and it gave me an error, according to my limited knowledge it's related towards going out of bound with your array.

Try using for loops:

for (int i = 0; i < (N-1); i++)

and change your switching algo to this:

if (a[j] < a[j+1])
{
            int tmp = a[j];
            a[j] = a[j+1];
            a[j+1] = tmp;
}

Remember, you need two loops, you run the first for one time and the second will go threw the whole array, if that's okay, your first array will run for the second time and so on and on.

Watch out for overflowing your array.

Thanks for the input!

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.