0

Can someone help me to output the numbers in the array that are user inputted and sort them in ascending order please this is the code i've come up with

so far it .... prompts the user no less then 50 elements and a value no more than 999

it doesnt.... output the elements and sorts them

#include <iostream.h>
#include <iomanip.h>

int sort(int [], int);
 const int LIMIT = 50;
    int MYARRAY[LIMIT];
int x,i,j,min,minx,temp,moves=0;
int value;
		
int main()
{
	for(x=0;x<LIMIT;x++)
{
	for(x=0;x<LIMIT;x++)
	{
	while(value < 999)
	{ 
		cout << "\n\nPlease enter a number: ";
		cin >> MYARRAY[x];
		value = value + MYARRAY[x];
		moves = sort(MYARRAY, LIMIT);
	}
	cout << MYARRAY[LIMIT];
	}

	cout << "\n\nThe sorted list, in ascending order, is:\n";
	cout << setw(4) << MYARRAY[LIMIT];
	cout << endl << moves << " moves were made to sort this list\n";
}
	return 0;
}

int sort(int a[], int n)
{

for(x=0;x<(LIMIT-1);x++)
	{
		min = MYARRAY[x];
		minx = x;
		for(j=x+1;j<i; j++)
		{
			if(MYARRAY[j] < min)
			{
				min = MYARRAY[j];
				minx = j;
			}
		}
		if (min < MYARRAY[x])
		{
			temp = MYARRAY[x];
			MYARRAY[x] = min;
			MYARRAY[minx]=temp;
			moves++;
		}
	}
return moves;
}
2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by Narue
0

You have quite a few problems. Compare this with what you have and see if you can find them:

#include <iostream>

using namespace std;

void sort ( int a[], int n );

int main()
{
  const int LIMIT = 5;
  int array[LIMIT];

  cout<<"Enter "<< LIMIT <<" numbers: ";
  for ( int i = 0; i < LIMIT; i++ )
    cin>> array[i];

  cout<<"Before: ";
  for ( int i = 0; i < LIMIT; i++ )
    cout<< array[i] <<' ';
  cout<<endl;

  sort ( array, LIMIT );

  cout<<"After:  ";
  for ( int i = 0; i < LIMIT; i++ )
    cout<< array[i] <<' ';
  cout<<endl;
}

void sort ( int a[], int n )
{
  for ( int i = 0; i < n; i++ ) {
    int min = i;

    for ( int j = i + 1; j < n; j++ ) {
      if ( a[j] < a[min] )
        min = j;
    }

    int save = a[i];
    a[i] = a[min];
    a[min] = save;
  }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.