-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew_operator.cpp
More file actions
38 lines (38 loc) · 1003 Bytes
/
new_operator.cpp
File metadata and controls
38 lines (38 loc) · 1003 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
/*
* new_example.cpp
*/
/*
? dynamic allocation
? is the same as C => int * array= malloc(n*sizeof(int))
? or using the operator new => int *array= new int [n]
? Instead,
? to deallocate we can use
? free(array) or delete []array
*/
#include<iostream>
void calc_max_min(int *a, int n, int *max, int *min){
*max= a[0];
*min= a[0];
for(int i=1; i<n; i++){
if(a[i] > *max)
*max= a[i];
if(a[i] < *min)
*min= a[i];
}
}
int main(int argc, char * argv[]){
std:: cout<< "Insert array size: ";
int n;
std::cin >> n;
int * array= new int[n];//? new is used for dynamic allocation of an array of integers of size n
for(int i=0; i<n; i++){
std::cout << "Insert element " << i << ": ";
std:: cin >> array[i];
}
int MAX, MIN;
calc_max_min(array, n, &MAX, &MIN);
std:: cout << "Max is: " << MAX << std:: endl;
std:: cout << "Min is: " << MIN << std:: endl;
delete [] array;
return 0;
}