1. Write a C++ function to sort an array of ten integer values in ascending order.
The function will accept two arguments-- a pointer that points to the array and the array size. The function returns a pointer that points to the sorted array.
Solution:
#include<iostream>
#include<conio.h>
using namespace std;
int *sortAsc(int *p, int size);
int main()
{
int arr[]={23,34,2,3,5,12,42,56,89,8};
int *p=sortAsc(arr,10);
//output the sorted array
int i;
for(i=0;i<10;i++)
cout<<*(p+i)<<endl;
getch();
return 0;
}
int *sortAsc(int *p, int n){
int i,j;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(*(p+j)<*(p+i))
{
int temp=*(p+j);
*(p+j)=*(p+i);
*(p+i)=temp;
}
return p;
}
2. Modify the solution of exercise 1 in order to sort the array in descending order.
Solution:
#include<iostream> #include<conio.h>
using namespace std;
int *sortAsc(int *p, int size);
int main()
{
int arr[]={23,34,2,3,5,12,42,56,89,8};
int *p=sortAsc(arr,10);
//output the sorted array
int i;
for(i=0;i<10;i++)
cout<<*(p+i)<<endl;
getch();
return 0;
}
int *sortAsc(int *p, int n){
int i,j;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(*(p+j)>*(p+i))
{
int temp=*(p+j);
*(p+j)=*(p+i);
*(p+i)=temp;
}
return p;
}
No comments
Post a Comment