Problem in the C++ tutorial.

Pages: 12
One thing I still don't understand though - wouldn't the one dimensional array be just one row of 15 values?


Yes. But rows and columns are just a framework that we humans use to understand stuff better. In reality, the array is really 60 bytes of memory (assuming a 4-byte int). Think of it as 15 contiguous ints. When we look at it as a 1-dimensional array, it is 1 row of 15 ints. When we look at it as a 2-dimensional array, it is 3 rows of 5 ints each.

What does the * do to it? Again, glossed over in the tutorial.\


An array can be represented in code by a pointer to its first element. Pointers is 2 topics later in the tutorial, and there is a section on "Pointers and Arrays". DON'T GLOSS OVER POINTERS!!!!
Isn't the tutorial misleading when it says they both output the data as (graphic), when the graphic is a 2-dimensional array?
closed account (Dy7SLyTq)
not neccesarily. when i made my dungeon crawler i used a dimensional array becuase thats what i was used to and it was easier to manipulate for me. however i treated it two dimensional and if i were to explain it to someone, it would be a two dimensional array
I don't get that at all.
Isn't the tutorial misleading when it says they both output the data as (graphic), when the graphic is a 2-dimensional array?


Be careful with your statements. The tutorial says:

None of the two source codes above produce any output on the screen, but both assign values to the memory block called jimmy in the following way:


Note: "None [sic] .... produce any output on the screen..."

The purpose of the example is not to show printouts (because there is none), but to show how memory is allocated in a 2-dimensional array. Note the statement further up the tutorial:

Multidimensional arrays are just an abstraction for programmers, since we can obtain the same results with a simple array just by putting a factor between its indices:


The abstraction for programmers is the key. You don't have to use it if you don't want to. If you don't want to use the abstraction, you can use the contiguous memory as a single dimensional array and do the math yourself.

To the computer it's just memory. The C++ compiler gives you the flexibility to look at it as either a 1-dimensional or 2-dimensional array--it's your choice. The example shows you how. The "graphic" that you mention just shows the entire memory used by the (either) array.

So, if you have data in your code that is conceptually a 3 x 5 array (e.g. scores on 5 tests for 3 students, weight for 3 people over 5 weeks, etc.), you can set up either a 1-dimensional or 2-dimensional array to handle it, and both use the same memory. as demonstrated in the examples.

The multi-dimensional array is a nice construct, but sometimes it gets a little cumbersome to use, or the syntax can get a bit hairy. In that case, it makes sense to treat it the memory as a single dimensional array and index into it manually. It depends on the application.

But, as demonstrated by this example, you can do both at the same time. You could have an int*[5] pointer (2-dimensional) (MAKE SURE YOU UNDERSTAND POINTERS!!!) and an int* pointer (1-dimensional) pointing validly to the same memory. You just need to understand how the memory is laid out--and that's what the example is showing you.
Last edited on
Topic archived. No new replies allowed.
Pages: 12