-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathissue15.c
More file actions
34 lines (28 loc) · 779 Bytes
/
issue15.c
File metadata and controls
34 lines (28 loc) · 779 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
// C++ program to Sort square of the numbers
// of the array
#include<bits/stdc++.h>
using namespace std;
// Function to sort an square array
void sortSquares(int arr[], int n)
{
// First convert each array elements
// into its square
for (int i = 0 ; i < n ; i++)
arr[i] = arr[i] * arr[i];
// Sort an array using "sort STL function "
sort(arr, arr+n);
}
// Driver program to test above function
int main()
{
int arr[] = { -6 , -3 , -1 , 2 , 4 , 5 };
int n = sizeof(arr)/sizeof(arr[0]);
cout << "Before sort " << endl;
for (int i = 0; i < n; i++)
cout << arr[i] << " " ;
sortSquares(arr, n);
cout << "\nAfter Sort " << endl;
for (int i = 0 ; i < n ; i++)
cout << arr[i] << " " ;
return 0;
}