-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2.cpp
55 lines (49 loc) · 1.14 KB
/
2.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#define Size 10
#include <iostream>
using namespace std;
typedef int DataType;
typedef struct
{
DataType data[Size];
int length;
} Array;
void CreateArray(Array*& L, DataType a[Size]){
for(int i=0; i<Size; i++){
L->data[i] = a[i];
}
L->length = Size;
}
Array* DeleteNegative(Array*& L){
Array* A = (Array*)malloc(sizeof(Array));
int k = 0;
for(int i=0; i<L->length; i++){
if(L->data[i]>=0){
A->data[k] = L->data[i];
k++;
}
}
A->length = k;
return A;
}
void DispArray(Array* L){
for(int i=0; i<L->length; i++){
cout << L->data[i] << endl;
}
free(L);
}
int main(){
Array* L = (Array*)malloc(sizeof(Array));
DataType a[Size];
DataType element;
cout << "Please enter the 10 array elements one by one in a newline:" << endl;
for (int i=0; i<Size; i++){
cin >> element;
a[i] = element;
}
CreateArray(L, a);
cout << "The positive array is:" << endl;
DispArray(DeleteNegative(L));
cout << "As shown, the time complexity is O(n); the space complexity if O(n)" << endl;
free(L);
return 0;
}