Blog about Programming Languages & Coding

Blog about Programming Languages & Coding
Contents for Computer Science, IT, B.Sc. CS & IT, M.Sc. CS & IT, MCA, BE CS & IT, ME CS & IT , Interview Questions, Books and Online Course Recommendations from Udemy, Coursera, etc

Arrays

 

What is Arrays Data Structure?


An array is a data structure that stores a collection of elements of the same data type in a contiguous block of memory. Each element in the array is identified by its index or position within the array, starting from 0.


Arrays can be one-dimensional, two-dimensional, or multi-dimensional, depending on the number of indices required to access each element. One-dimensional arrays are commonly used to store a list of items, while two-dimensional arrays are used to represent a matrix or a table.


The main advantage of arrays is that they provide fast access to individual elements, as elements can be accessed in constant time using their index. This makes arrays a popular choice for storing large amounts of data that need to be accessed quickly.


Some common operations that can be performed on arrays include:


Accessing an element: An element in the array can be accessed by its index.

Inserting an element: An element can be inserted at a specific index in the array.

Deleting an element: An element can be deleted from a specific index in the array.

Searching for an element: The array can be searched for a specific element.

Arrays have a fixed size, which means that once an array is created, its size cannot be changed. If additional elements need to be added to the array, a new array must be created with a larger size and the existing elements copied over.


Applications of Arrays:

Arrays have a wide range of applications in computer programming. Here are some common applications of arrays:


Storing and accessing collections of data: One of the most common applications of arrays is to store and access collections of data. Arrays provide a fast and efficient way to access individual elements in the collection.


Sorting and searching: Arrays are frequently used in sorting and searching algorithms. For example, many sorting algorithms such as quicksort, mergesort, and heapsort use arrays as the underlying data structure.


Matrices and tables: Two-dimensional arrays are often used to represent matrices and tables. This is particularly useful in scientific and engineering applications, where matrices are commonly used to represent systems of linear equations.


Image processing: Arrays are also used in image processing applications. Images can be represented as arrays of pixels, with each pixel represented by a value that corresponds to its color.


Dynamic programming: Dynamic programming algorithms often use arrays to store intermediate results. This is particularly useful in problems where subproblems must be solved and their results combined to solve the overall problem.


Audio processing: Arrays can be used to represent audio signals. The elements of the array correspond to the values of the audio signal at each point in time.


Graphs and networks: Arrays are used to represent graphs and networks. Each element of the array corresponds to a node in the graph, and the values in the array indicate the connections between nodes.


These are just a few examples of the many applications of arrays in computer programming. Arrays are a fundamental data structure that is used in many different types of algorithms and applications.


PYTHON PROGRAM TO IMPLEMENT INSERTION AND DELETION OPERATIONS ON ARRAY

Insertion Code:

arr=[]

n=int (input("how many elements you want?"))

for i in range(n):

    elt=int(input("Enter element:"))

    arr.append(elt)

print("------intitial arrays-------")

for i in range(n):

    print("array[",i,"]",arr[i])

 

pos=int(input("Enter position u want to add:"))

val=int(input("Enter element to be added:"))

j=n

arr.append(0)

n=n+1

while(j>pos):

    arr[j]=arr[j-1]

    j=j-1

arr[pos]=val

print("-------arrays after insertion------")

for i in range(n):

    print("array[",i,"]",arr[i])

OUTPUT:

Deletion Code:

arr=[]

n=int (input("how many elements you want?"))

for i in range(n):

    elt=int(input("Enter element:"))

    arr.append(elt)

print("------intitial arrays-------")

for i in range(n):

    print("array[",i,"]",arr[i])

 

pos=int(input("Enter position u want to delete:"))

j=pos

 

while(j<pos):

    arr[j-1]=arr[j]

    j=j-1

n=n-1

print("-------arrays after deletion------")

for i in range(n):

    print("array[",i,"]",arr[i])

OUTPUT:



Arrays Arrays Reviewed by Asst. Prof. Sunita Rai on April 09, 2023 Rating: 5

No comments:

Powered by Blogger.