EXP 1: C program to traverse an array
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50}; // Declare and initialize an array
int i;
printf("The elements of the array are:\n");
for (i = 0; i < 5; i++) {
printf("Element at index %d: %d\n", i, arr[i]);
}
return 0;
}
Exp: 2 C program to insert an element into an array
#include <stdio.h>
int main() {
int arr[100], n, i, pos, value;
// Get number of elements
printf("Enter number of elements in array: ");
scanf("%d", &n);
// Get elements from user
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
// Get position and value to insert
printf("Enter the position to insert (0 to %d): ", n);
scanf("%d", &pos);
printf("Enter the value to insert: ");
scanf("%d", &value);
// Shift elements to the right
for (i = n; i > pos; i--) {
arr[i] = arr[i - 1];
}
// Insert the value
arr[pos] = value;
n++; // Increase size
// Display updated array
printf("Array after insertion:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
EXP: 3 C program to search for an element in an array
#include <stdio.h>
int main()
{
int arr[10], found = 0, key, i;
printf("Enter 10 element in array:\n");
for (i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
for (i = 0; i < 10; i++)
{
if (arr[i] == key)
{
found = 1;
break;
}
}
if (found == 1)
{
printf("Element found at the position %d\n", i + 1);
}
else
{
printf("Element not found\n");
}
return 0;
}
EXP 4: C program to update an element
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int pos = 2; // Index to update (0-based)
int newValue = 99; // New value to update
printf("Original array:\n");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
// Update value at position 2
arr[pos] = newValue;
printf("\n\nArray after update at index %d:\n", pos);
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
EXP 15: C program to insertion Sort.
#include <stdio.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key = arr[i]; // Current element to be inserted
j = i - 1;
// Move elements of arr[0..i-1] that are greater than key
// to one position ahead of their current position
while (j >= 0 && arr[j] > key)
{
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key; // Insert the key at the correct position
}
}
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[100], n;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
insertionSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
EXP 8: C program traverse in a binary tree
#include <stdio.h>
#include <stdlib.h>
// Define the structure for a node in the tree
struct TreeNode
{
int data;
struct TreeNode *left;
struct TreeNode *right;
};
// Function to create a new node
struct TreeNode *createNode(int data)
{
struct TreeNode *newNode = (struct TreeNode *)malloc(sizeof(struct TreeNode));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
// Function to insert a node in the tree (basic BST insert)
struct TreeNode *insertNode(struct TreeNode *root, int data)
{
if (root == NULL)
{
return createNode(data); // If the tree is empty, create the first node
}
if (data < root->data)
{
root->left = insertNode(root->left, data); // Insert in the left subtree
}
else
{
root->right = insertNode(root->right, data); // Insert in the right subtree
}
return root;
}
// Function for inorder traversal (Left, Root, Right)
void inorderTraversal(struct TreeNode *root)
{
if (root == NULL)
{
return;
}
inorderTraversal(root->left); // Traverse the left subtree
printf("%d ", root->data); // Print the data of the current node
inorderTraversal(root->right); // Traverse the right subtree
}
// Main function
int main()
{
struct TreeNode *root = NULL;
// Insert nodes into the tree
root = insertNode(root, 50);
insertNode(root, 30);
insertNode(root, 20);
insertNode(root, 40);
insertNode(root, 70);
insertNode(root, 60);
insertNode(root, 80);
// Print inorder traversal of the tree
printf("Inorder traversal of the tree: \n");
inorderTraversal(root); // Output: 20 30 40 50 60 70 80
return 0;
}
Comments
Post a Comment