Homework due. Test the random number generator Create a 1D array of n ints Fill the array with random numbers between 0 and 100 Compute and report the average of the array contents Do this for n = 10, 100, 1000, 10000, 100000. Homework due. Create a 2D array of m x n ints
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
C++
Arrays – Part II
int array[20];
x
10
y
20
z
30
a[0]
0
a[1]
0
a[2]
0
int x = 10;
int y = 20;
int z = 30;
int a[3] = {0};
px
py
pz
pa
int *px;
int *py;
int *pz;
int *pa;
?
x
10
y
20
z
30
a[0]
0
a[1]
0
a[2]
0
int *px;
int *py;
int *pz;
int *pa;
int x = 10;
int y = 20;
int z = 30;
int a[3] = {0};
px = &x;
py = &y;
pz = &z;
pa = a;
px
x
py
y
pz
z
pa
a
*px = 27;
*py = 28;
*pz = 29;
pa[0] = 30;
pa[1] = 31;
pa[2] = 32;
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
for (int i = 0; i < 10; ++i) {
std::cout << a[i] << std::endl;
}
int *pa = a;
for (int i = 0; i < 10; ++i) {
std::cout << *pa << std::endl;
++pa;
}
int x = 5;
int *pa = new int[x];
for (int i = 0; i < x; ++i) {
*(pa + i) = i;
}
delete [] pa;
int x = 5;
int *pa = new int[x];
for (int i = 0; i < x; ++i) {
*pa = i;
++pa;
}
delete [] pa;
int x = 5;
int *pa = new int[x];
for (int i = 0; i < x; ++i) {
*(pa + i) = i;
}
delete [] pa;
int rows = 4;
int cols = 5;
// -- requires a pointer to a pointer
int **ma;
// -- requires two separate allocations
// and some pointer assignments
ma = new int*[rows];
ma[0] = new int[rows * cols];
for (int i = 1; i < rows; ++i) {
ma[i] = ma[i - 1] + cols;
}
// -- now you can access it as a normal 2D array
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
ma[i][j] = i * cols + j;
}
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
std::cout << ma[i][j] << "\t";
}
std::cout << std::endl;
}
// -- you have to delete both allocations in
// reverse order
delete [] ma[0];
delete [] ma;