-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointers.cpp
More file actions
56 lines (44 loc) · 907 Bytes
/
pointers.cpp
File metadata and controls
56 lines (44 loc) · 907 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
const int P = 3;
const int Q = 5;
void printMat(int* x, int nrows, int ncols)
{
for(int i = 0; i < nrows; i++)
{
for(int j = 0; j < ncols; j++)
cout << setw(2) << *(x+(i*ncols)+j) << " ";
cout << endl;
}
cout << endl;
}
int main()
{
/*int N;
cout << "Enter a integer: ";
cin >> N;
float *p = new float(N);
for (int i = 0; i < N; i++)
{
//*(p+i) = sqrt(i); //pointer arithmetic
p[i] = sqrt(i);
}
float sum = 0;
for (int i = 0; i < N; i++)
{
//sum += *(p+i); //pointer arithmetic
sum += p[i];
}
cout << "Sum of square roots: " << sum << endl;
*/
int mat[P][Q];
for(int i = 0; i < P; i++)
for(int j = 0; j < Q; j++)
mat[i][j] = 0;
int *pointer = (int*)mat;
for(int i = 0; i < P*Q; i++)
*(pointer+i) = i+1;
printMat(pointer,P,Q);
}