7.1 What is a Structured Data Object and how is it different than an Array?

 

 

 

 

Array

An array is a data structure that allows storage of a sequence of values. The values are stored in a contiguous block of memory with the same data type. Arrays allow fast random access to particular elements. If the number of elements is indefinite or if insertions are required a linked list may be a more appropriate data structure.

Example:

int examplearray[100]; //This declares an array

This would make an integer array with 100 slots, or places to store values (also called elements). To access a specific part element of the array, you put the array name and, in brackets, an index number. This corresponds to a specific element of the array.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Structures

 

Structures are a way of storing many different variables of different types under the same name. This makes it a more modular program, which is easier to modify because its design makes things more compact. It is also useful for databases.

 

 

struct database
{
  int id_number;
  int age;
  float salary;
};
int main()
{
  database employee;  //There is now an employee variable that has modifiable 
                                                  //variables inside it.
  employee.age=22;
  employee.id_number=1;
  employee.salary=12000.21;
  return 0;
}

The struct database declares that database has three variables in it, age, id_number, and salary.

You can use database like a variable type like int. You can create an employee with the database. Then, to modify it you call everything with the 'employee.' in front of it. You can also return structures from functions by defining their return type as a structure type

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Questions:

 

Arrays

 

  1. Which of the following correctly declares an array?      
    A. int anarray[10];
    B. int anarray;                                                Answer: A
    C. anarray{10};
    D. array anarray[10];
  2.  What is the index number of the last element of an array with 29 elements?
    A. 29
    B. 28                                                               Answer: B
    C. 0
    D. Programmer-defined
  3.  Which of the following is a two-dimensional array?
    A. array anarray[20][20];
    B. int anarray[20][20];                                   Answer: B
    C. int array[20, 20];
    D. char array[20];

 

Structure

 

     1.    Which of the following is a properly defined struct?
            A. struct {int a;}
            B. struct a_struct {int a;}                                Answer:  D
            C. struct a_struct int a;
            D. struct a_struct {int a;};

 

     2.    How types of data types can a struct have?

 

 A. char

 B. int                                                              Answer:  D

 C. float

 D. or all of the above