Write a C++ program that creates an array of 10
elements. The program should then take input in the
array from the user. After that, the program sorts the
array elements in descending order. The sorting code
should access array elements using pointer notation
#include <iostream>
using namespace std;
int main(){
int array[10];
cout<<"Input 10 integers\n";
for(int i = 0; i < 10; i++)
cin>>array[i];
for(int i = 0; i < 10; i++){
for(int j = i + 1; j < 10; j++){
if(*(array + j) > *(array + i)){
int temp = *(array + i);
*(array + i) = *(array + j);
*(array + j) = temp;
}
}
}
cout<<"Sorted in descending order\n";
for(int i = 0; i < 10; i++)
cout<<array[i]<<" ";
cout<<"\n";
return 0;
}
Comments
Leave a comment