BUBBLE SORT .....WHAT AM I DOING WRONG ??? :( ..help!:(

+1 rasel ahmed · December 12, 2015

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

class Prob1{

private:

    int i, x[10], s;

public:

    void ReverseTheArray(){

        srand(time(0));
        s =5;
        int c =0;
        for(i=0; i<s ; i++){
        //cout<<"Enter a value : ";
        //cin>>x;
          x= rand()% 10 ;
          cout<<endl;
                c++;
          cout<<"number entered: "<<c<<"="<<x;

            cout<<endl;
            cout<<endl;

            int temp;


            for( int i=0; i<s ; i++){
                for(int j=0;j<s-i-1;j++){
                       if(x>x[j+1]){

                        temp = x;
                        x= x[j+1];
                        x[j+1]= temp;

                       }
                }


            }

        }
    }

    void Display(){
        int temp;
            for( int i=0; i<s ; i++){
                for(int j=0;j<s-i-1;j++){
                       if(x>x[j+1]){

                        temp = x;
                        x= x[j+1];
                        x[j+1]= temp;

                       }
                }


            }


        }



};



int main()
{
   Prob1 o;


   o.ReverseTheArray();
   o.Display();

    return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 rasel ahmed · December 13, 2015
it will b x[ j ] = x[ j+1] ??? 
0 sherry dang · December 16, 2015
...
0 C C · December 21, 2015
i think you may have a problem on your 2nd line in your 2 for loops, should be x[j] = x[j+1]; instead of just x = x[j+1]

look at my sample code for 10 integers: http://pastebin.com/P7Ky9XDc
0 C C · December 21, 2015
and make your inner loop j , start from int j =1;
0 Yaw Ofori · January 21, 2016
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

class Prob1 {



int i, x[10], s;

public:

void Display() {

//srand(time(0));//Don't know what this is doing.
s = 5;
int c = 0;
for (i = 0; i < s; i++) {
 
x = rand() % 10;//Assignment to indexed array

c++;
cout << "number entered: " << c << " = " << x;
cout << endl;
}
 
cout << endl;
cout << endl;

int temp;

//Arranges in ascending order.
for (int i = 0; i<s; i++) {
for (int j = 0; j < s  - 1; j++) {
if (x[j] > x[j + 1]) {

temp = x[j];
x[j] = x[j + 1];
x[j + 1] = temp;

}
}
}cout << "\n\nDisplay array in ascending order: " << endl;
for (int i = 0; i < s; i++)
cout << x << "  ";
cout << endl;

}
//Prints out the array
/*void printarray() {
for (int i = 0; i < s; i++)
cout << x << "  ";
cout << endl;
}*/
 
//Reversing the array
void ReverseTheArray() {
int temp;
for (int i = 0; i < s; i++) {
for (int j = s; j < 0; j--) {
if (x[j] > x[j + 1]) {

temp = x[j];

   x[j] = x[j + 1];

x[j + 1] = temp;
}
}
}
cout << endl;

for (int i = s-1; i >= 0; i--)//just turns the array around(descending order)
cout << x << "  ";
cout << endl;
}
};



int main()
{
Prob1 o;


o.Display();
//o.printarray();

cout << "\n\nThis is the reversed array: ";
o.ReverseTheArray();
//o.printarray();

cout << endl;
system("pause");
return 0;
}
  • 1

C++

107,221 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator