0% found this document useful (0 votes)
2 views5 pages

M4 What Are Arrays

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

M4 What Are Arrays

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

What Are Arrays?

● Definition: Arrays are a collection of elements of the same data type stored in contiguous memory
locations.
● Why Use Arrays?
○ Store and manage large amounts of data.
○ Access elements using an index.

Syntax:
Copy code
data_type array_name[size];
Example:
Copy code
int numbers[5]; // Declares an integer array of size 5

One-Dimensional Arrays

Array Manipulation
Accessing Elements:
Copy code
int numbers[5] = {1, 2, 3, 4, 5};
printf("%d", numbers[0]); // Outputs: 1
Updating Elements:
Copy code
numbers[1] = 10; // Updates the second element to 10

Searching in an Array

● Linear Search:
○ Traverse the array to find the desired element.

Example:
Copy code
int search(int arr[], int n, int key) {
for (int i = 0; i < n; i++) {
if (arr[i] == key) {
return i; // Element found at index i
}
}
return -1; // Element not found
}


● Binary Search:
○ Works on sorted arrays.

Example:
Copy code
int binarySearch(int arr[], int n, int key) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}

Insertion

● Insert an element into a specific position in the array.

Example:
Copy code
void insert(int arr[], int *n, int pos, int value) {
for (int i = *n; i > pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos] = value;
(*n)++;
}

Deletion

● Remove an element at a specific position in the array.

Example:
Copy code
void delete(int arr[], int *n, int pos) {
for (int i = pos; i < *n - 1; i++) {
arr[i] = arr[i + 1];
}
(*n)--;
}

Finding the Largest/Smallest Element


Example:
Copy code
int findLargest(int arr[], int n) {
int largest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
return largest;
}

int findSmallest(int arr[], int n) {


int smallest = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
}
}
return smallest;
}

Two-Dimensional Arrays

● Definition: A matrix-like structure where data is stored in rows and columns.

Syntax:
Copy code
data_type array_name[rows][columns];
Example:
Copy code
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};

Addition of Two Matrices


Example:
Copy code
void addMatrices(int A[2][2], int B[2][2], int C[2][2]) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
}

Multiplication of Two Matrices


Example:
Copy code
void multiplyMatrices(int A[2][2], int B[2][2], int C[2][2]) {
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
C[i][j] = 0;
for (int k = 0; k < 2; k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
}

Transpose of a Square Matrix


Example:
Copy code
void transpose(int matrix[3][3], int transposed[3][3]) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
transposed[j][i] = matrix[i][j];
}
}
}

Null-Terminated Strings as Array of Characters

● Definition: Strings in C are arrays of characters ending with a null character (\0).

Example:
Copy code
char str[] = "Hello";
printf("%s", str);

String Manipulation Using Standard Library Functions

● Include <string.h> for standard string functions.


● Common Functions:
strlen: Get string length.
Copy code
int len = strlen("Hello");

strcpy: Copy one string to another.


Copy code
char dest[20];
strcpy(dest, "Hello");

strcat: Concatenate two strings.


Copy code
char str1[20] = "Hello ";
strcat(str1, "World");

strcmp: Compare two strings.


Copy code
if (strcmp("abc", "abc") == 0) {
printf("Strings are equal");
}

strrev: Reverse a string (not standard in some compilers).


Copy code
strrev(str);

You might also like