i have this code which input 3 numbers into ascending order. i know make it 10 i just change the array size but then it is only ascending the numbers that were inputing that are under 10. how can i change it so it's any number.

``````#include <iostream>
#include <conio.h>

using namespace std;

int  main ()
{   int x;

int array [3], t;
for (x=0; x<3; x++)
{
cout << "Enter integer number: " << endl;
cin >> array[x];
}
for (x=0; x<3; x++)
{
for (int y=0; y<2; y++)
{
if(array[y]>array[y+1])
{
t=array[y];
array[y]=array[y+1];
array[y+1]=t;
}
}
}
cout << "The integers in ascending order are : ";
for (x=0;x<3;x++)
{
cout <<"\n";
cout <<array[x];
cout << "\n";
}
system ("pause");
return 0;
}``````

May I know what exactly you changed?

just

``int array [3], t;``

to

``int array [10], t;``

And you didn't change the ones in the loops??

1. Your bubble-sort can be improved with a flag to reduce the number of iterations required to sort it. See this for an example.

2. Your code could be much simpler if you use STL classes like `std::vector` 3. NEVER use `system("pause");` . Use `std::cin.get();` instead. See this for more information.

Hope this helped :)

When you change the array size, change the bounds on the for loop as well. Thats why
you should use integer constants :

``````const int MAX_SIZE = 10;
int inputArray[MAX_SIZE] = {0}; //initialize all elements to 0

for(int i = 0; i < MAX_SIZE; ++i){
cin >> inputArray[i];
}

//sort array using bubble sort
for(int i = 0; i < MAX_SIZE; ++i){
for(int j = 0; j < MAX_SIZE; ++j){
if(inputArray[i] > inputArray[j]) std:::swap(Array[i],Array[j]);
}
}

//print Array
for(int i = 0; i < MAX_SIZE; ++i){
cout << inputArray[i] << " ";
}``````

Now if you want to change the array size, all you have to do is change only MAX_SIZE
to a different number.

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.