Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Linear Arrays
2.1.
Initialization of Arrays
2.1.1.
Passing No Value
2.1.2.
Passing Values
2.1.3.
Passing values, not size
2.2.
Different Array Operations
2.2.1.
Inserting the elements
2.2.2.
Accessing the elements
2.2.3.
Searching an element
3.
Record
3.1.
Creating a Record
3.2.
Different Record Operations
3.2.1.
Accessing the values of the record
3.2.2.
Updating the values of the record
4.
Difference Between Linear Array And A Record
5.
Frequently Asked Questions
5.1.
What are the advantages of arrays?
5.2.
What are the disadvantages of arrays?
5.3.
What is the default array value in JAVA?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Difference Between Linear Array And A Record

Author Kartik Singh
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

Data structures help implement physical forms of abstract data types. Data structures serve as building blocks of the software development process.

linear-array-vs-record

 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

array

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 initialize Array 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 ] = { };

 

Passing Values

We initialize by passing the values of the elements. We define the array size here.

SYNTAX:

int arr[ 3 ] = { 1, 2, 3 };

 

Passing values, not size

We initialize by passing the values of the elements. We do not define the array size here.

SYNTAX:

int arr[ ] = { 1, 2, 3, 4, 6};

Different Array Operations

We can perform the following operations on an array:

Inserting the elements

Array provides us with random access. Therefore, we can insert an element at a given index using the assignment operator.

For an 1D array, C++ Code is:

#include <iostream>

using namespace std;
int main() {

  int arr[5];
  arr[0] = 1;
  arr[1] = 2;
  arr[2] = 3;
  arr[3] = 4;
  arr[4] = 5;
  for (int i = 0; i < 5; i++) {
    cout << arr[i] << " ";
  }

  return 0;
}

 

OUTPUT:

1 2 3 4 5

 

Accessing the elements

Array provides us with random access. Therefore, we can access an element at a given index.

For a 1D array, C++ Code is

#include <iostream>

using namespace std;
int main() {

  int arr[5];
  arr[0] = 1;
  arr[1] = 2;
  arr[2] = 3;
  arr[3] = 4;
  arr[4] = 5;
  cout << arr[2] << " ";
  cout << arr[4] << " ";

  return 0;
}


OUTPUT:

3 5

 

Searching an element

We can traverse the array in linear time(1 D array) to search for an element. 

For a 1D array, C++ Code is

#include <iostream>

using namespace std;
int main() {

  int arr[5];
  arr[0] = 1;
  arr[1] = 2;
  arr[2] = 3;
  arr[3] = 4;
  arr[4] = 5;
  
  for (int i = 0; i < 5; i++) {
    if (arr[i] == 4) {
      cout << "Element is present ";
      return 0;
    }
  }
  
  cout << "Element is not present ";
  return 0;
}


OUTPUT:

Element is present
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Record

record

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;
}

Different Record Operations

Accessing the values of the record

Here is a C++ implementation of accessing the values of a record. 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;
  cout << "Student name: " << person.name << endl;
  cout << "Student age: " << person.age << endl;
  cout << "Student class: " << person.standard;
  return 0;
}


OUTPUT:

Student name: Kartik
Student age: 18
Student class: 12

 

Updating the values of the record

Here is a C++ implementation of updating the values of a record.

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;
  cout << "Student name: " << person.name << endl;
  cout << "Student age: " << person.age << endl;
  cout << "Student class: " << person.standard << endl;
  person.age = 15;
  person.standard = 9;
  cout << "Updates values " << endl;
  cout << "Student name: " << person.name << endl;
  cout << "Student age: " << person.age << endl;
  cout << "Student class: " << person.standard;
  return 0;
}

 

OUTPUT:

Student name: Kartik
Student age: 18
Student class: 12
Updates values 
Student name: Kartik
Student age: 15
Student class: 9

Must Read Array of Objects in Java

Difference Between Linear Array And A Record

Here is a table that tells what the difference between linear array and a record is, based on various parameters.

Array

Record

A data structure that stores finite data of the same data types. A data structure that are used to store a collection of variables of different data types.
The array has numbered memory locations. The record has fields that are named.
Every array memory location points to the same data type. Every field in a record can point to variables of different data types.
The memory locations of an array are numbered. The record has fields that are named.

Linear arrays allocate a contiguous block of memory for all its elements.

 

Records, on the other hand, allocate memory for each field separately. 
Insert, Access, and searching are common array operations. Access and updation are common record operations.
Built in almost all programming languages. Built-in in some and are implemented using class or structures.

Also see,  Rabin Karp Algorithm

Frequently Asked Questions

What are the advantages of arrays?

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

 

Check out some amazing Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Basics of C, Basics of Java, etc., along with some Contests and Interview Experiences only on Coding Ninjas Studio

Check out The Interview Guide for Product Based Companies and some Popular Interview Problems from Top Companies, like  Amazon, Adobe, Google, etc., on Coding Ninjas Studio.

Cheers!

Live masterclass