#include<iostream.h>
#include<conio.h>
int main()
{   int a[10],temp,bak;
    cout<<" enter the elements "<<endl;
    for(int i=0;i<5;i++)
    cin>>a[i];
    int m=a[0];
    for(int j=0;j<5;j++)
    {
       if(a[j]>a[j+1])
       {
             temp=a[j];
             a[j]=a[j+1];
             a[j+1]=temp;
       }  
       
                   
        
    }
    cout<<endl;
    for(int k=0;k<5;k++)
    {
         cout<<a[k]<<endl;
    }
    getch();
}

Dani AI

Generated

Two quick points that explain the observed behavior, then a safe modern fix.

The problem
The original routine only makes one pass of adjacent swaps and also reads or writes past the intended range (accessing a[j+1] when j reaches the last valid index). That combination produces undefined behavior and leaves the array only partially ordered. fixed the single-pass issue by adding the nested loop; correctly pointed out the mismatch between declared size and used elements (and the unused variable).

Simple, robust fix
Prefer a container with a real size and a standard algorithm. This removes "magic numbers", prevents out-of-bounds mistakes, and is easier to test. Example (compiles with modern g++/clang++):

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

int main() {
    std::size_t n;
    if (!(std::cin >> n) || n == 0) return 1;
    std::vector<int> v(n);
    for (std::size_t i = 0; i < n; ++i) std::cin >> v[i];
    std::sort(v.begin(), v.end());
    for (int x : v) std::cout << x << '\n';
    return 0;
}

If you need a manual bubble sort, make sure the inner loop stops before the last compared pair each pass (inner upper bound should be n - i - 1), and run n-1 passes.

Troubleshooting and best practices

  • Compile with warnings enabled (-Wall -Wextra -std=c++17) and fix all warnings.
  • Avoid nonportable headers like conio.h and functions such as getch(); they hide portability bugs.
  • Keep variable lifetimes and meanings clear (remove unused variables like m or give them a purpose).
  • Test small inputs and boundary cases; a single out-of-range index can make results look random because of undefined behavior.

This both addresses the original bug and gives a safer, maintainable pattern for sorting input in C++.

Recommended Answers

All 5 Replies

This Is The Right Code

#include<iostream>
using namespace std;
int main()
{ 
	int a[10],temp,i,j,m;
	cout<<" enter the elements "<<endl;
	for(i=0;i<5;i++)
		cin>>a[i];
	m=a[0];
	for(i=0;i<4;i++)
	{
		for(j=i+1;j<5;j++)
		{
			if(a[i]>a[j])
				{
					temp=a[i];
					a[i]=a[j];
					a[j]=temp;
				} 
		}
	}
cout<<endl;
for(int k=0;k<5;k++)
{
cout<<a[k]<<endl;
}
return 0;
}

That's good, but why does a have 10 elements? :) And m isn't used, etc.

That's good, but why does a have 10 elements? :) And m isn't used, etc.

Didn't look in much detail..just removed the compilation errors...

I see. BTW, it should have 5 elements.

IMO "debug my code" is not a question...

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.