Tuesday 16 May 2017

c++ program to print ascending and descending order by passing array pass by reference

// ascending and descending.cpp: Defines the entry point for the console application.

#include<iostream>
using namespace st
d;
void ascending(int arr[],int size);
void descending(int arr[],int size);
int main(){
int arr[10];
cout<<" enter 10 randon integers "<<endl;
for(int x=0;x<10;x++){
cin>>arr[x];
}
cout<<" enter 1 for ascending order \n and 2 for descending"<<endl;
int choice;
cin>>choice;
if(choice==1){
ascending(arr,10);
}
else{
if(choice==2){
descending(arr,10);
}
}
for(int x=0;x<10;x++){
cout<<x+1<<" = "<<arr[x]<<endl;
}
}
void ascending(int arr[],int size){
for(int x=0;x<size;x++){
for(int y=0;y<size-x-1;y++){
if(arr[y]>arr[y+1]){
int temp=arr[y];
arr[y]=arr[y+1];
arr[y+1]=temp;
}
}
}
}
void descending(int arr[],int size){
for(int x=0;x<size;x++){
for(int y=0;y<size-x-1;y++){
if(arr[y+1]>arr[y]){
int temp=arr[y];
arr[y]=arr[y+1];
arr[y+1]=temp;
}
}
}


}




















No comments:

Post a Comment