hello could u pls help me with these programs

 Tweet
9 replies [Last post]
ramyaa
Offline
Last seen: 9 years 51 weeks ago
Joined: 2009-11-30
bOt Points: 102

1.selection sorting

2.insertiom sorting

3. to delete an element which is repeated in the array

for eg:12334, ans is 1234

pls help me

thanks in advance and happy new year

shashwat
Offline
Last seen: 1 year 30 weeks ago
Joined: 2009-02-18
bOt Points: 1160
Selection Sort

Hi ramyaa,

Happy new year

Here is your program for SELECTION SORT

#include <iostream>

using namespace std;

void selectionSort(int x[], int n) {

for (int pass=0; pass<n-1; pass++) {

int potentialSmallest = pass;  // assume this is smallest

//--- Look over remaining elements to find smallest.

for (int i=pass+1; i<n; i++) {

if (x[i] < x[potentialSmallest]) {

//--- Remember index for latter swap.

potentialSmallest = i;

}

}

//--- Swap smallest remaining element

int temp = x[pass];

x[pass] = x[potentialSmallest];

x[potentialSmallest] = temp;

}

}

int main()

{

int i,x[10]={12,44,55,34,7,10,3,30,20,15};

cout<<"Elements of the array are as follows\n";

for(i=0;i<9;i++)

cout<<x[i]<<", ";

cout<<x[i]<<"\n";

selectionSort(x, 10);

cout<<"Elements of the array after SORTING are as follows\n";

for(i=0;i<9;i++)

cout<<x[i]<<", ";

cout<<x[i]<<"\n";

return 0;

}

shashwat
Offline
Last seen: 1 year 30 weeks ago
Joined: 2009-02-18
bOt Points: 1160
Insertion Sort

Below is the program for INSERTION SORT

#include <iostream>

using namespace std;

void insertion_sort(int x[],int length)

{

int key,i;

for(int j=1;j<length;j++)

{

key=x[j];

i=j-1;

while(x[i]>key && i>=0)

{

x[i+1]=x[i];

i--;

}

x[i+1]=key;

}

}

int main()

{

int i,x[10]={12,44,55,34,7,10,3,30,20,15};

cout<<"Elements of the array are as follows\n";

for(i=0;i<9;i++)

cout<<x[i]<<", ";

cout<<x[i]<<"\n";

insertion_sort(x, 10);

cout<<"Elements of the array after INSERTION SORTING are as follows\n";

for(i=0;i<9;i++)

cout<<x[i]<<", ";

cout<<x[i]<<"\n";

return 0;

shashwat
Offline
Last seen: 1 year 30 weeks ago
Joined: 2009-02-18
bOt Points: 1160
Delete elements which are repeated in the array

#include <iostream>

using namespace std;

void delRepeated(int x[], int n) {

int pass,j,count=0;

for (pass=0; pass<n-1; pass++) {

for (int i=pass+1; i<n; i++) {

if ((x[pass] == x[i]) && (x[i]!=NULL)) {

count++;

for (j=i; j<n-count; j++)

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

x[j]=NULL;

}

}

}

cout<<"Total number of repeated elements that were deleted = "<<count<<"\n";

}

int main()

{

int i,x[10]={12,44,12,34,7,12,3,12,20,44};

cout<<"Elements of the array are as follows\n";

for(i=0;i<9;i++)

cout<<x[i]<<", ";

cout<<x[i]<<"\n";

delRepeated(x, 10);

cout<<"Elements of the array after DELETION are as follows\n";

for(i=0;i<9;i++)

cout<<x[i]<<", ";

cout<<x[i]<<"\n";

return 0;

ramyaa
Offline
Last seen: 9 years 51 weeks ago
Joined: 2009-11-30
bOt Points: 102
Thanks a lot... shashwat

Thanks a lot... shashwat

ramyaa
Offline
Last seen: 9 years 51 weeks ago
Joined: 2009-11-30
bOt Points: 102
hello shashwat can u pls

hello shashwat

can u pls make insertion and selection sorting program without use namespace std ,make it simply like u have the bubble sort and do tell me why u have used namespace std

Note:- This post has been edited by ramyaa at Sun, 2010-01-10 19:52.

shashwat
Offline
Last seen: 1 year 30 weeks ago
Joined: 2009-02-18
bOt Points: 1160
Hi ramyaa, Just change the

Hi ramyaa,

Just change the include header file line code as indicated below-

Once you have done this now simply remove using namespace std from your code and execute it. To read about namespace read this tutorial.

krish99
Offline
Last seen: 10 years 6 weeks ago
Joined: 2010-01-11
bOt Points: 74
addition and subtraction of rational numbers using constructors

hi...i have a problem in the program code of addition and subtraction of two rational numbers using constructors in c++...can u pls help me out...

shashwat
Offline
Last seen: 1 year 30 weeks ago
Joined: 2009-02-18
bOt Points: 1160
Addition and subtraction of rational numbers using constructors

#include <iostream.h>

class rational

{

double num;

double den;

public:

// Constructor

rational( double n = 0.0, double d = 1.0 ) : num( n ), den( d )

{

if ( den > 0 )

{

d = den;

}

else

{

cout << "Error Try again" << endl;

}

for ( int i = 2; i <= num; ++i )

{

if ( ((num/i) == ((int)(num/i))) && ((den/i) == ((int)(den/i))) )

{

num /= i;

den /= i;

--i;

}

}

}

rational operator+ ( const rational &rhs )

{

return rational( num * rhs.den + rhs.num * den, den * rhs.den );

}

// subtraction

rational operator- ( const rational &rhs ) const

{

return rational( num * rhs.den - rhs.num * den, den * rhs.den );

}

// multiplication

rational operator* ( const rational &rhs ) const

{

return rational(num * rhs.num, den * rhs.den);

}

// division

rational operator/ ( const rational &rhs ) const

{

return rational(num * rhs.den, den * rhs.num);

}

// less than

bool operator<  ( const rational &rhs ) const

{

return( num * rhs.den < rhs.num * den );

}

// greater than

bool operator>  ( const rational &rhs ) const

{

return( num * rhs.den > rhs.num * den );

}

// less or equal to

bool operator<= ( const rational &rhs ) const

{

return( num * rhs.den <= rhs.num * den );

}

// greater than or equal to

bool operator>= ( const rational &rhs ) const

{

return( num * rhs.den >= rhs.num * den );

}

// equality

bool operator== ( const rational &rhs ) const

{

return num == rhs.num && den==rhs.den;

}

// inequality

bool operator!= ( const rational &rhs ) const

{

return !( num == rhs.num && den==rhs.den );

}

// extraction

friend ostream &operator<< ( ostream&, const rational& );

// insertion

friend istream &operator>> ( istream&, rational& );

};

istream &operator>> ( istream &in, rational &rhs )

{

return in >> rhs.num >> rhs.den;

}

// show form: (a, b)

ostream &operator<< ( ostream &out, const rational &rhs )

{

return out << rhs.num << "/" << rhs.den;

}

int main()

{

rational x, a, b;

char c;

do

{

cout << "Enter a fraction for a: ";

cin  >> a;

cout << "Enter a fraction for b: ";

cin  >> b;

x = a + b; cout << "\nx = a + b: " << x << endl;

x = a - b; cout << "\nx = a - b: " << x << endl;

x = a * b; cout << "\nx = a * b: " << x << endl;

x = a / b; cout << "\nx = a / b: " << x << endl;

cout << "Continue? (y/n) ";

cin  >> c;

} while ( c == 'y' );

return 0;

}

Use our Online C/C++ Compiler to check out this program. I have gave the following input to this program using the compiler -

And i got the output as shown below -