Purpose: to give your own name to a new data type.
Syntax: Just like a variable declaration.
Effect the name becomes the short hand name for a data type and can be used to declare variables and constants later in your program
I've must used it for horrible things like arrays of function pointers that you can
forget about for now.
5.6 The type void and void* -- voidstar
This is the weirdest data type in C++/C. It indicates the absence of data.
It is used in function definitions:
void name(parameters)....
Type name(void)....
The text also mentions something I don't want to talk about! C++ and C allows you to have pointers that indicate a single place in memory without saying what type of data is to be found there. They are declared using 'void*' and there is not much you can do with them.
Again let's skip this topic.
(typename) expression
typename ( expression )
The enum statement declares a new data type. It can have any values you like!
Syntax of an enumeration declaration:
enum name { list_of_values };
Notice the squiggly braces. Parentheses and square brackets do not work.
Output and input of enumerated values
However, the compiler codes the values as int's anyway. This means that if
you output an enumerated value you get a number. You have to convert it
to a string and a clever way to do this is on page 188 by using an array of char texts.
You can use an array of strings as well.
Page 189 shows a clever solution to the problem of inputting enumerated values. Again
the compiler converts values to integers. To be able to input the values you
must write code like get_enum that looks up a input string in the table of
values.
5.8 Tables
array[rows][columns]Do not write array[row, col]. It compiles, runs and gives you array[col]!.
. . . . . . . . . ( end of section 5.8 Tables) <<Contents | End>>
5.10 Pairs
I think we can skip this data type! Used in CSCI202.
. . . . . . . . . ( end of section Reading) <<Contents | End>>
Glossary
enum Glass={empty, half_empty, half_full, full};
.... fred (void )...then the compiler will object if you ever write any call of fred with data in it:
...fred (42);is now illegal.
Similarly, if a function is declared to return "void" then you may not
use it in an expression to get a value.
How are pointers and arrays useful in computer programming
First: arrays are a very common type of data. See below for examples, and
the book.
Second pointers provide a simple and fast way to access items in the array. In this class I refered back to [ 13.html#How do pointers and text strings work together ] in the previous class. Here is an example with an array of ints being added up by using a pointer
int sum(int a[], const int N)
{
int sum =0;
for( int* p = a; p < a+N; p++)
sum = sum + *p;
return sum;
}
char input;
const char QUOTES = '\"';These statements reserve a piece of primary memory and give it a temporary name. It is just big enough to contain one byte (8 bits) of information.
You can also use "char" to declare arrays of characters and to specify parameters and returned types from functions:
char example ( char para ) { return para + 1; }
You use "typedef" by writing it in front of any normal looking variable declaration:
typedef vector <int> intvect;and it totally changes the meaning. In stead of creating a new variable (storage, address, ...) it defines a new data type. You can then use it to declare variables
intvect example_int_vector;
Typedefs can make some declarations a lot easier to understand
However I don't intend to require them in any project, use them in
example, or test your knowledge of them in any quiz or final.
.What is the function of enumertaion types
They make programs more readable. See next Question.
How does an enum reduce bugs
Enumerated data makes you code more readable. It makes it
harder to do stupid things like forgetting how you coded a
particular value. It makes it easier to see a mistake. For example
if you saw:
light=0;
...
light=1;
...
light=2;you can only guess at what is in my mind. But if first create an enumerated type with good names you know precisely what I'm writing about:
enum StopLight {red,yellow,green};
...
StopLight light;
...
light=red;
...
light=yellow;
...
light=green;
And if you find me doing somethin like
light = light * green;you can be pretty sure I've made a mistake!
Here more examples:
enum TicTacToeSymbol {empty, oh, cross};
enum LifeState {dead, alive};
enum Month{jan, feb,mar,apr,may,jun,jul,aug,sep,nov,dec};
enum DayInWeek { mon, tue, wed,thu, fri, sat, sun };
When using enukeration types does the compiler assign a values to the different words
YES!
The compiler removes the names and replaces them by numbers so you don't have to learn anything new about how enumerated data works -- it is an int in disguise.
The normal numbers are: 0,1,2,3,....
You can specify any int you like for an enumerated value.
Can you explain the example on Page 188
This shows you a common technique that is used when you have an enumerated
type and want to input and output its values. The technique is to declare
an array of strings that show the outside view of the data -- inside the
computer enumerated values are just numbers. Then you use the array
to output them.
Example.... I declare TicTacToe to have three values: empty, oh, and cross. (see above). This does not help me output the values. So I also declare
const string tttout[3]={" ", "O", "X"};
So no I can output a particular place on the board like this:
cout << tttout[empty];
More below...
What is a multidimensional array
A one dimensional array is a row of items numbered [0], [1], [2], ...,
[SIZE-1]. Examples include an array of characters "abc" and this array
of ints:
const int DAYS_IN_MONTH[12]={31,28,31,30,31,30,31,31,30,31,30,31}; // not leap year
You access an element like this DAYS_IN_MONTH[jan].
Note: I used an enumerated value in the above example...
Multidimensional arrays have two or more subscripts. They have two or more dimensions. They are arrays of arrays.
Examples of two-dimensional arrays.
TicTacToe board[3][3]; // 9 cells in a square array
LifeState plane[100][100]; // for conway's game of lifeYou access elements in these arrays like this: board[0],[1], plane[24][42], etc.
Once you have got a TicTacToe board and a array tttout defining how to output the symbols you display a board like this
for(int row=0; row < 3 ; row++)
{
for(int col=0; col < 3; col++)
{
cout << " " << tttout[ board[row][col] ] ;
if(col < 2)
cout << " | ";
}
cout << endl;
if( row < 2 )
cout << "-----" << endl;
}(This code needs combining with previous snippets and testing...)
Another example is a single work sheet for a spreadsheet! In fact, most of the tables that users want see are two dimensional tables:
double sales[WEEKS][DAYS_IN_WEEK];You access the cells like this sales[8][fri]
Three diemnsional arrays are also common. A modern spreadsheet has many worksheets and each of those is a two diemnsional array of formulas. Similarly, many scientific models of objects -- a body, a bridge, a block of uranium, etc. are modeled by a large three-dimensional array. The cells in the array can store the ammount of bine, the stress, the radiation, etc. in the cell... and the computer can calculat what happens. This kind of volume modelling and computation is expensive and common!
double bone[10][10][10];declares an array of 1,000 cells organized like a cube. You access a particular cell like this bone[row][col][slice].
double a[5],b[5],c[5],d[5],e[5];And the pages and pages of special code for each row of the array... I went in a different direction. I declared
double a[25];and had to mentally transcribe a[r,c] into
a[ 5 * r + c ]every where...
Don't get me on the topic of prgramming matrix operations. I have had to do it in two different jobs and did four years of college work on them....