-
Notifications
You must be signed in to change notification settings - Fork 0
/
LAB6.cpp
198 lines (161 loc) · 2.79 KB
/
LAB6.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//TASK 1:
#include<iostream>
using namespace std;
int main()
{
//a)
// Pointer is not pointing any address. Initialize it first.
int* number, n;
number = &n;
cout << number << endl;
//b)
// Data types of pointers are different. Pointer can only point to address of variable of same data type.
double* realPtr;
double reaPtr = 6.98;
realPtr = &reaPtr;
double* integerPtr;
integerPtr = realPtr;
//c)
// Pointer cannot store the value of variable. It must point just to an address
int* x, y;
x = &y;
//d)
// Loop was not correct.
char s[] = "this is a character array";
for (int i = 0; s[i] != '\0'; i++)
{
cout << *s << ' ';
}
//e)
// Data types of pointers are different. Pointer can only point to address of variable of same data type and initialize it.
short* numPtr, result;
short nuPtr=6.98;
numPtr = &nuPtr;
short* genericPtr = numPtr;
result = *genericPtr + 7;
//f)
// Simple variable cannot cannot copy directly from an address. First save value to another variable.
double x = 19.34;
double xPtr = x;
cout << xPtr << endl;
}
//TASK 2:
#include<iostream>
#include<fstream>
using namespace std;
void Copy(char arr[5])
{
char c1Array[5];
fstream obj;
obj.open("copy.txt", ios::out);
for (int i = 0; i < 5; i++)
{
c1Array[i] = arr[i];
}
for (int i = 0; i < 5; i++)
{
obj << c1Array[i];
}
obj.close();
}
int main()
{
char cArray[5];
for (int i = 0; i < 5; i++)
{
cin >> cArray[i];
}
char cPtr[5];
for (int i = 0; i < 5; i++)
{
cPtr[i] = cArray[i];
}
Copy(cPtr);
}
//TASK 3:
#include<iostream>
using namespace std;
void print(float arr[10])
{
for (int i = 0; i < 10; i++)
{
cout << arr[i] << endl;
}
}
int main()
{
float* a;
float* floPtr = new float[10];
for (int i = 0; i < 10; i++)
{
cout << "Enter array: ";
cin >> floPtr[i];
}
print(floPtr);
delete floPtr;
}
//TASK 4:
#include<iostream>
using namespace std;
int* SortFun(int arr[], int size)
{
int* cpyArr = new int[size];
for (int i = 0; i < size; i++)
{
cpyArr[i] = arr[i];
}
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
if (cpyArr[j] < cpyArr[j + 1])
{
int temp = cpyArr[j];
cpyArr[j] = cpyArr[j + 1];
cpyArr[j + 1] = temp;
}
}
}
return cpyArr ;
}
int main()
{
int n;
cout << "Enter size of array: ";
cin >> n;
int* arr = new int[n];
for (int i = 0; i < n; i++)
{
cout << "Enter array: ";
cin >> arr[i];
}
int* ptr = SortFun(arr, n);
for (int i = 0; i < n; i++)
{
cout << ptr[i];
}
}
//TASK 5:
#include<iostream>
using namespace std;
int main()
{
int* ptr = new int[999], n;
for (int i = 0; i < 999; i++)
{
cout << "Enter Array: ";
cin >> n;
if (n == -1)
{
int* ptr1 = new int[i];
for (int i = 0; i < i; i++)
{
ptr1[i] = ptr[i];
}
}
else
{
cin >> ptr[i];
}
}
}