Showing posts with label CSIT. Show all posts
Showing posts with label CSIT. Show all posts

Caesar Cipher in C

 #include<stdio.h>

int main(){

char inputtext[]="Deep Raj Bhujel", ciphertext[100], decipheredtext[100];

int i,shift=3;

for(i=0;inputtext[i]!='\0';i++){

if(isupper(inputtext[i]))

ciphertext[i]=(inputtext[i]-65+shift)%26 + 65;

else if(islower(inputtext[i]))

ciphertext[i]=(inputtext[i]-97+shift)%26 + 97;

else

ciphertext[i]=inputtext[i];

}

ciphertext[i]='\0';

printf("The encrypted string is %s\n\n",ciphertext);

for(i=0;ciphertext[i]!='\0';i++){

if(isupper(ciphertext[i]))

decipheredtext[i]=(ciphertext[i]-65-shift)%26 + 65;

else if(islower(ciphertext[i]))

decipheredtext[i]=(ciphertext[i]-97-shift)%26 + 97;

else

decipheredtext[i]=ciphertext[i];

}

decipheredtext[i]='\0';

printf("The decrypted string is %s",decipheredtext);

return 0;

}

Implementation of Preprocessor in C Programming

Q. Write macro definitions with arguments for calculation of area and perimeter of a triangle, a square and a circle. Store these macro definitions in a file called "areaperi.h". Include this file in your program, and call the macro definitions for calculating area and perimeter for different triangles, squares and circles.

Step 1: Create a header file (.h) declaring the prototypes of required functions.

float area1(int, int, int); // for triangle
int area2(int); // for square
float area3(float); // for circle

float perimeter1(int, int, int); // for triangle
int perimeter2(int); // for square
float perimeter3(float); // for circle

Step 2: Create a C programming file (.c) defining those functions.

#include<stdio.h>
#include<math.h>
#define PI 3.1416

float perimeter1(int a, int b, int c) // for triangle
{
    return a+b+c;
}

int perimeter2(int a) // for square
{
    return 4*a;
}

float perimeter3(float r) // for circle
{
    return 2*PI*r;
}

float area1(int a, int b, int c) // for triangle
{
    float s = perimeter1(a, b, c)/2;
    return sqrt(s*(s-a)*(s-b)*(s-c));
}

int area2(int a) // for square
{
    return pow(a,2);
}

float area3(float r) // for circle
{
    return PI*r*r;
}

Step 3: Write your C program (.c) including the areaperi.h file as #include "areaperi.h".

Machine Learning

Mathematics for ML:

1. Statistics & Probability
Populations & Sampling
Mean, Median, Mode & Expected Values
Variance & Covariance
Random Variables
Common Probability Distributions (normal, binomial & uniform)
Central Limit Theorem
Conditional Probability
Bayes’ Theorem
Maximum Likelihood Estimation (MLE)
Linear & Logistic Regression

2. Linear Algebra
Scalars, Vectors, Matrices & Tensors
Matrix Operations (Addition, Subtraction, Multiplication, Transpose, Determinant & Inverse)
Matrix Rank & Linear Independence
Eigenvalues & Eigenvectors
Matrix Decompositions (e.g., SVD)
Principal Component Analysis (PCA)

3. Calculus
Derivatives & Gradients
Gradient Descent Algorithm
Vector/Matrix Calculus (Jacobian, Hessian)
Chain Rule
Fundamentals of Optimisation (Local vs. Global Minima, Saddle Points & Convexity)

Data Science

Coming Soon..

Blockchain

Coming Soon..

Internet of Things

Coming Soon..

System Analysis and Design

Coming Soon..

Searching Problem in AI

Q. A farmer has a wolf, a goat and a cabbage on the west side of a river. He wants to get all of his animals and cabbage across the river onto the east side. The farmer has a row boat but he only has enough room for himself and one other thing. The wolf will eat the goat if they are left together alone. The goat will eat the cabbage if they are left together alone. How can the farmer get everything on the east side? Draw the search tree and show the final solution.

Solution:

Farmer, Wolf, Goat, Cabbage = (f,w,g,c)

West side, East side = (W,E)

Initially, all are on the West side.

                                         (W, W, W, W)

Then,

Stack Implementation using Array

// Static Implementation of Stack

#include<stdio.h>

#include<conio.h>

#define N 5

int stack[5];

int top = -1;

void push(){

int x;

printf("Enter data: ");

scanf("%d",&x);

if(top==N-1)

printf("Overflow\n");

else{

top++;

stack[top]=x;

}

}

Binary Search Algorithm

#include<stdio.h>

#include<conio.h>

int BinarySearch(int [],int,int,int);

int a[100],i,n,key,flag,l,r,m;

void main(){

printf("Enter array size: ");

scanf("%d",&n);

printf("Enter array elements (in ascending order): ");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

printf("Enter value to be searched: ");

scanf("%d",&key);

flag=BinarySearch(a,0,n-1,key);

if(flag==0)

printf("%d is not found.",key);

else

printf("%d is found at %d position",key,flag+1);

getch();

}

Linear Search Algorithm

#include<stdio.h>
#include<conio.h>
void LinearSearch(int [],int,int);
int a[100],i,n,key;
void main(){
printf("Enter array size: ");
scanf("%d",&n);
printf("Enter array elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter value to be searched: ");
scanf("%d",&key);
LinearSearch(a,n,key);
getch();
}

Counting Sort

#include<stdio.h>

#include<conio.h>

void counting_sort(int [],int,int);

int a[100],count[100],b[100],i,k,n;

void main(){

// int a[100],i,n;

printf("Enter array size: ");

scanf("%d",&n);

printf("Enter array elements: ");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

k=a[0];

for(i=1;i<n;i++){

if(k<a[i])

k=a[i];

}

counting_sort(a,n,k);

printf("After Sorting: ");

for(i=0;i<n;i++)

printf("%d ",a[i]);

getch();

}

Quick Sort

#include<stdio.h>

#include<conio.h>

void quick_sort(int [],int,int);

int partition(int [],int,int);

int a[100],i,j,n,temp,lb,ub,start,end,pivot,loc;

void main(){

// int a[100],i,n;

printf("Enter array size: ");

scanf("%d",&n);

printf("Enter array elements: ");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

quick_sort(a,0,n-1);

printf("After Sorting: ");

for(i=0;i<n;i++)

printf("%d ",a[i]);

getch();

}

Selection Sort

#include<stdio.h>

#include<conio.h>

void selection_sort(int [],int);

int a[100],i,j,n,temp,min;

void main(){

// int a[100],i,n;

printf("Enter array size: ");

scanf("%d",&n);

printf("Enter array elements: ");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

selection_sort(a,n);

printf("After Sorting: ");

for(i=0;i<n;i++)

printf("%d ",a[i]);

getch();

}


void selection_sort(int a[],int n){

for(i=0;i<n-1;i++){

min=i;

for(j=i+1;j<n;j++){

if(a[j]<a[min])

min=j;

}

// swap(a[i],a[min])

if(min!=i){

temp=a[i];

a[i]=a[min];

a[min]=temp;

}

}

}

Insertion Sort

#include<stdio.h>

#include<conio.h>

void insertion_sort(int [],int);

int a[100],i,j,n,temp;

void main(){

// int a[100],i,n;

printf("Enter array size: ");

scanf("%d",&n);

printf("Enter array elements: ");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

insertion_sort(a,n);

printf("After Sorting: ");

for(i=0;i<n;i++)

printf("%d ",a[i]);

getch();

}


void insertion_sort(int a[],int n){

for(i=1;i<n;i++){

temp=a[i];

j=i-1;

while(j>=0 && a[j]>temp){

a[j+1]=a[j];

j--;

}

a[j+1]=temp;

}

}

Bubble Sort

 #include<stdio.h>

#include<conio.h>

void bubble_sort(int [],int);

int a[100],i,j,n,temp;

void main(){

// int a[100],i,n;

printf("Enter array size: ");

scanf("%d",&n);

printf("Enter array elements: ");

for(i=0;i<n;i++)

scanf("%d",&a[i]);

bubble_sort(a,n);

printf("After Sorting: ");

for(i=0;i<n;i++)

printf("%d ",a[i]);

getch();

}


void bubble_sort(int a[],int n){

for(i=0;i<n-1;i++){

for(j=0;j<n-1;j++){

if(a[j]>a[j+1]){

temp=a[j];

a[j]=a[j+1];

a[j+1]=temp;

}

}

}

}

Binary Tree Implementation and Traversal [Pre-Order, In-Order, Post-Order]

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

struct node{

int data;

struct node *left,*right;

};

struct node* create(){

int x;

struct node *newnode;

newnode=(struct node*)malloc(sizeof(struct node));

printf("Enter data (-1 for no node): ");

scanf("%d",&x);

if(x==-1)

return 0;

newnode->data=x;

printf("Enter left child of %d: ",x);

newnode->left=create();

printf("Enter right child of %d: ",x);

newnode->right=create();

return newnode;

}

Queue Implementation using Linked List

 #include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void enqueue(int);

void dequeue();

void display();

void peek();

struct node{

int data;

struct node *next;

};

struct node *newnode, *front=0,*rear=0, *temp;

void main(){

enqueue(3);

enqueue(5);

enqueue(7);

enqueue(9);

display();

peek();

dequeue();

display();

peek();

getch();

}

Stack Implementation using Linked List

// Dynamic Implementation of Stack

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

void push(int);

void pop();

void display();

void peek();

struct node{

int data;

struct node *next;

};

struct node *newnode, *top=0,*temp;

void main(){

push(3);

push(5);

push(7);

push(9);

display();

peek();

pop();

display();

peek();

getch();

}

Queue as a List

 #include<stdio.h>

#include<conio.h>

#define N 5

void display();

int queue[N],front=-1,rear=-1,i;

void enqueue(int x){

if(rear==N-1)

printf("Queue is full.");

else if(front==-1 && rear==-1){

front=rear=0;

queue[rear]=x;

}

else{

rear++;

queue[rear]=x;

}

}