G
gdotone
I am reading "A Book on C" forth edition.
In chapter 6, the author discusses "The Use of typedef". The following is the examples given in the book:
#define N 3
typedef double scalar;
typedef scalar vector[N];
typedef scalar matrix[N][N];
The author explains that typedef is a mechanism to create new types. typedef, according to the author, increases the self-documenting ability of the program and using typedef is helpful when conceptually appropriate.
The author gives the following example of what could have been written instead of the other.
typedef vector matrix[N]; /* instead of the one below */
typedef scalar matrix[N][N];
My question:
In the statement: typedef vector matrix[N]; being that vector is typedef as a scalar array, this statement says the matrix[N] is an array of the array vector?
design question:
An example function is given which adds vectors, three actually in the book,
void add ( vector x, vector y, vector )
{
int i;
for ( i = 0; i < N; i++ )
x = y + z;
}
When a variable is declared to be of type vector, as in vector x, x is an array holding scalar values?
The idea to define or think in terms of vector(s) is because of the problemdomain?
So, if one were writing a chess game, one could say something like:
int N 8;
typedef int position;
typedef postion boardLocation[N]N];
I'm not chess player so forgive for not being more creative with the names of the variable, I think of a chess board containing squares which have a position and will hold a piece, like a king, queen, knight, etc. I have not gotten to enumerations in the book yet, so, I'll say that those pieces willhave a number associated with its kind.
First am I abstracting that correctly? I'm I think about it correctly, it doesn't seem so.
So, maybe all I have written is doesn't fit the problem correctly.
perhaps its enough to say:
typedef int position[N][N];
because that's what I think of when looking at a chess board. I mean that is what happens in the early code, but that code doesn't seem to fit the problem domain. position typed as an int? No, right. Because when you think ofthe position on a chess board its like and (row, column) thought, right?
I'm trying to follow the idea that one should leave the detail off until later in a program. (so to speak, write)
If this example doesn't lend itself well to discuss abstraction, please give another. I'll try to follow the example that you give.
Thanks a lot everyone.
In chapter 6, the author discusses "The Use of typedef". The following is the examples given in the book:
#define N 3
typedef double scalar;
typedef scalar vector[N];
typedef scalar matrix[N][N];
The author explains that typedef is a mechanism to create new types. typedef, according to the author, increases the self-documenting ability of the program and using typedef is helpful when conceptually appropriate.
The author gives the following example of what could have been written instead of the other.
typedef vector matrix[N]; /* instead of the one below */
typedef scalar matrix[N][N];
My question:
In the statement: typedef vector matrix[N]; being that vector is typedef as a scalar array, this statement says the matrix[N] is an array of the array vector?
design question:
An example function is given which adds vectors, three actually in the book,
void add ( vector x, vector y, vector )
{
int i;
for ( i = 0; i < N; i++ )
x = y + z;
}
When a variable is declared to be of type vector, as in vector x, x is an array holding scalar values?
The idea to define or think in terms of vector(s) is because of the problemdomain?
So, if one were writing a chess game, one could say something like:
int N 8;
typedef int position;
typedef postion boardLocation[N]N];
I'm not chess player so forgive for not being more creative with the names of the variable, I think of a chess board containing squares which have a position and will hold a piece, like a king, queen, knight, etc. I have not gotten to enumerations in the book yet, so, I'll say that those pieces willhave a number associated with its kind.
First am I abstracting that correctly? I'm I think about it correctly, it doesn't seem so.
So, maybe all I have written is doesn't fit the problem correctly.
perhaps its enough to say:
typedef int position[N][N];
because that's what I think of when looking at a chess board. I mean that is what happens in the early code, but that code doesn't seem to fit the problem domain. position typed as an int? No, right. Because when you think ofthe position on a chess board its like and (row, column) thought, right?
I'm trying to follow the idea that one should leave the detail off until later in a program. (so to speak, write)
If this example doesn't lend itself well to discuss abstraction, please give another. I'll try to follow the example that you give.
Thanks a lot everyone.