+ 2

sorting

void sort(float a[], int indx[], int n); int main() { float a[8]= {23,45,67,89,23,90,100,55}; cout<<"Before Sorting: "<<endl; for(int i=0; i<8; i++) { cout<<a[i]<<" , "; } cout<<"\nAfter Sorting: "<<endl; sort(a,3,8); } void sort(float a[], int indx[], int n) { //indirect bubble sort for(int i=1; i<n; i++) { //bubble up max for(int j=0; j<n-i; j++) { if(a[indx[j]]>a[indx[j+1]]) { swap(indx[j],indx[j+1]); } } } cout<<"the array is: "<<endl; for(int i=0; i<n; i++) { cout<<a[i]<<" , "; } } what is the wrong in this code?

23rd Apr 2021, 1:25 PM
Ramisa Fariha
Ramisa Fariha - avatar
10 Respostas
+ 4
sort() function signature void sort( float[], int[], int ); Invoked in main() as follows sort( a, 3, 8 ); The second parameter is defined to accept array of `int`, but instead, an `int` was given when calling it.
23rd Apr 2021, 1:57 PM
Ipang
+ 2
Bubble sort is a classic... Put simply Consider swapping 2 nos a and b You need a 3rd variable c The algorithm: a = b b = c c= a
24th Apr 2021, 5:05 AM
Sanjay Kamath
Sanjay Kamath - avatar
+ 2
thank you @Martin Taylor...the solution only has the function but not the code..I was confused about how to write the index array but now it's clear to me..And I've tried to understand from the book but sometimes the way the language is used there seems hard to understand.
24th Apr 2021, 8:35 PM
Ramisa Fariha
Ramisa Fariha - avatar
+ 1
thank you
23rd Apr 2021, 2:06 PM
Ramisa Fariha
Ramisa Fariha - avatar
+ 1
Is this some kind of test? ☺️ I counted three errors, one extra operation and a violation of logic ☺️ #include <iostream> using namespace std; // Declare function void sort(float a[], int indx[], int n) { //indirect bubble sort for(int i=1; i<n; i++) { //bubble up max for(int j=0; j<n-i; j++) { if(a[indx[j]]>a[indx[j+1]]) { swap(indx[j],indx[j+1]); } } } cout<<"the array is: "<<endl; for(int i=0; i<n; i++) { cout<<a[indx[i]]<<" , ";//3 } } int main() { float a[8]= {23,45,67,89,23,90,100,55}; cout<<"Before Sorting: "<<endl; int indx[8]; //1 for(int i=0; i<8; i++) { indx[i] = i; //2 cout<<a[i]<<" , "; } cout<<"\nAfter Sorting: "<<endl; sort(a,indx,8); } P. S: "sort(a, 3, 8); 🤔 Why three, what were you trying to get?" ☺️
25th Apr 2021, 4:13 PM
Solo
Solo - avatar
0
@ Martin Taylor will I create the index array with the reverse values of the main array? void sort(float a[], int indx[], int n); void showarray( float a[],int n); void showindxarray( int a[],int n); int main() { float a[8]= {23,45,67,89,23,90,100,55}; int array_size=8; cout<<"Before Sorting: "<<endl; showarray(a,array_size); int indx[]= {55,100,90,23,89,67,45,23}; int indxarray_size=8; cout<<"\nAfter Sorting: "<<endl; sort(a,indx,8); showindxarray(indx,indxarray_size); } void sort(float a[], int indx[], int n) { //indirect bubble sort for(int i=1; i<n; i++) { //bubble up max for(int j=0; j<n-i; j++) { if(a[indx[j]]>a[indx[j+1]]) { swap(indx[j],indx[j+1]); } } } } void showarray( float a[],int n) { for(int i=0; i<n; i++) { cout<<a[i]<<","; } } void showindxarray( int a[],int n) { for(int i=0; i<n; i++) { cout<<a[i]<<","; } } after swapping indx array not changing
24th Apr 2021, 6:59 AM
Ramisa Fariha
Ramisa Fariha - avatar
0
That was a mistake
25th Apr 2021, 4:53 PM
Ramisa Fariha
Ramisa Fariha - avatar