質問

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

using namespace std;

int main()
{
    int a, b, c[5], d;

    cout << "enter any five number\n";
    for(a=0; a<5; a++)
    {
        cin>>c[a];
    }
    for(a=0; a<5; a++)
    {
        for(b=++a; b<5; b++)
        {
            if(c[b] < c[a])
            {
                d = c[b];
                c[b] = c[a];
                c[a] = d;
            }
        }       
    }

    cout << "\nhere is the entered numbers in order\n"; 
    for(a=0; a<5; a++)
    {
        cout << c[a];
        cout << endl;
    }
    getch();
    return 3;
}

I am desk checking this program and I expect the program to sort numbers in ascending order but I am getting the wrong output help please.

役に立ちましたか?

解決

in the inner loop it should be a + 1 not ++a

他のヒント

for(a=0;a<5;a++) and for(b=++a;..) causes a to be incremented twice.

Did you mean for(b=a+1;b<5;b++) ?

for(a=0; a<5-1; a++){
  for(b=0; b<5-a-1; b++){
    if(c[b] < c[a]){
      d = c[b];
      c[b] = c[a];
      c[a] = d;
    }
  }
}
   for(a=0; a<5; a++)
    {
        for(b=++a; b<5; b++)
        {
            if(c[b] < c[a])
            {
                d = c[b];
                c[b] = c[a];
                c[a] = d;
            }
        }       
    }

this should be

for(a=0; a<4; a++)
{
    for(b=a+1; b<5; b++)
    {
        if(c[b] < c[a])
        {
            d = c[b];
            c[b] = c[a];
            c[a] = d;
        }
    }       
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top