Average Value of Array
Average Value of Array
CAP4103
School of Engineering
Department of Computer Science and Engineering
Submitted By
Student Name Rakesh Dey sarkar
Enrolment Number 230160307023
Programme Masters in Computer Application
Department Computer Science and Engineering
Session/Semester 2023-2025/Second Semester
Submitted To
Faculty Name Ms. Sapna Sharma
Q1.What is Data Structure ? Explain Various Types of Data Structure
.
Ans: A data structure is a way of organizing and storing data to perform operations
efficiently. It defines a set of rules or conventions for organizing and managing data,
which allows for easier access, modification, and processing. Data structures are
essential components in computer science and are used in various applications to
solve different types of problems.
1. Arrays:
An array is a collection of elements, each identified by an index or a
key.
Elements in an array are stored in contiguous memory locations.
Accessing elements in an array is fast, O(1) time complexity.
Insertion and deletion operations can be slow, especially in the middle,
as shifting may be required.
2. Linked Lists:
A linked list is a linear data structure where elements are stored in
nodes, and each node points to the next node in the sequence.
Dynamic in nature, which means it can grow or shrink during program
execution.
Insertion and deletion are generally faster compared to arrays,
especially in the middle.
Random access to elements is slower, O(n) time complexity.
3. Stacks:
A stack is a last-in, first-out (LIFO) data structure.
Elements can only be added or removed from the top of the stack.
Common operations include push (to add an element) and pop (to
remove the top element).
Used for managing function calls, undo mechanisms, and parsing
expressions.
4. Queues:
A queue is a first-in, first-out (FIFO) data structure.
Elements are added at the rear (enqueue) and removed from the front
(dequeue).
Common operations include enqueue and dequeue.
Used in scenarios like job scheduling, breadth-first search algorithms.
5. Trees:
A hierarchical data structure with a root node and subtrees of child
nodes.
Binary Trees have at most two children per node.
Binary Search Trees (BST) maintain an ordering property that allows for
efficient search, insertion, and deletion.
Common tree-based algorithms include binary search, AVL trees, and
heaps.
6. Graphs:
A collection of nodes (vertices) and edges connecting these nodes.
Directed graphs have edges with a direction, while undirected graphs
do not.
Used to represent relationships between entities and solve problems
like shortest path algorithms and network flow.
7. Hash Tables:
Utilizes a hash function to map keys to indices in an array.
Provides fast insertion, deletion, and lookup operations on average,
with O(1) complexity.
Collision resolution methods, like chaining or open addressing, handle
situations where multiple keys map to the same index.
Average value of array
Method overloading occurs when there are multiple
public class AverageArray { methods in the same class with the same name but
different parameters.
public static void main(String[] args) { The methods must have the same name, but their
parameter lists must differ in either the number of
// Sample array parameters or the data
return a + b + c;
write a program to find the factorial value of the number entered
throught the keyboard }
public static void main(String[] args) { System.out.println("Sum of 2.5 and 3.7 is: " + obj.add(2.5, 3.7));
Scanner scanner = new Scanner(System.in); System.out.println("Sum of 1, 2, and 3 is: " + obj.add(1, 2, 3));
} }
return factorial;
}
obj.display("Hello, World!");
// Constructor
}
public Employees(String name, String designation) {
}
this.name = name;
this.designation = designation;
}
1.
Objects and Classes: Object: An instance of a class that
encapsulates data (attributes) and behavior (methods). Think
of it as a real-world entity like a car or a bank account.
// Static method to print employee details
System.out.println("Designation: " + employee.designation); 2. Pillars of OOP: Encapsulation: Bundling data and related methods
System.out.println("Employment Type: " + employmentType); together within an object, restricting direct access to internal data for better
protection and control. Abstraction: Focusing on essential details and
System.out.println("-----------------------------"); hiding unnecessary complexity, presenting a simplified view of an object's
functionality. Inheritance: Creating new classes (subclasses) that inherit
}
properties and methods from existing classes (superclasses), promoting code
public static void main(String[] args) { reuse and reducing redundancy. Polymorphism: Objects of different classes
responding differently to the same method call based on their own
// Creating employee objects implementation, enabling flexible and dynamic behaviour. Java Programming
Basics:
Employees employee1 = new Employees("John", "Manager");
Employees employee2 = new Employees("Alice", "Developer"); 1. Data Types: Data types define the kind of data a variable can hold. Java
offers various primitive and reference data types:
Employees employee3 = new Employees("Bob", "Designer");
Primitive Data Types: int: Stores whole numbers (e.g., -10, 25) double:
Stores floating-point numbers (e.g., 3.14, -5.2) boolean: Stores true or false
// Calling static method to display employee details values char: Stores single characters (e.g., 'a', '$') byte, short, long:
Specialized numerical types for specific ranges
System.out.println("Employee Details:");
show(employee2); 2. Control and Looping Structures: These control the flow of program
execution: Control Structures: if-else: Makes decisions based on conditions
show(employee3); switch: Multi-way branching based on values while: Repeats a block of
code until a condition is false for: Repeats a block of code a specific
}
number of times do-while: Repeats a block at least once, then checks the
} condition Looping Structures: for-each: Iterates over elements in a
collection
3. Classes: Classes are blueprints for creating objects. They define the
properties (attributes) and behaviors (methods) of objects
create a class overload the method display () three time in the class
# Declaring an array
my_array = [1, 2, 3, 4, 5]
# Accessing elements
# Modifying elements
Arrays are widely used in programming for tasks like storing lists of items, iterating
through collections, and implementing algorithms that require constant-time access
to elements. Despite their efficiency for random access, arrays may have limitations,
such as fixed size and potential inefficiencies in insertions and deletions, particularly
in the middle of the array. In such cases, other data structures like linked lists may be
more suitable.
In this diagram, each box represents the memory location of an array element, and
the arrow indicates the starting address of the array. Accessing an element involves a
direct calculation based on the index and the size of each element. This simplicity
and efficiency in memory addressing make arrays a fundamental and widely used
data structure in programming.
In computer science, arrays come in various types, each serving specific purposes and
addressing different requirements. Here are some of the common types of arrays:
1.One-dimensional Array:
A simple, linear array where elements are stored in a single line or row.
Accessing elements involves using a single index.
Examples include arrays of integers, characters, or floating-point
numbers.
# One-dimensional array in Python
my_array = [1, 2, 3, 4, 5]
2.Multi-dimensional Array:
Arrays with more than one dimension. Common types include 2D arrays
(matrices) and 3D arrays.
Elements are accessed using multiple indices corresponding to the array's
dimensions.
Useful for representing tables, grids, and matrices.
# Two-dimensional array (matrix) in Python
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
3. Dynamic Array:
An array that can dynamically resize during runtime.
Provides a higher-level abstraction than traditional fixed-size arrays.
Commonly used in languages like Python wi