+ 1

# What sorting is wrong 🙄

https://code.sololearn.com/ctB3LrnXm3qW/?ref=app Iam try to input a array and I like to sort it ascending and descending orders Sorting is mistake in my code . check and solve it guys

4th Jan 2022, 2:58 AM
Sangeetha Santhiralingam
4th Jan 2022, 11:36 AM
Sangeetha Santhiralingam
+ 3
Provide more description of your question, give some sample input and the desired output to get more relevant answers.
4th Jan 2022, 4:12 AM
Rishi
+ 3
Thank you very much
4th Jan 2022, 11:31 AM
Sangeetha Santhiralingam
+ 2
Okay 👌🆗
4th Jan 2022, 7:11 AM
Sangeetha Santhiralingam
+ 2
#include <bits/stdc++.h> using namespace std; int main() { int arr[10],c; cout<<"Enter the number of elements in the array \n"; cin>>c; int b = sizeof(arr); cout<<"leanth off array= "<<b<<endl; int q = sizeof(arr[0]); cout<<q<<endl; //(1) no need n thus n value //int n = sizeof(arr) / sizeof(arr[0]); //sizeof (arr) is the total size occupied by the array. sizeof (arr [0]) is the size of the first element in the array. cout<<"enter the every elements of the array orderly\n"; for(int i=0;i<c;i++) { cin>>arr[i]; } cout<<"The array is\n"; for(int i=0;i<c;i++) { cout<<arr[i]<<" "<<"\n"; } cout<<endl; //(2) // sort(arr,arr+n); n is total array size you declared as you calculated. you just need to use c as array elenents you are using.. so use sort(arr,arr+c); //sorting the array from start to end. //cond...
4th Jan 2022, 10:13 AM
Jayakrishna 🇮🇳
+ 2
cout<<"Ascending order is \n"; for(int i=0;i<c;i++) //(3) array indexes starts from 0 so use, i=0 instead of 1 andd condition i<c; { cout<<arr[i]<<" "; } cout<<endl; sort(arr,arr+c,greater<int>()); //(4)here same use c instead of n cout<<"Descending order is \n"; for(int i=0;i<c;i++) //(5)same here use i=0 to c { cout<<arr[i]<<" "; } } edit: https://code.sololearn.com/cKASQ0bcX57b/?ref=app
4th Jan 2022, 10:14 AM
Jayakrishna 🇮🇳
+ 2
you can remove n variable because unused. And better to use #include<iostream> #include<algorithm> In place of #include <bits/stdc++.h> You're welcome.. edit: https://code.sololearn.com/cKASQ0bcX57b/?ref=app
4th Jan 2022, 11:53 AM
Jayakrishna 🇮🇳
+ 2
Okay 👌
4th Jan 2022, 11:53 AM
Sangeetha Santhiralingam
+ 2
Look at this code for reference: This is best for dynamically storing and sorting array. plus it sorts floats and integral values https://code.sololearn.com/cM95l02G31pX/?ref=app
5th Jan 2022, 10:59 AM
Abraham