In Class Exercises 20210128 Turing
In Class Exercises 20210128 Turing
January 1, 2021
Abstract
Today’s goal is that you know the primitive data types in C, and how
to create your own non-primitive datatypes.
1
Aloha BLM We are WPI
Figure 2: Some pins are for the address, and others are for data. Address pins
are countable.
3 A Memory Address
3.1 in Turing Machine
In a Turing machine, an address may be any of the natural numbers, starting
at 0. This is a countably infinite set.
4 A Pointer is an Address
In C, this address concept is very useful. It is renamed pointer. Think of the
control’s cursor in the Turing machine model diagram. The pointer “points
to” one sheet in the roll of paper towels. We associate the sheet in the roll of
paper towels with an addressable memory cell. Machines are typically byte-
addressable, so we associate the sheet in the roll of paper towels with a memory
cell containing 8 bits (called a byte). This means, the pointer corresponds to
the index, likewise the address, of that byte.
Pointers, addresses, are numbers. We can perform arithmetic with them.
Machines are byte addressable. If we add one to a byte address, we are
talking about the next byte.
LBGTQIA+
Aloha BLM We are WPI
Figure 3: The hotel staff point to a room. The room number, the “address”
of the room, is used to find the room. A hotel guest (Doe) is the content of
the room. When the name Doe is associated with room 123, room service can
be delivered to room 123 or Doe’s room, equivalently. If we add one to a hotel
room number, we are talking about the next room.
Addresses, pointers, are typed, that is, we declare them with a type, such as
int* for pointing to an integer. If we add one to an int*, we are talking about
the next integer.
5 Bytes
5.1 Binary Numbers, and Hexadecimal
Using the binary number system, it takes 4 bits to represent the numbers 0
through 15 inclusive. We like single digits, so after 0 through 9, we make use of
A through F to complete the idea of 0 through 15, but using single characters.
That way, we can also use 0 through 9, and A-F in the next “column”; this is
no longer the 10’s column, it is the 16’s column.
Fill in the remaining spots in the table. If you have any question about
how to do it, please ask. In subsequent lectures this material will be considered
background, so please take this opportunity to get it clarified.
LBGTQIA+
Aloha BLM We are WPI
Examples:
Word Decimal Binary Hexadecimal
Zero 0 0b0000 0x0
One 1 0b0001 0x1
Two 2 0b0010
Three 3 0b0011
Four 4 0b0100
Five 5
Six 6
Seven 7
Eight 8
Nine 9
Ten 10 0xA
Eleven 11 0xB
Twelve 12
Thirteen 13
Fourteen 14
Fifteen 15
It took 4 bits to represent 0 through 15 in the unit’s column, and 4 additional
bits to represent 0 through 15 in the 16’s column.
These 8 bits are called a byte, and they are the addressable unit in the
hardware.
Thought-provoking questions: Choose X the best answer(s).
LBGTQIA+
Aloha BLM We are WPI
LBGTQIA+
Aloha BLM We are WPI
00000000
and there is a “carry bit” that is one, which we can discard.
Thus, we added 1 to something, we got zero, so that something must be -1.
LBGTQIA+
Aloha BLM We are WPI
LBGTQIA+
Aloha BLM We are WPI
Figure 6: Where did I put (the key with) the room number? The address of /
pointer to where the room* is located is of type room**.
7 Variables
Variables do not have to remain constant. Pi remains constant, it is always
3.14159. . . . The amount of time before the next rain storm can be different.
When we have in mind (and in the computer) some information that could
change, and that we need to remember, we allocate some memory to hold that
information. That memory has an address, like hotel room 123, and we can
associate a name with it, like Doe. For the duration of Doe’s stay, Doe, and
guest at 123, are equivalent. No matter what datatype our byte, or group of
bytes, has, it is convenient to attach a name to it. You can write code that is
obscure to read by naming your data as var1, var2, var3 etc. Preferably, you
will choose names that convey something of the meaning of your data.
int numClasses = 5;
MovieTitle could be the name of a group of bytes that are being interpreted
as a list of characters. Of course there are movie titles of a single character. We
could write:
When we have a collection of data, each datum of the same type, we can use
an array. We might have a collection of movie titles that we like, and another
collection of those we don’t.
LBGTQIA+
Aloha BLM We are WPI
char goodMovies[100][50];
char badMovies[100][50];
Each collection allows 100 movies, the names of which may each be as long
as 49 characters. (The last character is reserved to be ‘\0’.)
Then, to select at an element in the collection, we can use an index.
char favoriteMovie[50];
strcpy(favoriteMovie, goodMovie[3]); copies the fourth goodMovie into the
favorite, because strcpy is defined that way.
One idea about variables is that some memory has to be reserved for them
(so, they will have an address as a result of that reservation). We can also keep
track of their address if we wish.
float my2pi = 3.141 *2;
The amount of memory reserved depends upon the datatype. Variables that
are double take more bytes that variables that are float.
8 Pointers to Variables
Suppose we have two variables:
float my2pi = 3.141 *2;
and
double myOther2pi = 3.14159 *2;
and
double* whereMyOther2piIs = &myOther2pi;
To access the memory just after that reserved for my2pi, the address of
my2pi is incremented:
*(whereMy2piIs+1)
*(whereMyOther2piIs+1)
LBGTQIA+
Aloha BLM We are WPI
LBGTQIA+
Aloha BLM We are WPI
10 Do It Yourself Datatypes
10.1 struct
Developers frequently have ideas that are expressible using aggregations of the
primitive datatypes in C. For example, contact information can include a per-
son’s name, address, birthday, shirt size, etc. Imagine have a datatype for
contact information, then a pointer to that datatype, and the ability to move
through a list of contacts by adding 1 to the pointer to the datatype.
LBGTQIA+
Aloha BLM We are WPI
Let’s make a simpler example to begin. Parts will be the same every time,
and the rest will be the decision of the developer.
typedef struct
{
int justACountingNumber;
double canHaveAFractionalPart;
char aLetterOrDigit;
long anIntegerThatCanBeBig;
} nameOfCustomType;
The first line, typedef struct, and the { after it, will be the same every
time.
The last line will always start with } will have some datatype name, and will
always end with a semicolon.
Here is an exercise. If you have any question about how to do it, please ask.
In subsequent lectures this material will be considered background, so please
take this opportunity to get it clarified.
Recall that complex numbers consist of two real numbers. For a real number,
use the C datatype double. Write a datatype for a complex number.
To declare a variable of any datatype that is a struct, use the datatype name
and give a variable name.
For example, suppose we have:
typedef struct
{
int justACountingNumber;
double canHaveAFractionalPart;
char aLetterOrDigit;
long anIntegerThatCanBeBig;
} Motley4;
To set values of m4, set the values of its component fields individually. For
example,
m4.justACountingNumber = 7;
m4.aLetterOrDigit = ’q’;
LBGTQIA+
Aloha BLM We are WPI
Practice: (If you have any question about how to do it, please ask. In
subsequent lectures this material will be considered background, so please take
this opportunity to get it clarified.)
Set values into variables of the type Complex you defined earlier.
We also might want to read values from the fields of a compound datatype.
Variable names to the left of the equal sign are getting assigned, just as when
we initialize. To read the value from a field, we identify the field we want on
the right hand side of the equal sign.
double someDouble = m4.canHaveAFractionalPart;
Practice: (If you have any question about how to do it, please ask. In
subsequent lectures this material will be considered background, so please take
this opportunity to get it clarified.)
Read a value from a field of a variable of the type Complex you defined
earlier, into a newly declared variable (which should be of the appropriate type).
typedef enum
{
Monday,
Tuesday,
Wednesday,
Thursday,
Friday = 100,
Saturday,
Sunday
}dayOfWeek;
LBGTQIA+
Aloha BLM We are WPI
(let us say, English) alphabet, and maps it to the letter offset by some integer.
Suppose we map a letter to the letter next after it. For ’z’, it becomes ’a’. So
we have a character, we want to think of it as a number, so that we can add 1
to it, so long as it is not a z. We can use the process of casting.
Practice: (If you have any question about how to do it, please ask. In
subsequent lectures this material will be considered background, so please take
this opportunity to get it clarified.)
1. Using the code above, change it so that the letter is initially some alpha-
betic character other than a, but not w,x,y or z also not W,X,Y or Z. Also,
add 3 to it.
Note that, in ASCII, capital letters start at A=65, and small letters start
at a=97. Write code that will change a capital letter into the correspond-
ing lower case letter.
2. Create a datatype that is an enumerated type. In the datatype, list all
WPI buildings in which you have a class this term.
LBGTQIA+