Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Data structures help implement physical forms of abstract data types. Data structures serve as building blocks of the software development process.
Data structures are divided into two categories: Linear data structure. Non-linear data structure. The most popular data structures are arrays, linked lists, trees, graphs, and records.
In this article, we will discuss linear arrays and record in detail. We will also discuss what the difference between linear array and a record is.
Linear Arrays
An array is a collection of elements of the same type. The elements are placed in contiguous memory locations that are individually referenced. We use an index to reference an array element.
Initialization of Arrays
There are the following methods used to initializeArray in C++:
Passing No Value
We initialize an array without passing any value to its elements. We define the array size here.
SYNTAX:
int arr[ 10 ] = { };
You can also try this code with Online C++ Compiler
Records are data structures that are used to store a collection of variables. These variable fields inside the record have names and values. Thus, a record is a type of container for these fields.
Consider the example of a student's information. The name fields for student record include name, age, gender, class, and address. The value to these fields can be Jack, 19, Male, 12, and Indore. Thus, a student record would store all these fields and values. These can be accessed as a single entity. Records help to represent complex data types.
Creating a Record
In some languages, they have built structures that can be used as a record. In other languages like Python, C++, C#, etc. We use class to create a record.
General SYNTAX for creating records:
record(record_name, {field1, field2, . . fieldn})
In C++, records are created using class.
Code in C++
#include <iostream>
#include <string>
using namespace std;
class student {
public: string name;
int age;
int standard;
};
int main() {
student person;
person.name = "Kartik";
person.age = 18;
person.standard = 12;
return 0;
}
You can also try this code with Online C++ Compiler
It is easy to store elements in an array of the same data type. It helps in implementing stacks and queues. Using 2D arrays, we can conveniently represent matrices. Provides easy access to its elements.
What are the disadvantages of arrays?
The memory allocation is contiguous, so wastage of memory can occur. Prior knowledge of the array size is necessary to perform some array operations. Deletion and insertion of elements become tedious.
What is the default array value in JAVA?
JAVA assigns the default array value for int, short, byte, and long as 0. For float and double is 0.0. For boolean values, it is false and null for objects.
Conclusion
In this article, we learned about arrays and records. We discussed what the difference between linear array and a record is. We learned about the different operations that are supported by arrays and records.
To learn more about arrays and records, you can check out our other blogs