0% found this document useful (0 votes)
2 views

Array

The document provides an overview of arrays, including definitions, types (one-dimensional, two-dimensional, and multidimensional), and examples of their usage in programming. It explains linear and binary search algorithms with corresponding C code implementations. Additionally, it covers operations such as insertion, deletion, and matrix operations like multiplication and bubble sort.

Uploaded by

prathamesh427869
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Array

The document provides an overview of arrays, including definitions, types (one-dimensional, two-dimensional, and multidimensional), and examples of their usage in programming. It explains linear and binary search algorithms with corresponding C code implementations. Additionally, it covers operations such as insertion, deletion, and matrix operations like multiplication and bubble sort.

Uploaded by

prathamesh427869
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

ARRAY

Definition of array?
Array is a collection of data structure in a contiguous(continuous)
memory location.
Data can be of any data type like int,float,double char.
An array is used to represent a list of number or list of names.
Example:
List of employees in a organization.
Test score of students in a college

Imagine an array as a large chunk of memory divided into small


blocks of memories each block is capable of storing a data value of
some type
Types of Array:
 One-Dimensional Array
 Two Dimensional Array
 Multi-Dimensionall Array
One-Dimensional Array
 A variable that represents a list of elements in a linear
sequence of element index is called one dimensional array.
 For example if you want to represent a set of five number
says (35,40,20,57,19) by an array variable number then
number is declared as
Int num[5];

Declaration Syntax:
Type name[size]; # value are not initialized
Initializing Syntax:
Type name [size]={35,40,20,57,19} # values are initialized
in curly brackets
Linear search algorithm:
Linear search is a straightforward searching algorithm that can check elements in a array
sequentially until the desired elements are found or the end of the array is reached
It works in both sorted array and unsorted array.
#include <stdio.h>

int linearSearch(int arr[], int size, int target) {


for (int i = 0; i < size; i++) {
if (arr[i] == target) {
return i; // Target found, return index
}
}
return -1; // Target not found, return -1
}

int main() {
int arr[] = {10, 20, 30, 40, 50};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 30;

int result = linearSearch(arr, size, target);

if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found in the array\n");
}
return 0;}
// C Program to implement binary search using iteration
#include <stdio.h>
int binarySearch(int arr[], int left, int right, int key) {
while (left <= right) {
// calculating mid point
int mid = left + (right - left) / 2;
// Check if key is present at mid
if (arr[mid] == key) {
return mid;
}
// If key greater than arr[mid], ignore left half
if (arr[mid] < key) {
left = mid + 1;
}
// If key is smaller than or equal to arr[mid],
// ignore right half
else {
right = mid - 1;
}
}
// If we reach here, then element was not present
return -1;
}
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int size = sizeof(arr) / sizeof(arr[0]);

// Element to be searched
int key = 23;

int result = binarySearch(arr, 0, size - 1, key);


if (result == -1) {
printf("Element is not present in array");
}
else {
printf("Element is present at index %d", result);}
return 0;}
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int size = sizeof(arr) / sizeof(arr[0]);

// Element to be searched
int key = 23;

int result = binarySearch(arr, 0, size - 1, key);


if (result == -1) {
printf("Element is not present in array");
}
else {
printf("Element is present at index %d", result);
}
return 0;
}
One dimensional array
int main()
{
int i, num, n, pos= -1, array[10], beg, end, mid, found=0;
printf("Enter number of elements\n");
scanf("%d", &n);

printf(“\nEnter the elements: ");


for (i = 0; i < n; i++)

scanf("%d", &array[i]);
printf("Enter the number to search\n");
scanf("%d", &num);
beg= 0;
end = n - 1;
while (beg <= end)
{
mid = (beg+end)/2;
if (array[mid] == num)
{
printf("%d found at location %d.\n", num, mid+1);
return 0;
}
if (array[mid] < num)
beg = mid + 1;
else
end = mid - 1;
}
printf("Element Not Found!\n");
return 0;
}
Operation that can be performed on array
Inserting the
element

Deleting:
Two dimensional array
Defintion:
Two dimensional array can be defined by specifying the number of rows and number of columns.
Syntax:
Type arrayname[rows][columns]; Row/ Column 0 Column 1 Column 2 Column 3 Column 4
column
Example Program sum of 2*2 matrix:
Row 0 marks[0] marks[0] marks[0] marks[0] marks[0]
#include <stdio.h> [0] [1] [2] [3] [4]
int main() Row 1 marks[1] marks[1] marks[1] marks[1] marks[1]
[0] [1] [2] [3] [4]
{ Row 2 marks[2] marks[2] marks[2] marks[2] marks[2]
[0] [1] [2] [3] [4]
// Declare and initialize a 2x2 matrix
int matrix[2][2] = { {1, 2},
{3, 4} };
// Print the elements of the 2x2 matrix
printf("2x2 Matrix Elements:\n");
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
// Sum the elements of the 2x2 matrix
int sum = 0;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
sum += matrix[i][j];
}
}
// Print the sum of the elements
printf("Sum of all elements: %d\n", sum);
return 0;
}
#include <stdio.h>
int main()
{
// Declare and initialize a 2D array (3x3 matrix)
int matrix[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
// Print the elements of the 2D array
printf("2D Array Elements:\n");
for (int i = 0; i < 3; i++) # to handle rows we are using the variable i
{
for (int j = 0; j < 3; j++) #to handle columns we are using the variable j
{
printf("%d ", matrix[i][j]);
}
printf("\n"); }
// Sum the elements of the 2D array
int sum = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
sum += matrix[i][j]; } }
// Print the sum of the elements
printf("Sum of all elements: %d\n", sum);
return 0;
Program to multiply 2*2 matrix
#include <stdio.h>
int main() {
// Declare and initialize two 2x2 matrices
int matrix1[2][2] = {
{1, 2},
{3, 4}};
int matrix2[2][2] = {
{5, 6},
{7, 8}
};
// Declare a 2x2 matrix to store the result
int result[2][2] = {0};
// Perform matrix multiplication
for (int i = 0; i < 2; i++) # to handle the rows
{
for (int j = 0; j < 2; j++) # to handle the columns

{
# the values of k can be no of rows of 1st matrix or no of columns of 2nd matrix
result[i][j] =0;
for (int k = 0; k < 2; k++) # to store the remaning iteration of both rows and columns
{
result[i][j] =result[i][j]+ matrix1[i][k] * matrix2[k][j];
}
}
}
// Print the resulting matrix
printf("Result of matrix multiplication:\n");
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
printf("%d ", result[i][j]);
}
printf("\n");
}

return 0;

}
Result of matrix multiplication:
19 22
43 50
Multidimensional Array
It is a array of arrays which mean contain one or more arrays.
Syntax:
Data type array_name[size1][size2][size3]
Example program:
Bubble sort
#include <stdio.h>

// Function to perform Bubble Sort


void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) {
// Flag to detect if the array is already sorted
int swapped = 0;
// Last i elements are already in place
for (int j = 0; j < n-i-1; j++) {
if (arr[j] > arr[j+1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swapped = 1;
}
}
// If no two elements were swapped, the array is sorted
if (!swapped) {
break;
}
}
}
// Function to print the array
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
int main() {
// Example array
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);

printf("Unsorted array: \n");


printArray(arr, n);
// Perform Bubble Sort
bubbleSort(arr, n);

printf("Sorted array: \n");


printArray(arr, n);

return 0;
}
Output:
64 34 25 12 22 11 90

You might also like