Posts

Arrays

Image
  Arrays are a fundamental data structure in computer programming that allow you to store a collection of data of the same type in contiguous memory locations. An array is a collection of elements of the same data type, each identified by an index or a subscript. The elements in an array can be accessed by their index value. The first element in an array is typically indexed with 0, and the last element is indexed with n-1, where n is the total number of elements in the array. Arrays can be used to represent a variety of data structures, including lists, stacks, and queues. They are useful for storing large amounts of data that can be accessed and processed quickly. Here's an example of declaring and initializing an array of integers in C: int arr[5] = {1, 2, 3, 4, 5}; In this example, we declare an array of integers called arr with 5 elements. We initialize the array with the values 1, 2, 3, 4, and 5. To access an element in the array, we can use its index value: int x = arr[0

TRAVELING SALESMAN USING BRANCH AND BOUND TECHNIQUE

Image
The Traveling Salesman Problem (TSP) is a classic problem in computer science and optimization theory. The problem is defined as follows: given a set of cities and the distances between them, find the shortest possible path that visits each city exactly once and returns to the starting city. The Branch and Bound technique is an algorithmic paradigm for solving combinatorial optimization problems. The basic idea is to explore the search space by dividing it into smaller subproblems, and then pruning branches of the search tree that can be shown to lead to suboptimal solutions. The branch and bound algorithm for the TSP works by starting with an initial lower bound on the length of the optimal tour, and then exploring the search space by systematically building up partial tours and computing lower bounds on their completion. Sample C code for solving the Traveling Salesman Problem(TSP) using the branch and bound technique: #include <stdio.h> #include <stdlib.h> #include &

TRAVELING SALESMAN PROBLEM USING DYNAMIC APPROACH

Image
The Traveling Salesman Problem (TSP) is a classic optimization problem in computer science. The problem involves finding the shortest possible route that a salesman must take to visit a set of cities and return to the starting point. The dynamic programming approach to solving the TSP involves breaking the problem down into subproblems and storing the solutions to these subproblems in a table. Here's how you can implement a C program to solve the TSP using dynamic programming. To solve aTSP using C Programming #include <stdio.h> #include <stdlib.h> #include <math.h> #define MAX_CITIES 10 int num_cities; int city_coords[MAX_CITIES][2]; double dist[MAX_CITIES][MAX_CITIES]; double dp[MAX_CITIES][1 << MAX_CITIES]; double tsp(int curr_city, int visited); int main() { int i, j; printf("Enter the number of cities: "); scanf("%d", &num_cities); printf("Enter the coordinates of the cities:\n"); for (i = 0;

FLOYD’S TRIANGLE USING C and C++

Image
What is Floyd's Triangle Floyd's triangle is a right-angled triangular pattern of natural numbers, named after Robert Floyd who described it in his book "Non-Programmer's Introduction to Programming". It consists of n rows, where each row contains consecutive natural numbers starting from 1 in the first row to n in the nth row. The triangle is formed by aligning the first number of each row with the left edge of the triangle. For example, a Floyd's triangle of 5 rows will look like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Floyd's triangle can be useful for a variety of programming applications, such as testing loop structures, generating numerical patterns, and displaying output in a visually pleasing way. Print Floyd's Triangle of n rows using C #include <stdio.h> int main() { int n, i, j, k = 1; printf("Enter the number of rows: "); scanf("%d", &n); for (i = 1; i <= n; i++) {

DOUBLY LINKED LIST

DOUBLY LINKED LIST PROGRAMMING IN C #include<stdio.h> #include<conio.h> #include<ctype.h> #include<stdlib.h> void create(); void insert(int); void delet(int); void display(); int ch,pos; char ch1; struct link { int data; struct link *llink,*rlink; }*start,*end,*temp; void main() { start=end=NULL; while(ch!=6) { clrscr(); printf("\n\tDOUBLY LINKED LIST\n"); printf("\n1.Creation \n2.Insertion \n3.Deletion \n4.Display \n5.Exit"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: create(); break; case 2: printf("\nEnter the postion"); scanf("%d",&pos); insert(pos); break; case 3: printf("\n[B]ackward list"); printf("\n[F]orward list"); ch1=toupper(getch()); display(); printf("\nEnter the position:"); scanf("%d",&pos); delet(pos); break; case 4: printf("\n[B]ackward list"); prin

CIRCULAR LINKED LIST

CIRCULAR LINKED LIST PROGRAM: #include<stdio.h> #include<conio.h> #include<stdlib.h> struct node { int info; struct node *next; }*first=NULL,*last=NULL; unsigned int count; int main(void) { int choice; void create(void); void insert(void); void rem(void); void display(void); printf("\n\tCircular Linked List"); do { clrscr(); printf("\n\toperations"); printf("\n\t1.create\n\t2.insert\n\t3.remove\n\t4.display"); printf("\n\t5.exit"); printf("\n\tEnter your choice:"); scanf("%d",&choice); switch(choice) { case 1: create(); break; case 2: insert(); break; case 3: rem(); break; case 4: display(); break; case 5: printf("Exited"); getch(); return 0; default: printf("Enter(1-5)only"); getch(); } } while(1); } void create(void) { struct node *temp=NULL; int n; if(first!=NULL) { printf("\nlist is not empty"); getch(); retur

PROCESS CREATION (In Linux/Unix)

PROCESS CREATION   PROGRAM: #include<stdio.h> #include<conio.h>             main(int argc,char *argv[]) {             int pid;             pid=fork(); if(pid<0) {             fprintf(stderr,”Fork failed”);             exit(-1); } else if(pid==0)             execlp(“/bin/ls”,”ls”,NULL) else {             wait(NULL):             printf(“Child completed”); } exit (0); }

OPERATING SYSTM’s ALGORITHM (In LINUX OS) - PRODUCER CONSUMER PROBLEM

PRODUCER CONSUMER PROBLEM PROGRAM: #include<stdio.h> #include<string.h> main() { int i,size,count=0,c,x=1,no,h,buf[100]; printf(“\nEnter the buffer size”); scanf(“%d”,&size); do { printf(“\n1.Produce\n2.Consume\n3.View produced item\n4.Exit”); printf(“\nEnter your choice”); scanf(“%d”,&c); switch(c) {             case 1:                         if(count>=size)                                     printf(“\nBuffer is full”);                         else                         {                         printf(“\nEnter the number of elements to be produced”);                                     scanf(“%d”,&no);                                     if((count+no)>size)                                                 printf(“\nEnter the number less than %d”,(size-count+1)); else { h=count; for(i=++h;i<=(h+no-1);i++) {             printf(“\nEnter the value”);             scanf