Array Data Type
1. Array
If we discuss about the array, to be familiar with the basic concepts, we can analogize with the followingexample:
Figure 5.1 Illustration image of array in a data value
1. Step 1 : Array Declaration and Initialization, Forms a variable of type array
first, including the name, size, and content of data type.
Type name [element];
or
Type name [element] =
{element1,element2,element_n};
for example :
Sequence of value above can be declared using the array type :
int nilai [6];
or
int nilai [6] = {80,90,95,80,85};
2. Step 2 : Accessing Array, Perform operations on each element of the data set individually. Format array access is defined as follows.
name [index];
For example, to store the value of the second row, 90, of these data, it can be written in the following form
nilai[1] = 90;
The order of elements in the array always starts from 0, then the value of 90 referred by the first index, while the value of 97 displayed by the third index
read nilai[3];
2. Multidimensional arrays
Matrix, is an array that have 2 or more rows and also 2 or more columns, or more precisely, is an array that is inside another array, depending on how its use. For example, there is a twodimensional array with size 2x2 is shown in the followingexample:
Figure 5.2 Illustration of the matrix
1) Step 1 : Array Declaration and Initialization,
Type name [rows_element][column_element];
Int nilai[3][4];
2) Step 2 : Accessing Array,
name [row_index][column_index];
For example, to store values in the second row and the third column, it can be written as follows
nilai[1][2] = 80;
To display the values 90 in the third row and the second column can be written as
follows :
read nilai[2][1];
3. Array of Char
In C++ string data type can be obtained by defining an array of char. Because it is essentially strings is a collection of variable of type char. Although the form of an array, the treatment of an array of type char a little more special. Since there are several functions that can be used to directly manipulate this array. While on an array of non char there is no function to manipulate. Functions for an array of type char is placed in file include string.h.How to declare a string? Just like with a regular array but the data type is char.
Here's an example
Figure 5.3 Source code example using Array Data Type
Figure 5.4 Display of Running Program using Array Data Type
Functions in string.h
Just as with the other include files, string.h is located in / usr / include. We will see what functions are supported by string.h../* Copy SRC to DEST. */
extern char *strcpy (char *__restrict __dest, __const char*__restrict __src)
__THROW;
/* Copy no more than N characters of SRC to DEST. */
extern char *strncpy (char *__restrict __dest,__const char *__restrict __src, size_t __n)
__THROW;
/* Append SRC onto DEST. */
extern char *strcat (char *__restrict __dest, __const char*__restrict __src)
__THROW;
/* Append no more than N characters from SRC onto DEST. */
extern char *strncat (char *__restrict __dest, __const char*__restrict __src,size_t __n)
__THROW;
/* Compare S1 and S2. */
extern int strcmp (__const char *__s1, __const char *__s2)
__THROW __attribute_pure__;
/* Compare N characters of S1 and S2. */
extern int strncmp (__const char *__s1, __const char *__s2,size_t __n)
__THROW __attribute_pure__;
/* Return the length of S. */
extern size_t strlen (__const char *__s)
__THROW__attribute_pure__;__END_NAMESPACE_STD
These seven functions is the most widely used for manipulating strings. If you are using Microsoft Visual C++, there are more strings manipulation functions, such as strstr, strupr, and strlwr.
Table 5.1 Default Function to processing words
The above functions can be used in string type. In the previous example, we display the word cout BELAJAR by doing one at a time from the array elements. To be practical, we will change the previous example with the following program :
Figure 5.5 Source code example using Array Data Type
Figure 5.6 Display of Running Program using Array Data Type
Capturing Character Input
Some time we may require our program to capture input from the user in the form of characters. To capture the characters, we can use the same command as capturing input of integer or float. Here's an example program :
Figure 5.7 Source code example to capture character input
Figure 5.6 Display of Running Program to capture character input
cin >> kata1;
Although kata1 is an array but accessing char array type can omit the index information. This facility specifically for arrays of char only. On line 10 as well . To display a string <array of char> not need to display one at a time, but can display all at once by omiting array index is concerned.
Row 12 shows how to use strncpy function. The contents of the array character copied from array kata1 to larik3 just as much as 3 characters, then display its contents. On line 14 the contents of kata1 and kata3 is compared. But the comparison was limited to 3 characters. The result is a value of 0 which means 'true'. Finally, display the size of the array kata2 which amounted 7 characters.
The conclusion, Ways access an array of char with the non char is different. C++ provides special facilities for an array of char. This is to facilitate programmers of using string data type. Accessing by eliminating the array index. The secret is, every char array that we created is automatically added null character by the C++ which means mark the end of the string..
Like the contents of kata2, by the C++ memory allocation to be :
the last character is the final mark of the kata2 string. Therefore if done cout then C + + will display the contents of the array by looping kata2 until a null character is found. That why array index when accessing array of char can be removed. Because of its benchmark to the null character.
5.4. Exercise
1. Create a program to sumthe numbers that inserted by the user.
2. Create a program to convert decimal numbers into binary. The conversion results can be directly displayed or stored in an array of char variable
Struct Data Type
If in the previous case, namely the array, each component uses the same data type, it is different in a struct that may has components that have different data types. For example, a record of one's data, which consists of names, addresses, ages and department. All data is collected in a single record with the name, address, age, and department as its fields. So this record is an extension of the array data type.1 Struct declaration
Form of a prior record, covering the fields that are in the record along with the data type for each field.struct structName {
datatype fieldName1;
datatype fieldName2;
datatype fieldName3;
};
e.g :
struct StudentRec {
string name;
string idNum;
float gpa;
};
Form variable with the record type.
structName variableName;
e.g :
StudentRec theStudent;
2 Accessing struct
Perform operations on each element of the individual record. For example, the operation to fill the value of each element. Specific values can be assigned, with the rules to reference field of a record.variable_name.field_name = nilai;
e.g :
theStudent.name = "Sally";
or
cin >> theStudent.idNum;
Show data
cout<<variable_name.field_name;
e.g :
cout<< theStudent.gpa;
3 Nested struct
Elements of a struct can also be another struct. An example can be seen below.struct fullname {
string firstname;
string lastname;
};
struct StudentRec {
fullname name;
string idNum;
float gpa;
};
4 Array of Struct
Elements of an array can also be a struct. An example can be seen below :struct fullname {
string firstname;
string lastname;
};
struct StudentRec {
fullname name;
string idNum;
float gpa;
};
StudentRec theStudent[10];
1. Task 1 : Make a program to store personal information with struct.
1. Step 1 : Struct Declaration
struct fullname {
string firstname;
string lastname;
};
struct StudentRec {
fullname name;
string idNum;
float gpa;
};
2. Step 2 : Variable declaration and initialization
StudentRec theStudent[10];
3. Step 3 : Process
int main() {
cout<<”Masukkan banyaknya mahasiswa : ”;
cin>>n;
cout<<”Data mahasiswa”<<endl;
for (int i=0;i<n;i++) {
cout<<”Nama depan : “;
cin>>theStudent[i].name.firstname;
cout<<”Nama belakang : “;
cin>>theStudent[i].name.lastname;
cout<<”Nim : “;
cin>>theStudent[i].idNum;
cout<<”IPK : “;
cin>>theStudent[i].gpa;
}
4. Step 4 : Finalization
cout<<”Data mahasiswa”<<endl;
for (int i=0;i<n;i++) {
cout<<theStudent[i].name.firstname;
cout<<theStudent[i].name.lastname;
cout<<theStudent[i].idNum;
cout<<theStudent[i].gpa;
}
return 0;
}
Figure 6.1 Source code example using struct
Task 2 : Program compilation and execution
Figure 6.2 Display of Running Program using struct
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.
For example :
enter the number of students : 1
enter the nim : 412
enter UTS value : 72
enter UAS value : 74
-------------
nim : 412
average : 73
EmoticonEmoticon