-
Notifications
You must be signed in to change notification settings - Fork 0
/
LAB3.cpp
194 lines (158 loc) · 3.05 KB
/
LAB3.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
//TASK 8:
#include <iostream>
using namespace std;
int main()
{
int firstvalue = 5, secondvalue = 15;
int *p1, * p2, ** p3, ** p4;
// p1 = address of firstvalue
p1 = &firstvalue;
cout << p1 << endl;
// p2 = address of secondvalue
p2 = &secondvalue;
cout << p2 << endl;
// p3 = address of firstpointr
p3 = &p1;
cout << p3 << endl;
// p4 = address of secondpointer
p4 = &p2;
cout << p4 << endl;
// value pointed by p1 = 10
*p1 = 10;
cout << *p1 << endl;
// value pointed by p2 = value pointed by p1
*p2 = *p1;
cout << *p2 << endl;
// p1 = p2 (address of pointer is copied or not)
p1 = p2;
cout << p1 << " " << p2 << endl;
// p3 =p4 (check address of pointer is copied or not)
p3 = p4;
cout << p3 << " " << p4 << endl;
// value pointed by p1 = 20
*p1 = 20;
cout << *p1 << endl;
// print firstvalue, secondvalue
cout << "First Value: " << firstvalue << endl;
cout << "Second Value: " << secondvalue << endl;
return 0;
}
//TASK 9:
#include<iostream>
using namespace std;
int main()
{
// (a) PART
double numbers[10] = { 0.0,1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9 };
// (b) PART
double* nptr, d;
nptr = &d;
// (c) PART
cout << "(c) Part:" << endl;
for (int i = 0; i < 10; i++)
{
cout << numbers[i] << " ";
}
cout << endl << endl;
// (d) PART
nptr = numbers;
nptr = &numbers[0];
// (e) PART
cout << "(e) Part:" << endl;
for (int i = 0; i < 10; i++)
{
cout << *(nptr + i) << " ";
}
cout << endl << endl;
// (f) PART
cout << "(f) Part:" << endl;
for (int i = 0; i < 10; i++)
{
cout << *(numbers + 1) << " ";
}
cout << endl << endl;
// (g) PART
cout << "(g) Part:" << endl;
for (int i = 0; i < 10; i++)
{
cout << nptr[i] << " ";
}
cout << endl << endl;
// (h) PART
numbers[4];
*(numbers + 4);
nptr[4];
*(nptr + 4);
// (i) PART
cout << "(i) Part:" << endl;
nptr = numbers;
cout << "&(nPtr + 8) = " << (nptr + 8) << endl;
cout << "*(nPtr + 8) = " << *(nptr + 8) << endl << endl;
// (j) PART
cout << "(j) Part:" << endl;
nptr = &numbers[5];
cout << "&(nPtr -= 4) = " << (nptr -= 4) << endl;
cout << "*(nPtr -= 4) = " << *nptr << endl << endl;
system("pause");
return 0;
}
//TASK 10:
#include <iostream>
using namespace std;
void print(int** a)
{
cout << "Array is :" << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << a[i][j] << " ";
}
cout << endl;
}
}
int main()
{
int** matrix = new int* [3];
for (int i = 0; i < 3; i++)
{
matrix[i] = new int[3];
}
cout << "Enter array: " << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> matrix[i][j];
}
}
print(matrix);
return 0;
}
//TASK 11:
#include<iostream>
using namespace std;
void print(int* a)
{
cout << "You entered Elements : ";
for (int i = 0; i < 5; i++)
{
cout << a[i] << " ";
}
}
int main()
{
int* a;
//(1)
a = new int[5];
//(2)
cout << "Enter array: " << endl;
for (int i = 0; i < 5; i++)
{
cin >> a[i];
}
//(3)
print(a);
system("pause");
return 0;
}