CPP
cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main () {
int *ptr=NULL,n,invs=0;
cout<<"enter the number of elements in array"<<endl;
cin>>n;
cout<<"You have entered:"<<n<<endl;
ptr=new int [n];
srand(time(0));
cout<<"array elements are:"<<endl;
for (int x = 0; x <n; x++) {
ptr[x]=rand() % 99;
cout<<ptr[x]<<",";
}
cout<<endl;
for (int x = 0; x <n-1; x++) {
for (int i= x+1; i <n; i++) {
if(ptr[x]>ptr[i]){
invs++;
cout<<"("<<ptr[x]<<","<<ptr[i]<<")"<<endl;
}
}
}
cout <<"this array has "<<invs<<" inverses"<<endl;
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run