Sort an Array in Ascending or Descending order using Function Pointers
Object: Write a C++ program to input elements in an array and sort array using pointers. How to sort an array in ascending or descending order using function pointers in C programming. Logic to sort an array using pointers in program.
Example
Input
Input array elements: 10 -1 0 4 2 100 15 20 24 -5
Output
Array in ascending order: -5, -1, 0, 2, 4, 10, 15, 20, 24, 100,
Array in descending order: 100, 24, 20, 15, 10, 4, 2, 0, -1, -5, C++ projects for beginners with source code
Code:
#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
int num[10],i,j,temp;
cout<<“Enter Any Numbers:\n”;
for(i=1;i<=10;i++)
{
cin>>num[i];
}
cout<<“Inpute Array is: “;
for(i=1;i<=10;i++)
{
cout<<num[i]<<” “;
}
for(i=1;i<=10;i++)
{
for(j=1;j<=10-i;j++)
{
if(num[j]>num[j+1])
{
temp=num[j];
num[j]=num[j+1];
num[j+1]=temp;
}
}
}
cout<<“\n\nArray in Ascending Order: “;
for(j=1;j<=10;j++)
{
cout<<num[j]<<” “;
}
cout<<“\n\nArray in Descending Order: “;
for(j=10;j>=1;j–)
{
cout<<num[j]<<” “;
}
_getch();
}
#include<iostream> #include<conio.h> using namespace std; void main() { int num[10],i,j,temp; cout<<"Enter Any Numbers:\n"; for(i=1;i<=10;i++) { cin>>num[i]; } cout<<"Inpute Array is: "; for(i=1;i<=10;i++) { cout<<num[i]<<" "; } for(i=1;i<=10;i++) { for(j=1;j<=10-i;j++) { if(num[j]>num[j+1]) { temp=num[j]; num[j]=num[j+1]; num[j+1]=temp; } } } cout<<"\n\nArray in Ascending Order: "; for(j=1;j<=10;j++) { cout<<num[j]<<" "; } cout<<"\n\nArray in Descending Order: "; for(j=10;j>=1;j--) { cout<<num[j]<<" "; } _getch(); }
Pingback: C++ projects for beginners with source code - Codeboks
You made some good points there. I did a Google search about the topic and found most people will believe your blog.