Please add ascending sort to C++ program below. #include <iostream> using namesp
ID: 3591306 • Letter: P
Question
Please add ascending sort to C++ program below.
#include <iostream>
using namespace std;
int main()
{
int n;
int arr[100];
cout<<"enter the no of elements in array: ";
cin>>n; //reading the size of array
cout<<"Enter the elements into array one by one ";
for(int a=0;a<n;a++)
{
cin>>arr[a]; // reading elements into array one by one
}
//Sort Array Here
for(int i=0;i<n-2;i++)
{
for(int j=i+1;j<n;j++)
{
for(int k=j+1;k<n;k++)
{
if(arr[i]+arr[j]+arr[k]==0) // condition to check whether the elements sum is zero or not
{
cout<<"{"<<i<<" "<<j<<" "<<k<<"},";
}
}
}
}
return 0;
}
Explanation / Answer
main.cpp -------->
#include <iostream>
using namespace std;
int main()
{
int i,j,n,temp;
int arr[100];
cout<<"enter the no of elements in array: ";
cin>>n; //reading the size of array
cout<<"Enter the elements into array one by one ";
for(int a=0;a<n;a++)
{
cin>>arr[a]; // reading elements into array one by one
}
//Sort Array Here
cout<<" Data before sorting: ";
for(j=0;j<n;j++)
{
cout<<arr[j]<<" ";
}
for(i=0;i<=n;i++)
{
for(j=0;j<=n-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
cout<<" Data after sorting: ";
for(j=0;j<n;j++)
{
cout<<arr[j]<<" ";
}
for(int i=0;i<n-2;i++)
{
for(int j=i+1;j<n;j++)
{
for(int k=j+1;k<n;k++)
{
if(arr[i]+arr[j]+arr[k]==0) // condition to check whether the elements sum is zero or not
{
cout<<"{"<<i<<" "<<j<<" "<<k<<"},"<<endl<<endl;
}
}
}
}
return 0;
}
Output ------------>
enter the no of elements in array: 10
Enter the elements into array one by one
2
3
89
56
2
36
45
78
88
55
Data before sorting: 2 3 89 56 2 36 45 78 88 55
Data after sorting: 2 2 3 36 45 55 56 78 88 89
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.