-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathSource.cpp
More file actions
44 lines (37 loc) · 908 Bytes
/
Source.cpp
File metadata and controls
44 lines (37 loc) · 908 Bytes
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <fstream>
#include <time.h>
using namespace std;
int main()
{
const int arrsize = 10;
int* arr = new int[arrsize];
srand((unsigned int)time(NULL));
cout << "Array arr: " ;
for (int i = 0; i < arrsize; i++)
{
arr[i] = (rand() % 10);
cout << arr[i] << " ";
}
cout << endl;
//Given the int array arr, generate another int array output.
//whose element indiciates how many elements in arr is smaller than arr[i].
//For example, if arr is given as [5,8,5,6,8,1,5,9,5,8],
//output should be [1,6,1,5,6,0,1,9,1,6].
int* output = new int[arrsize];
cout << "Array output: ";
for(int i=0;i<arrsize;i++)
output[i] = 0;
for(int i=0;i<arrsize;i++){
for (int j=i+1; j<arrsize; j++){
if(arr[i]>arr[j])
output[i] += 1;
else if(arr[i] < arr[j])
output[j] += 1;
}
}
for (int i = 0; i < arrsize; i++) {
cout << output[i] << " ";
}
return 0;
}