.Open pages 181-196 Advanced Data .Open Reading . Introduction The most useful part of the reading is the material on type conversions in section 5.7 and on multidimensional array in section 5.9. Using enumerated types makes you programs more readable, however, so you should look at section 5.8 -- it is not that difficult. I'm hoping to skimp on the rest of the reading. . 5.5 Complicated declarations and typedef I don't expect you to use this section in labs, quizzes, classes, or the final but typedef may be used later in the part of the book we are covering. It is definitely part of CSCI202. 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: .As_is void name(parameters).... .As_is 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. .Open 5.7 Type conversions . 5.7.1 Automatic type conversion Worth putting a PostIt note on this page! . 5.7.2 Explicit Type conversions and casts .As_is (typename) expression .As_is typename ( expression ) .Close . 5.8 Enumeration types These are a useful tool for making your programs more readable. They also tend to make your programs less buggy. The .Key enum statement declares a new data type. It can have any values you like! Syntax of an enumeration declaration: .As_is 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. .Open 5.8 Tables . Introduction A very powerful idea needed in lots of projects . 5.9.1 Multidimensional arrays Notice the syntax .As_is array[rows][columns] Do not write `array[row, col]`. It compiles, runs and gives you `array[col]`!. . 5.9.2 Tables with the help of vectors and matrices .Close 5.8 Tables . 5.10 Pairs I think we can skip this data type! Used in CSCI202. .Close Reading .Open Glossary cast::="to force a value in one data type to an equivalent value in a different data type". char::datatype=8 bit. enumeration::="To list a set of values and have them numbered...", a C/C++ data type. enum::lexeme, "Used to define a new type of int with constant names for values". .As_is enum Glass={empty, half_empty, half_full, full}; .Close .Open Questions . What are declarations A declaration reserves some storage and gives it a name. It may also put data in the storage. . Please explain the void parameter and its use in more detail If you declare a function like this .As_is .... fred (void )... then the compiler will object if you ever write any call of `fred` with data in it: .As_is ...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 .See ./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 .As_is int sum(int a[], const int N) .As_is { .As_is int sum =0; .As_is for( int* p = a; p < a+N; p++) .As_is sum = sum + *p; .As_is return sum; .As_is } . What does the command char do The keyword `char` is the name of a predefined type -- the computers standard character type -- these days coded as 8 bit ASCII. Because "char" is the name of a data type you use it primarily to declare variables and constants: .As_is char input; .As_is 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: .As_is char example ( char para ) { return para + 1; } . What is typedef The typedef key word (notice the spelling) is used to give a new name to a complex data type. Giving things well chosen names makes them easier to understand. You use "typedef" by writing it in front of any normal looking variable declaration: .As_is typedef vector 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 .As_is 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: .As_is light=0; .As_is ... .As_is light=1; .As_is ... .As_is 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: .As_is enum StopLight {red,yellow,green}; .As_is ... .As_is StopLight light; .As_is ... .As_is light=red; .As_is ... .As_is light=yellow; .As_is ... .As_is light=green; And if you find me doing somethin like .As_is light = light * green; you can be pretty sure I've made a mistake! Here more examples: .As_is enum TicTacToeSymbol {empty, oh, cross}; .As_is enum LifeState {dead, alive}; .As_is enum Month{jan, feb,mar,apr,may,jun,jul,aug,sep,nov,dec}; .As_is 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 .As_is const string tttout[3]={" ", "O", "X"}; So no I can output a particular place on the board like this: .As_is 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: .As_is 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. .As_is TicTacToe board[3][3]; // 9 cells in a square array .As_is LifeState plane[100][100]; // for conway's game of life You 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 .As_is for(int row=0; row < 3 ; row++) .As_is { .As_is for(int col=0; col < 3; col++) .As_is { .As_is cout << " " << tttout[ board[row][col] ] ; .As_is if(col < 2) .As_is cout << " | "; .As_is } .As_is cout << endl; .As_is if( row < 2 ) .As_is cout << "-----" << endl; .As_is } (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: .As_is 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! .As_is 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]. .Box Be happy you have multidimensional arrays. When I started out I didn't have them in my programming languages. I will not forget in hurry a friend who need a 5><5 square and declared 5 different arrays: .As_is 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 .As_is double a[25]; and had to mentally transcribe `a[r,c]` into .As_is a[ 5 * r + c ] every where... .Close.Box . If we want to combine 2 different arrays do the rules of matrix addition and mulitplication apply NO. Sadly, no. .... You can not add and multiply arrays in C++ unless you have included definitions of the operations. You'd have to hunt for these on the Internet. 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.... .Close . Next -- Quiz 7 + Lab 8 + Project 6 .See ./projects.html#P6 .Close