Notes on Array
Notes on Array
Here:
int = data type
numbers = name of the array
5 = number of elements it can store (index from 0 to 4)
Since there are 5 values in the initializer list, the compiler sets the size of
numbers to 5. This saves manual effort and avoids mismatch between
declared size and number of initial values.
3. Multi-Dimensional Array:
Arrays with more than two dimensions.
Syntax: int arr[3][4][2];
Used in advanced applications like 3D graphics or tensor
computations.
Row-Major Order
Comparison Table
#include <stdio.h>
int main() {
int A[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9,10,11,12}
};
printf("Row-Major Order Traversal:\n");
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 4; j++) {
printf("%d ", A[i][j]);
}
}
Example:
Matrix (4x5): Triplet Representation:
0 0 0 5 0 Row Col Value
0 0 8 0 0 0 3 5
0 0 0 0 0 1 2 8
0 6 0 0 9 3 1 6
3 4 9
#include <stdio.h>
int main() {
int rows = 4, cols = 5;
int matrix[4][5] = {
{0, 0, 0, 5, 0},
{0, 0, 8, 0, 0},
{0, 0, 0, 0, 0},
{0, 6, 0, 0, 9}
};
printf("Triplet Representation:\n");
printf("Row Col Value\n");
return 0;
}
printf("\n");
printf("\n");
if (n >= SIZE) {
return n;
return n;
}
arr[pos] = value;
return n + 1;
return n;
return n - 1;
if (arr[i] == value) {
return;
}
// Function to sort the array using bubble sort
arr[j + 1] = temp;
traverse(arr, n);
int main() {
scanf("%d", &n);
printf("Enter elements:\n");
scanf("%d", &arr[i]);
while (1) {
printf("\nMenu:\n");
scanf("%d", &choice);
switch (choice) {
case 1:
traverse(arr, n);
break;
case 2:
reverse(arr, n);
break;
case 3:
break;
case 4:
scanf("%d", &pos);
n = delete(arr, n, pos);
break;
case 5:
scanf("%d", &value);
search(arr, n, value);
break;
case 6:
sort(arr, n);
break;
case 7:
return 0;
default:
printf("Invalid choice!\n");
}
Selection Sort :
#include <stdio.h>
int min = i;
min = j;
if (min != i) {
arr[min] = arr[i];
arr[i] = temp;
int main() {
selectionSort(arr,n);
for (int i = 0; i < n; i++)
return 0;