mid-2 c programme
mid-2 c programme
mid-2 c programme
Declaration:
Syntax:
Datatype arr_name[size];
Ex:
int arr[10]; //It allocated 40 bytes of memory and allows to store
10 integer elements.
2) Runtime initialization: Assigning the values into an array during the program
execution is called runtime initialization.
Ex:
int marks[6],i;
for(i=0;i<6;i++)
Scanf(“%d”,&marks[i]);
1D Array Programs
a) Binary Search
Binary search is a more efficient searching technique but works only on sorted arrays. It
repeatedly divides the search interval in half.
Steps:
1) Set the low and high pointers to the beginning and end of the array.
2) Find the middle index using (low+high)/2.
3) Compare the middle element with the target:
a) If it matches, return the index.
b) If the target is smaller, adjust the high pointer to mid−1.
c) If the target is larger, adjust the low pointer to mid+1.
4) Repeat until the element is found or the pointers cross.
#include <stdio.h>
int binarySearch(int[],int,int); //function prototype
if (arr[mid] == target)
{
return mid; // Element found.
}
if (arr[mid] < target)
{
low = mid + 1; // Search in the right half.
}
else
{
high = mid - 1; // Search in the left half.
}
}
return -1; // Element not found.
}
int main()
{
int i,key,n;
printf("Enter the Size of the Array\n");
scanf("%d",&n);
int arr[n];
int index=binarySearch(arr,n,key);
if(index==-1)
printf("%d is not found in the given array",key);
else
printf("%d is found at %d index in the given array",key,index);
return 0;
}
b) Linear Search
Linear search is the simplest searching technique. It traverses the array sequentially and
compares each element with the target element until a match is found or the array ends.
Steps:
1) Start from the first element.
2) Compare the current element with the target.
3) If a match is found, return the index.
4) If no match is found after traversing the entire array, return -1 or a failure message.
Advantages:
Simple and easy to implement.
Does not require the array to be sorted.
Disadvantages:
Inefficient for large datasets.
#include <stdio.h>
int main()
{
int i,key,n;
printf("Enter the Size of the Array\n");
scanf("%d",&n);
int arr[n];
int index=linearSearch(arr,n,key);
if(index==-1)
printf("%d is not found in the given array",key);
else
printf("%d is found at %d index in the given array",key,index);
return 0;
}
c) Bubble sort
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares
adjacent elements, and swaps them if they are in the wrong order. This process is
repeated until the list is sorted.
Steps:
1) Start with the first element and compare it with the next element.
2) If the first element is greater than the second, swap them.
3) Move to the next pair and repeat until the last element of the array.
4) Repeat the entire process for n−1 passes, where n is the number of elements.
5) After each pass, the largest unsorted element "bubbles up" to its correct position.
#include <stdio.h>
int main()
{
int i,n;
printf("Enter the Size of the Array\n");
scanf("%d",&n);
int arr[n];
//Read the Array Elements
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
bubbleSort(arr, n);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}
d) Selection sort
Selection Sort divides the array into a sorted and an unsorted region. It repeatedly selects
the smallest (or largest) element from the unsorted region and moves it to the sorted
region.
Steps:
1) Start with the first element and find the smallest element in the unsorted region.
2) Swap the smallest element with the first element of the unsorted region.
3) Move the boundary between the sorted and unsorted regions by one element.
4) Repeat until the entire array is sorted.
Advantages:
1) Performs well on small datasets.
2) Simpler to implement than other algorithms.
Disadvantages:
1) Not suitable for large datasets due to its quadratic time complexity.
Main Logic:
void selectionSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
int minIndex = i;
for(i=0;i<n/2;i++)
{
int temp=marks[i];
marks[i]=marks[n-i-1];
marks[n-i-1]=temp;
}
2. 2D Array Declaration, initialization and accessing with an example program?
2D array is nothing elements are stored in the form rows and columns.
Declaration:
Syntax: data_type array_name[rows][columns];
rows – Number of rows.
columns – Number of columns
Initialization:
int matrix[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
Accessing of 2D array elements:
int value = matrix[1][2]; // Accesses element at 2nd row, 3rd column (6)
Ex:
#include <stdio.h>
int main()
{
int matrix[3][3] = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Display the 2D array
printf("2D Array Elements:\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
return 0;
}
3. 2D Array Programs
a) Matrix Multiplication
b) Matrix Addition
c) Trace of a matrix
Refer the Material or Lab Record to refer these programs.
// Calculate factorial
for (int i = 1; i <= num; i++)
{
factorial *= i;
}
// Display result
printf("Factorial of %d is %d\n", num, factorial);
return 0;
}
Recursion:
#include <stdio.h>
// Recursive function to calculate factorial
long long factorial(int n)
{
if (n == 0 || n == 1)
{
return 1; // Base case
}
else
{
return n * factorial(n - 1); // Recursive call
}
}
int main()
{
int num;
// Display result
printf("Factorial of %d is %lld\n", num, factorial(num));
return 0;
}
int main()
{
int n, i;
return 0;
}
return 0;
}
Output:
Enter number of terms: 7
Fibonacci Series (Iterative): 0 1 1 2 3 5 8
5. storage classes
Storage classes in C define the scope, lifetime, visibility, and default initial value of
variables. The main storage classes in C are:
1) Automatic (auto)
2) Register (register)
3) Static (static)
4) External (extern)
#include <stdio.h>
void autoExample()
{
auto int x = 10; // 'auto' is optional
printf("Automatic variable x = %d\n", x);
}
int main()
{
autoExample();
return 0;
}
Output:
Automatic variable x = 10
int main()
{
registerExample();
return 0;
}
Output:
Register variable x = 5
File2.c:
#include <stdio.h>
#include "File1.c"
extern int x; // Declare global variable
void display();
int main()
{
printf("Accessing x in another file: %d\n", x);
display();
return 0;
}
Output:
Accessing x in another file: 10
Value of x = 10
6. Categories of functions
According to the arguments and return values, there are four types of user defined
functions.
1. Functions with no arguments and no return values.
2. Functions with arguments and no return values.
3. Functions with arguments and return values.
4. Functions with no arguments and return values.
Unit-V
1. pointer Declaration, initialization and accessing with an example program?
Pointer is also a variable used to store the address of another variable.
Operators used in pointers:
1) & -> Address of operator
2) * -> Dereferce or Value at the address operator
#include<stdio.h>
int main()
{
int x=10,y=20;
int *p,*q;
p=&x;
q=&y;
printf("%d %d \n",x,y);
printf("%d %d\n",*p,*q);
}
5. Count number of vowels and consonants, digits and special characters in a text
file.
Refer the Lab program for this
Example:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *arr;
int n, i;
printf("Array elements:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}