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;  
        }

Dani AI

Generated

Short version: you increased the array declaration but left the loop bounds hard-coded, so only the first three slots (or the first ten, depending on which loops you changed) get processed. As hinted, every loop that walks the array must use the actual length. ’s suggestion to use a dynamic container is the cleanest fix; ’s idea of a single size constant is useful when you must keep a fixed array.

Here is a minimal, robust pattern using std::vector and std::sort (C++11+). It asks how many numbers you want, validates input, reads exactly that many values, sorts them, and prints them:

#include <iostream>
#include <vector>
#include <algorithm>
#include <limits>

int main() {
    std::cout << "How many numbers? ";
    int n;
    if (!(std::cin >> n) || n <= 0) return 1;

    std::vector<int> v;
    v.resize(n);
    for (int i = 0; i < n; ++i) {
        std::cout << "Enter integer: ";
        while (!(std::cin >> v[i])) {
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Invalid input, try again: ";
        }
    }

    std::sort(v.begin(), v.end());

    std::cout << "Ascending order:\n";
    for (int x : v) std::cout << x << '\n';
    return 0;
}

If you must use a raw array, pick one constant (e.g. const int MAX = 10) and always use that for loops and bounds checks — or ask the user for n and ensure n <= MAX. If you stick with bubble sort, use an inner bound of n - i - 1 and an early-exit flag to avoid unnecessary passes.

Quick troubleshooting: watch off-by-one errors, check every cin >> for failure, avoid nonstandard headers like conio.h and system("pause"), prefer size_t for indexes when using container sizes, and compile with a modern standard (e.g. -std=c++11).

Recommended Answers

All 5 Replies

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??

I think you're looking to dynamically implementing your arrays. See this for more information. There's also a few points about your code:

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.