Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Until now, every variable has represented a single entity, whether that entity be a real number, integer, logical value, character, etc. These types are known as simple types. There are times, however, when structured types are required. Two common structured types are the array and the record.
2. Variable
A variable is a named storage location that contains the value of the variable.
This definition of a variable includes the following.
Individual variables such as a0, a1, a2, etc.
Array variables such as a[0], a[1], a[2], etc.
Array record variables such as p[0].a, p[1].a, p[2].a, etc.
These forms are now covered in more detail.
We will now look at arrays and records.
Let us first look at the problem to be solved.
3. Program to show the problem
Consider a C program that uses a counter loop to read in a series of exactly 4 numbers and then print them in reverse order. Using individual variables, we cannot use a for or while loop as we need to remember each and every number so we can print them in reverse order. (That is the reason for arrays).
4. Individual variable declarations
We need 4 variables for 4 numbers. Here are the declarations for individual variables.
Here is the C code.
5. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
We need a better way as if there were 100 or 1000 or more numbers, this method of a variable for each number does not scale well and is not easy to code (takes a lot of code).
We need a way to clone variables.
6. Array declarations
An array is a structured data type consisting of elements of the same type that are accessed by an index expression.
Since an array variable is a variable, it needs to be declared. Here is the array declaration in C.
Note the use of the const (constant, named literal) for the known count. By convention, constants are written in all uppercase names.
This declaration statement declares variable a to be an array of 4 elements of type int indexed from 0 to 3.
7. Solution using arrays
Here is the C code.
This solution using arrays now easily scales up to sizes of 100, 1000, etc.
The constant is N.
The literal is 4.
8. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
9. Enumerated solution using arrays
Here is the above program unrolled to remove the for loops (and the constant N).
Here is the C code.
10. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
11. Record declarations
A record is a structured data type consisting of elements called fields of varying data types that are accessed by name.
This declaration statement declares variable p to be an array of 4 records of type Item with a named variable field called a.
12. Solution using records
Here is a solution using records. Note that there is only one named field x in the record structure Item. Often, there is more than one field in a record structure.
Here is the C code.
13. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.
14. Enumerated solution using records
Here is an enumerated solution using records.
Here is the C code.
15. Examples of input and output
Here are some examples of input and output for the above program code.
Here is an example input.
For the above example input, here is the expected output.