CSC161 2011S Imperative Problem Solving
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Reading: K&R 5.6-5.9
Please submit a question by 9 p.m. on Monday, 11 April 2011.
What's the point?
What is a two-dimensional array?
A standard array is one-dimensional
in that we draw it out
over a single axis. A two-dimensional array we envision as being drawn
in two dimensions, with a width (number of columns) and height (number of
rows).
It says that daytab is the first two-dimensional array we have dealt with. Then it follows up saying that a two-dimensional array is really a one-dimensional array. So what's the point?
Sometimes it's easier to think about your data in two-dimensional (or even three-dimensional) form. You're also learning a new notation for declaring and accessing the information, even though it's all stored as a one-dimensional array.
Why would one choose to use a fixed-dimensional array when it seems that it can all be done with pointers?
For the same reason that we use arrays instead of pointers: We want to allocate the appropriate amount of space for the values we're working with. Also, the notation is clearer.
What's the entire point of a multi-dimensional array when you can use a single array to solve the same types of problem?
Most of us find the multi-dimensional array notation clearer.
What the difference between a two-dimensional array and an array of pointers?
A two-dimensional array provides a fixed set of storage for all the values. An array of pointers gives you pointers to other areas of memory. I'll try to remember to draw a picture in class.
Which one is better a two-dimensional array or an array of pointers?
It depends on the context. If you know that it's in rectangular shape and you need to allocate space, you should use a two-dimensional array. If each row potentially has a different length, you're much better off with an array of pointers. In other cases, you'll need to weigh the differences. In 207, you'll learn cool ways to store spare matrices.
Why is it that we would need to use a two dimensional array instead of just using two different arrays?
Can you explain multi-dimensional arrays a bit better? I dont understand why it's better than just using two arrays.
Two arrays of fixed length are a 2xN array. Two-dimensional arrays support MxN arrays.
I'm not very clear on the difference in between arrays of pointers and "true" 2D arrays. What other important differences are there other than that pointer arrays can have varying sizes between rows?
It is noted in the reading that an advantage of pointer arrays over two-dimensional arrays is that pointer arrays can have "rows" of different length. Are there any other benefits to consider when deciding between pointer arrays and two-dimensional arrays, like efficiency in accessing memory storage, the amount of memory taken up, reassigning values, or anything along those lines?
Yes, I think you've covered most of them: Cost of storage and efficiency in accessing storage are the main ones. Ease of passing things from function to function is another issue. (It's much easier to pass an array of pointers than a two-dimensional array, since you have to specify the row length for a two-dimensional array.
Miscellaneous
Could we go through the sample code in section 5.6?
Yes. We'll talk about it on Wednesday.
What does it mean by overhead
?
Overhead
generally means resources required. These can be time
resources or space resources or both.
At the beginning of 5.6, it talks about the twin problems of complicated storage management and high overhead
. Can you please describe these potential problems?
Complicated storage management: Dealing with the fact that our strings change size is hard. Remember, you've allocated a fixed amount of memory to a string. If you try to put a longer string there, you'll screw something up. So you have to spend extra effort managing memory. High overhead: If you're moving a string elsewhere in memory, you need to copy each character. That's expensive in terms of computation time.
Is the pictorial representation of pointer arrays vs. multi-dimensional arrays in 5.9 supposed to indicate that pointer arrays take up less memory?
No. It's simply supposed to indicate that they are laid out in memory differently. There are many differences, including ease of indexing, number of pointers we have to follow, and so on and so forth.
Is qsort an example of selection sort?
No. qsort is the Quicksort routine, which we'll cover
some time in the next few weeks.
We have just learned about multidimensional arrays. Can we use them in our homework for this week?
Yes.
Can there be a billion(or another number)-dimensional array?
I'm not sure what the cap is on the number of dimensions, but it's pretty large.
Using an array of pointers, could you have an array with varying types (without an obnoxious amount of casting pointers)?
No. Arrays in C are primarily homogeneous. If you want to vary types, you need a lot of casting of pointers (or to use unions, which we have not yet covered).
Could you explain in more detail what static
does and how it is used?
static does a lot of different things, depending on the
context in which it is used. For global
variables, it means that
the variable is supposed to be local to the file. For local
variables,
it means that the variable is supposed to retain its value between function
calls.
Is it harder to catch a pointer bug or a multi-dimensional array bug?
It depends on the details of the bug. Pointer bugs can be particularly insidious in that the problem doesn't show up at the bug, but later in the code (e.g., because you've munged with memory that you then use elsewhere in the program).
Questions that should be answered in lab
For the following example from 5.9,
char aname[][15] = { "Illegal month", "Jan", "Feb", "Mar" };
How can the first set of brackets not have an integer in them? I thought a specific length was necessary for this type of array declaration..."
Questions Sam has not yet answered
In section 5.7, when seeing if the year is a leap year, how do they do this? It seems as though they have a pseudo if statement but I'm not sure how it works.
I don't understand the day_of_year or month_day sample code, could we go over it and talk about how it demonstrates the utility of multi-dimensional arrays?
What are rectangular multi-dimensional arrays
?
Questions Sam can't answer
Can lineptr point to the second line also?
Context! I need context.
What if I wanted to find the length of the second row. How would I do that?
Context! I need context.
If I type *temp[i] then the * is the row and
i is for the column?
If you use this as a variable (not as a declaration), then the
i gives the row and the * derferences the
first thing in that row. But I'm not sure if that's what you're asking.
I do not understand how multivariable functions work, like f[a][b]. Is it like calc?
f[a][b] isn't a function. I'm not sure what part of Calculus you're talking about.
Does there always need to be a comma between the parts of a multidimensional ararys?
Context! I need context. But usually you can use sets of brackets instead of commas. Most people find the commas clearer, but it also depends on the context in which someone is using it.
What is a multidimensional array? (the example seems to have regular ones).
I'm not sure what example you're talking about. I'm also not sure what
you mean by regular
. A multi-dimensional array is an array that has
more than one dimension. Essentially, we think about grouping data in
a grid of two or more dimensions.
[Skip to Body]
Primary:
[Front Door]
[Schedule]
[Piazzza]
-
[Academic Honesty]
[Instructions]
Current:
[Current Outline]
[Current EBoard]
-
[Current Assignment]
[Current Lab]
[Current Reading]
Groupings:
[Assignments]
[Documents]
[EBoards]
[Examples]
[Exams]
[Handouts]
[Labs]
[Outlines]
[Readings]
[Reference]
Related Courses:
[CSC195 2003S (Rebelsky)]
[CSC161 2009F (Coahran)]
[CSC161 2010S (Walker)]
[CSC161 2010F (Rebelsky)]
Misc:
[SamR]
[ISO]
[GNU Coding Standards]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue Apr 12 10:53:06 2011.
The source to the document was last modified on Tue Apr 12 10:53:04 2011.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CSC161/2011S/Readings/multi-arrays.html.