Wednesday 15 June 2016

Array of Records

Array of Records - We have discuss about array and records, or struct on C++, on Algorithm and Data Structure 1 (Algorithms and Data Structures 1). Here, we just combine those two in one implementation. And actually, we have use array of records, maybe it is more appropriate to call it array of structs but we will stick to array of records as it is more well known, when we discuss about struct. So we will only discuss a little bit of it.

1. Declaring Arrays of Records:

To declare an array of records in C++, we need to declare the struct first and then we declare the array with the previous struct as type. We will see struct declaration from Algorithms and Data Structures 1 which is:

   struct structName {
      datatype fieldName01;
      datatype fieldName02;
      datatype fieldName03;
   };

and also the example of student record:

   struct StudentRec {
      string name;
      string idNum;
      float gpa;
   };

Likewise, we will recall array declaration from Algorithms and Data Structures 1:

  Type name [arraySize];

If we declare a 10-element array called students of type StudentRec, the declaration will be:

   StudentRec students [10];

This example, is what we call array of records.

2. Initializing Array of Records:

As in normal arrays, we can initialize array of records elements using a single
statement, assume we have some variable student1, student2, student3, student4,
student5 of StudentRec, as follows:

   StudentRec students[] = {student01, student02,student03, student04, student05};

or you can use one on one initialization as follows:

   students[0] = student01;
   students[1] = student02;
   students[2] = student03;
   students[3] = student04;
   students[4] = student05;

3. Accessing Array of Records Elements:

An element is accessed by indexing the array name and then accessing the record variable. For example:

   string tempName = students[4].name;

The above statement will take 4th element from the array of records, and take only name field from it, and assign the value to tempName variable. Following is an example, which will use all the above-mentioned three concepts, declaration, assignment and accessing arrays of records:

   #include <iostream>
   using namespace std;
   struct fullname {
      string firstName01;
      string lastName02;
   };
   struct StudentRec {
      fullname name;
      string idNumber;
      float gpa;
   };
   int main ()
   {
      StudentRec students[10]; // students is an array of records of // 10 StudentRec
   cout << "Input Student Records" << endl;
   // initialize elements of array of records from user input

   for ( int i = 0; i < 10; i++ )
   {
      cout << "First name01 : ";
      cin >> theStudent[i].name.firstname01;
      cout << "Last name02 : ";
      cin >> theStudent[i].name.lastname02;
      cout << "Student Number : ";
      cin >> theStudent[i].idNum;
      cout << "GPA : ";
      cin >> theStudent[i].gpa;
   }
   cout << "Student Records : " << endl;
   // output each array element's value
   for ( int j = 0; j < 10; j++ )
   {
      cout << "\tName : " << theStudent[i].name.firstname01 <<" "<< theStudent[i].name.lastname02;
      cout << "\tStudent Number : " << theStudent[i].idNum;
      cout << "\tGPA : " << theStudent[i].gpa;
   }
   return 0;
   }

4. C++ Arrays of records in Detail:

As Arrays, array of records are important to C++ and should need lots of more detail. There are following few important concepts, which is actually arrays concepts, which should be clear to a C++ programmer:

Multi-dimensional arrays of records : C++ supports multidimensional arrays, and so array of records. The simplest form of the multidimensional array is the two-dimensional array.

Pointer to an array of records : You can generate a pointer to the first element of an array of records by simply specifying the array name, without any index.

Passing arrays to functions : You can pass to the function a pointer to an array by
specifying the array's name without an index.

Return array from functions : C++ allows a function to return an array.

5. Three the disadvantages of arrays

The disadvantages of arrays


EXERCISE
1. Create a program (by using arrays and structs) which asks the student data (nim, UTS values and UAS value) of N inputs (N is determined by the users themselves, maximum 50), then calculate the average value of each student, the average of all student and which student that has grade below the average.


EmoticonEmoticon