The content on this website, including videos and code examples, is for educational purposes only. All demonstrations and designs are fictional and created to illustrate coding techniques. Any resemblance to existing websites or brands is purely coincidental.
The creators and administrators of this website do not claim ownership or affiliation with any existing websites or companies. Users are encouraged to use the information responsibly for learning purposes. Liability for any misuse of the content provided is not accepted.
Array: It is a fixed-size sequence collection part of elements of the same data type.
Types of array:
1) One-Dimensional Array: It is a list of items that has one variable name and one subscript to access the items.
2) Two-Dimensional Array: It is an array of arrays that has two subscripts for accessing its values. It is used to represent table or matrix data.
3) Multi-Dimensional Array: It is an array with more than one dimension. Examples are 2d, 3d, and so on.
Static Array: These are the arrays declared using the static memory allocation technique.
Static Memory Allocation: It is the process of allocating memory at compile time.
Dynamic Array: These are the arrays declared using the dynamic memory allocation technique.
Dynamic Memory Allocation: It is the process of allocating memory at run time.
Searching: It is the process of finding the location of the specified element in the list.
Sorting: It is the process of rearranging elements in the list in ascending or descending order.
Searching:
searching.c
#include <stdio.h>
void main()
{
int array_of_numbers[5], i, number_to_be_searched;
printf("Enter the numbers of the list: ");
for (i = 0; i < 5; i++) {
scanf("%d", &array_of_numbers[i]);
}
printf("\nEnter the number to search in list: ");
scanf("%d", &number_to_be_searched);
for (i = 0; i < 5; i++) {
if (array_of_numbers[i] == number_to_be_searched) {
printf("\nNumber found at %d location in list.", i + 1);
break;
}
}
if (i == 5) {
printf("\nNumber not found in list.");
}
}
Sorting:
sorting.c
#include <stdio.h>
void main()
{
int array_of_numbers[5], i, j, temp;
printf("Enter the numbers to sort: ");
for (i = 0; i < 5; i++)
scanf("%d", &array_of_numbers[i]);
printf("\nYou entered: ");
for (i = 0; i < 5; i++)
printf("\t%d", array_of_numbers[i]);
printf("\nSorted list: ");
for (i = 0; i < 5; i++)
for (j = 0; j < 4; j++) {
if (array_of_numbers[j] > array_of_numbers[j + 1]) {
temp = array_of_numbers[j];
array_of_numbers[j] = array_of_numbers[j + 1];
array_of_numbers[j + 1] = temp;
}
}
for (i = 0; i < 5; i++)
printf("\t%d", array_of_numbers[i]);
}
Two dimensional array:
two_d_array.c
#include <stdio.h>
void main()
{
int matrix1[2][2], matrix2[2][2], product_matrix[2][2], i, j, k;
printf("Enter the first matrix elements (rows, 2 x 2 matrix): ");
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
scanf("%d", &matrix1[i][j]);
printf("\nEnter the second matrix elements (rows, 2 x 2 matrix): ");
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++)
scanf("%d", &matrix2[i][j]);
printf("\nFirst matrix is:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)
printf("%d\t", matrix1[i][j]);
printf("\n");
}
printf("\nSecond matrix is:\n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)
printf("%d\t", matrix2[i][j]);
printf("\n");
}
printf("\nProduct matrix is:\n");
for (i = 0; i < 2; i++)
for (j = 0; j < 2; j++) {
product_matrix[i][j] = 0;
for (k = 0; k < 2; k++) {
product_matrix[i][j] = product_matrix[i][j] + matrix1[i][k] + matrix2[k][j];
}
}
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++)
printf("%d\t", product_matrix[i][j]);
printf("\n");
}
}