0% found this document useful (0 votes)
17 views4 pages

#Include Stdio.h

The document contains C code that demonstrates basic array operations including insertion, deletion, and searching for elements in both one-dimensional and two-dimensional arrays. It provides functions to manipulate arrays and includes examples of how to use these functions in the main program. Each operation is illustrated with print statements to show the state of the array before and after the operation.

Uploaded by

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

#Include Stdio.h

The document contains C code that demonstrates basic array operations including insertion, deletion, and searching for elements in both one-dimensional and two-dimensional arrays. It provides functions to manipulate arrays and includes examples of how to use these functions in the main program. Each operation is illustrated with print statements to show the state of the array before and after the operation.

Uploaded by

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

#include <stdio.

h>

void insert(int arr[], int *n, int value, int pos) {


for (int i = *n; i >= pos; i--) {
arr[i] = arr[i - 1];
}
arr[pos - 1] = value;
(*n)++; // Increment array size
}

int main() {
int arr[10] = {1, 2, 3, 4, 5}; // Array with 5 elements
int n = 5;
int value = 10, pos = 3;

printf("Array before insertion:\n");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

insert(arr, &n, value, pos);

printf("\nArray after insertion at position %d:\n", pos);


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

return 0;
}

#include <stdio.h>

void delete(int arr[], int *n, int pos) {


for (int i = pos - 1; i < *n - 1; i++) {
arr[i] = arr[i + 1];
}
(*n)--; // Decrease array size
}

int main() {
int arr[10] = {1, 2, 3, 4, 5}; // Array with 5 elements
int n = 5;
int pos = 3;

printf("Array before deletion:\n");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

delete(arr, &n, pos);

printf("\nArray after deletion at position %d:\n", pos);


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}

return 0;
}
#include <stdio.h>
int search(int arr[], int n, int value) {
for (int i = 0; i < n; i++) {
if (arr[i] == value) {
return i; // Return index of element
}
}
return -1; // Return -1 if not found
}

int main() {
int arr[10] = {1, 2, 3, 4, 5};
int n = 5;
int value = 3;

int index = search(arr, n, value);

if (index != -1) {
printf("Element %d found at index %d\n", value, index);
} else {
printf("Element %d not found\n", value);
}

return 0;
}

#include <stdio.h>

void insert(int arr[][3], int *n, int value, int row, int col) {
arr[row][col] = value;
(*n)++;
}

int main() {
int arr[5][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int n = 3; // Number of rows
int value = 10, row = 1, col = 2;

printf("Array before insertion:\n");


for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}

insert(arr, &n, value, row, col);

printf("\nArray after insertion:\n");


for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}

return 0;
}
#include <stdio.h>

void delete(int arr[][3], int *n, int row, int col) {


for (int i = col; i < 2; i++) { // Move elements to the left
arr[row][i] = arr[row][i + 1];
}
(*n)--; // Decrease number of rows
}

int main() {
int arr[5][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int n = 3;
int row = 1, col = 2;

printf("Array before deletion:\n");


for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}

delete(arr, &n, row, col);

printf("\nArray after deletion:\n");


for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ", arr[i][j]);
}
printf("\n");
}

return 0;
}

#include <stdio.h>

int search(int arr[][3], int n, int value) {


for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
if (arr[i][j] == value) {
return i * 3 + j; // Return index in the flattened array
}
}
}
return -1; // Return -1 if not found
}

int main() {
int arr[5][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int n = 3;
int value = 5;

int index = search(arr, n, value);

if (index != -1) {
printf("Element %d found at position %d\n", value, index);
} else {
printf("Element %d not found\n", value);
}

return 0;
}

You might also like