Title: Homework due
1Homework 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
2Homework due
- Create a 2D array of m x n ints
- Fill the array with random numbers between 0 and
100 - Compute and report the average of the array
contents - Do this for all combinations of m 1, 10, 100
and n 1, 10, 100, 1000
3Homework special instructions
- Do not use any hard coded values or constants
when calculating loop indices - Use the techniques discussed for computing the
number of elements in rows/columns of an array - Due Thursday, next class meeting Ill look at
your results in class
4C
5Run time allocation
- Allocate an array of 20 ints
- int array20
- Allocates an array of 20 elements in main memory
at compile time - But what if we dont know how big the array
should be when we are compiling the program? - Java has no issues with this since everything is
run time allocate
6Run time allocation
- The dreaded P wordPointers!
- In C a pointer is nothing more than a variable
that holds an address in memory - Whereas most variables are themselves addresses
in memory and hold data - Its often referred to as an indirect address
7Regular (non-pointer) variables
int x 10 int y 20 int z 30 int a3
0
8Pointer variables
int px int py int pz int pa
?
- Notes
- int x is the same as int x
- int x, y declares 1 pointer (x) and 1
non-pointer (y) variable
9Pointers what do you do with them?
int px int py int pz int pa int x
10 int y 20 int z 30 int a3 0 px
x py y pz z pa a
px
x
py
y
pz
z
pa
a
- You can use them to refer to (point to) other
variables - The is called the address-of operator and
returns the address of the variable it is
operating on - Note that the array name is already and address
and needs no
10Pointing to other variables
- These assignments will change the contents of the
variables to whom they point - x is now 27
- y is now 28
- x is now 29
- a now holds 30, 31, 32
- The is called the indirection operator and
refers to the contents of the variable it is
operating on - Note that the array index operators and
perform a similar task on array type variables
px 27 py 28 pz 29 pa0 30 pa1
31 pa2 32
11Why?
- Typically, you wont use pointers to access
individual (primitive) variables - You might use pointers to access arrays
- At one point in time it used to be faster to do
so rather than use indexing not clear if this
is still true - You could use an array of pointers to create a
ragged edged array (like you can do in Java) - The real value of pointers is in dynamic, run
time memory allocation
12Array access though pointers
- int a10 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
- for (int i 0 i lt 10 i)
- stdcout ltlt ai ltlt stdendl
-
- int pa a
- for (int i 0 i lt 10 i)
- stdcout ltlt pa ltlt stdendl
- pa
-
- The tricks
- Pointer arithmetic (like pa) is smart enough to
know how much to add to the pointer (address) to
get to the next variable of the appropriate type - e.g. if pointing to an int it adds 4, if pointing
to a char it adds 1, if pointing to a double it
adds 8 - Similar for things like pa 2 (adds 8 for an
int, 2 for a char, 16 for a double) - Be sure to initialize the pointer properly
- Know how far you can go before overrunning the
end of the array
13Dynamic memory allocation
- Its really quite simple
- int x 5
- int pa new intx
- for (int i 0 i lt x i)
- (pa i) i
-
- delete pa
- Note that you have to keep track of the size
sizeof(pa) / sizeof(pa0) will not work
14What not to do
- int x 5
- int pa new intx
- for (int i 0 i lt x i)
- pa i
- pa
-
- delete pa
- Why is this a very, very, very bad thing to do?
15To fix it
- int x 5
- int pa new intx
- for (int i 0 i lt x i)
- (pa i) i
-
- delete pa
16Dynamic multi-dimensional arrays
- Basically, you need to dynamically allocate an
array of pointers - The method shown in the book is (in my opinion)
hooky at best - Heres how I do it
17Dynamic multi-dimensional arrays
int rows 4 int cols 5 // -- requires a
pointer to a pointer int ma // -- requires
two separate allocations // and some pointer
assignments ma new introws ma0 new
introws cols for (int i 1 i lt rows i)
mai mai - 1 cols
18Dynamic multi-dimensional arrays
// -- now you can access it as a normal 2D
array for (int i 0 i lt rows i) for (int
j 0 j lt cols j) maij i cols
j for (int i 0 i lt rows i) for
(int j 0 j lt cols j) stdcout ltlt
maij ltlt "\t" stdcout ltlt stdendl
19Dynamic multi-dimensional arrays
// -- you have to delete both allocations in //
reverse order delete ma0 delete ma
20Homework
- Same as the last assignment but this time use
dynamically allocated arrays and ask the user how
big they should be - i.e. you only need to write and compile 2
programs, 1 for the 1D case and 1 for the 2D case - All the different run sizes will be handled at
run time based on user inputs - Due next class period