Answer to Question #55971 in C++ for rishabh

Question #55971
write a programme that will read 10 integers from user and store them in an array .implement array using pointer and print array element in ascending and descending order.
1
Expert's answer
2015-11-03T03:11:28-0500
Solution
#include <iostream>
#include <math.h>
#define N 10

using namespace std;
int main(int argc, char **argv)
{
int *arr=new int[N];
int i;
for(i=0;i<N;i++)
{
cout<<"Input element #"<<i<<": ";
cin>>arr[i];
}

//ordering
int imin,j;
for(i=0;i<N;i++)
{
imin=i;
for(j=i+1;j<N;j++)
if(arr[j]<arr[imin])
imin=j;
j=arr[imin];
arr[imin]=arr[i];
arr[i]=j;
}

//output:
cout<<"In ascending order: ";
for(i=0;i<N;i++)
cout<<arr[i]<<" ";

cout<<endl<<"In descending order: ";
for(i=0;i<N;i++)
cout<<arr[N-i-1]<<" ";
return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS