write a profram for selection sort

whats the error in it?

Answers were Sorted based on User's Feedback



write a profram for selection sort whats the error in it?..

Answer / joshin

//program for selection sort
#include<stdio.h>
#include<conio.h>
#define MX 100
void selection(int [], int);
void selection(int a[],int n)
{
int minindx,t;
int i,j;
for(i=0;i<n-1;i++)
{
minindx=i;
for(j=i+1;i<n;j++)
{
if(a[j]<a[minindx])
minindx=j;
}
if(minindx!=i)
{
t=a[i];
a[i]=a[minindx];
a[minindx]=t;
}
}
}
void main()
{
int a[MX],i,n;
clrscr();
printf("enter total num of elements:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
selection(a,n);
printf("sorted arrau:\n");
for(i=0;i<n;i++)
{
printf("%d \t",a[i]);
}
getch();
}

Is This Answer Correct ?    8 Yes 1 No

write a profram for selection sort whats the error in it?..

Answer / alam

#include<iostream>
using namespace std;
int main()
{
int i,t,j,min,n=0,m=0;
int a[6]={6,3,2,10,5,8};
//0 1 2 3 4 5
for(i=0;i<=13;i++)
{
m++;
min=i;
for(j=i+1;j<=5;j++)
{
if(a[j]<a[min])
min=j;
n++;
}
t=a[i];
a[i]=a[min];
a[min]=t;
}
cout<<"******Selection Sort*********"<<endl;
for(i=0;i<=5;i++)
{
cout<<"a["<<i<<"]="<<a[i]<<endl;
}

cout<<"Outer Loop Total count :"<<m<<endl;
cout<<"Inner Loop Total count :"<<n<<endl;


return 0;
}

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More C C++ Errors Interview Questions

Declaration of Cube Guys please help me.. Is this a right way to declare cube.? If i Compile it. It Says: Cube undeclared what should i do? Please help \thanks in advanced #include<stdio.h> #include<math.h> #include<conio.h> main( ) { float x,y; while(x++<10.0) { printf("Enter Number:"); scanf("%d", &x); y = cube(x); printf("%f %f %f \n", x,pow(x,2),y); cube(x); } { float x; float y; y = x*x*x; } getch(); return (y); }

2 Answers  


void main() { for(int i=0;i<5;i++); printf("%d",i); } What is the output?..

32 Answers   College School Exams Tests, CTS, HCL, iGate, SmartData,


#include<>stdio.h> #include<>conio.h> { printf("hello"); void main() getch(); } what the out put of this program and why ......plz clear my answer

10 Answers   Wipro,


How to create a program that lists countries capitals when country is entered? (Terribly sorry, I'm a complete novist to coding with C, am looking for inspiration and general tips on how to code and create this program.)

0 Answers  


What is the code for following o/p * * * * * * * * * * * * * * * *

1 Answers  






what is the error in the following code: main() { int i=400,j; j=(i*i)/i; }

4 Answers  


I'm having trouble with coming up with the correct code. Do I need to put a loop? Please let me know if I'm on the right track and what areas I need to correct. I still don't have a good grasp on this programming stuff. Thanks =) The assignment was to write a program using string functions that accepts a coded value of an item and displays its equivalent tag price. The base of the keys: 0 1 2 3 4 5 6 7 8 9 X C O M P U T E R S Sample I/O Dialogue: Enter coded value: TR.XX Tag Price : 68.00

3 Answers   UCB,


What is the out put of this programme? int a,b,c,d; printf("Enter Number!\n"); scanf("%d",&a); while(a=!0) { printf("Enter numbers/n"); scanf("%d%d%d",&b,&c,&d); a=a*b*c*d; } printf("thanks!"); getche(); Entering numbers are a=1,b=2,c=3,d=4 b=3,c=4,d=-5 b=3,c=4,d=0

5 Answers   TCS,


Why are memory errors hard to debug?

1 Answers  


#include<stdio.h> void main() { int i=1; printf("%d%d%d",i++,++i,i); }

19 Answers  


what are the techniques for reducing the fragility of a memory bug?

1 Answers  


#include<iostream.h> #include<stdlib.h> static int n=0; class account { int age,accno; float amt; char name[20]; public: friend void accinfo(account [] ,int); void create(); void balenq(); void deposite(); void withdrawal(); void transaction(account []); }; void account :: create() { static int acc=1231; accno=acc+n; cout<<"\n\tENTER THE CUSTOMER NAME : "; cin>>name; cout<<"\n\t ENTER THE AGE : "; cin>>age; cout<<"\n\t ENTER THE AMOUNT : "; cin>>amt; // if(amt<=500) // cout<<"\n\tAMOUNT IS NOT SUFFICIENT TO CREATE AN ACCOUNT..."; cout<<"\n\t YOUR ACCOUNT NUMBER : "<<accno<<endl; n++; } void accinfo(account cus[],int ch) { int no,flag=0; cout<<"\n\t\tENTER YOUR ACCOUNT NUMBER : "; cin>>no; for(int i=0;i<=n&&flag==0;i++) if(no==cus[i].accno) { flag=1; switch(ch) { case 2: cus[i].balenq(); break; case 3: cus[i].deposite(); break; case 4: cus[i].withdrawal(); break; case 5: cus[i].transaction(cus); break; default: cout<<"\n\t\tEND OF THE OPERATION"; exit(1); } } if(flag==0) cout<<"\n\t\tYOUR ACCOUNT DOES NOT EXIST..."<<endl; } void account :: balenq() { cout<<"\n\t\tCUSTOMER NAME : "<< name << endl; cout<<"\n\t\tBALANCE : "<< amt << endl; } void account :: deposite() { int damt; cout<<"\n\t\tCUSTOMER NAME : "<< name <<endl; cout<<"\n\t\tBALANCE : "<< amt <<endl; cout<<"\n\tENTER THE AMOUNT TO BE DEPOSITED : "; cin>>damt; amt+=damt; cout<<"\n\t\tYOUR CURRENT BALANCE : "<<amt<<endl; } void account :: withdrawal() { int wamt; cout<<"\n\t\tCUSTOMER NAME : "<< name; cout<<"\n\t\tBALANCE : "<< amt; cout<<"\n\tENTER THE AMOUNT TO BE WITHDRAWN : "; cin>>wamt; if(amt-wamt>=500) { amt-=wamt; cout<<"\n\t\tYOUR CURRENT BALANCE : "<<amt; } else cout<<"\n\tYOUR BALANCE IS TOO LOW FOR WITHDRAWAL..."<<endl; } void account :: transaction (account cus[]) { int no,tamt,flag=0; cout<<"\n\tENTER THE RECEIVER'S ACCOUNT NUMBER : "; cin>>no; cout<<"\n\t\t ENTER THE AMOUNT : "; cin>>tamt; for(int i=0;i<=n&&flag==0;i++) if(cus[i].accno==no) { flag=1; cus[i].amt+=tamt; amt-=tamt; cout<<"\n\t\tYOUR CURRENT BALANCE : "<<amt<<endl; cout<<"\n\t\t RECEIVER'S BALANCE : "<<cus[i].amt<<endl; } if(flag==0) cout<<"\n\tRECEIVER'S ACCOUNT NUMBER IS NOT AVALIABLE..."<<endl; } void main() { account cus[10]; int ch; do { cout<<"\n\t\t BANK ACCOUNT"; cout<<"\n\t\t ************\n"; cout<<"\n\t\t1.CREATE AN ACCOUNT"; cout<<"\n\t\t2.BALANCE ENQUIRY"; cout<<"\n\t\t3.DEPOSITE"; cout<<"\n\t\t4.WITHDRAWAL"; cout<<"\n\t\t5.TRANSACTION"; cout<<"\n\t\t6.EXIT\n\n"; cout<<"\n\t\tENTER YOUR CHOICE : "; cin>>ch; if(ch==1) cus[n].create(); else accinfo(cus,ch); }while(1); }

1 Answers  


Categories