Posts

Showing posts with the label C Programming

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++) {

Graphics in C

Now u can create simple graphics program by using graphics methods in C programming language /*Graphics in C*/ #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<stdlib.h> void main() { int gd=DETECT,gm; initgraph(&gd,&gm,""); settextstyle(TRIPLEX_FONT,HORIZ_DIR,5); outtextxy(20,8,"****************************"); outtextxy(20,85,"****************************"); outtextxy(40,40,"/**My Sweet Family**/"); settextstyle(GOTHIC_FONT,HORIZ_DIR,5); outtextxy(30,110,"  AAAA "); outtextxy(30,270,"  BBBB"); outtextxy(30,150,"  CCCC"); outtextxy(30,190,"  DDDD"); outtextxy(30,230,"  EEEE"); outtextxy(30,320,"******************************"); getch(); } #include<stdio.h> #include<conio.h> #include<graphics.h> #include<dos.h> #include<stdlib.h> void main() { in

CONSTRUCTOR AND DESTRUCTOR USING C++

/*BANK OPERATION USIN CONSTRUCTOR AND DESTRUCTOR*/ PROGRAM: #include<iostream.h> #include<conio.h> #include<stdio.h> class account { private :   float balance;   float rate; public :   account();   ~account();   void deposit();   void withdraw();   void compound();   void getbalance();   void menu(); }; account::account() {   cout<<"\n\t BANK OPERATION USIN CONSTRUCTOR AND DESTRUCTOR ";   cout<<"\n\t _______________________________________________\n";   cout<<"\n Enter the initial balance:\n";   cin>>balance;   cout<<"\n Interest rate:\n";   cin>>rate; } account::~account() {   cout<<"\n Database has been deleted\n"; } void account::deposit() {   float amount;   cout<<"\n Enter the amount:";   cin>>amount;   balance=balance+amount;   cout<<"\n Current balance="<<balance; } void account::wi

BOOKS DETAILS USING C STRUCTURE

              To write a c program to create a collection of books using array of structure and performs the following operatons. (i)                  Searching a book with title and author name. (ii)                Sort the book based on title. /*BOOKS DETAILS*/ #include<stdio.h> #include<conio.h> #include<string.h> struct books { char book[20]; char author[20]; int price; }; int main() { int i,ch,j,n,flag=1; struct books l[10]; struct books t[1]; char sbook[20],saut[20]; clrscr(); do{ clrscr(); printf("\n\t\tBOOK LIST\n\nMAIN MENU\n\n"); printf("\n1.creation\n2.search by title & author\n"); printf("3.sort by title\n4.exit\n"); printf("\nEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: clrscr(); printf("\nEnter number of booklist:"); scanf("%d",&n); for(i=1;i<=n;i+