Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Thursday, February 9, 2012

SDL Setting in Code Blocks


How many of you had losses hours of times and looked no. of site to set of SDL Graphic library in code block id with gcc-gnu compiles.But its take only minute to set up if you have certain header file and lib file.
First download file uploaded Here.
Then extract it and copy all header file inside include folder and paste on include folder located inside C:\Program Files\CodeBlocks\MinGW\include (I haved installed it in c: inside program file)
and copy all file inside lib folder to lib folder of C:\Program Files\CodeBlocks\MinGW\lib and also on C:\Windows\System32 for 32 bit operating system.If you had used 64 bit os then paste it on C:\Windows\System64 also.
Now your SDL Setting had been completed.For linker setting and environment setting follow lazyfoo.net (Tutorial->Setting up SDL)

Thursday, November 24, 2011

Looping Without Loop in C Programming

‎In c programming we made loop with help of conditional statements.The conditional statements use for looping in c programming are IF LOOP , WHILE LOOP and DO WHILE LOOP.

How many of us can make looping without using any conditional statements mentioned above.If i asked you guys to make looping program without using any conditional statements then you guys will ask me umm there is possible to do looping without using conditional.I also think that before i found this program and i don’t believe.But when i compile this code, I am enforce to believe it.

   1: //looping without default loops(if, do, while, for)
   2: #include<stdio.h>
   3: #include<stdlib.h>
   4: void (*func[2])(int);
   5: void main(int n)
   6: {
   7:           int i;
   8:           printf("\n %d",n);
   9:           func[0]=&exit;
  10:           func[1]=&main;
  11:           i = ++n <= 10;
  12:           (func[i])(n);
  13: } 



Another method for looping without using any loop in c programming is



   1:///this concept is also good 
   2:  
   3: #include <stdio.h>
   4: #include <stdlib.h>
   5: void main(int j) {
   6:          printf("%d\n", j);
   7:          (&main + (&exit - &main)*(j/1000))(j+1);
   8: }

Sunday, August 14, 2011

Warshall Algorithm Using c/c++

Source code of Warshall Algorithm is listed below
/*
IDE : CODE BLOCKS 10.05
warshall.cpp
*/
 //USE OF WARSHALL'S ALGORITHM TO CREATE TRANSITIVE CLOSURE OF A GRAPH

#include<iostream>
#include<conio.h>
#include<windows.h>
using namespace std;
const int num_nodes =10;

Algorithm of Hashing Using c/c++

Source code of Hashing using c++ is listed below.
/*
IDE : Codeblocks 10.05
file name : hashing.cpp
*/

#include<iostream>
using namespace std;
class hashing
{
 private:
int hashpos;
int array[50];
 public:
hashing()
{

Sunday, July 31, 2011

Algorithm of Merging Sort using c/c++ with source code

Merging is the process of combining two or more sorted files into a third sorted file.

Merge sort is based on divide and conquer paradigm.

Source Code of Merging Sort Using c/c++ is as follow

/**IDE : CODE BLOCKS 10.05
PROGRAM : merge.cpp
*/
#include <iostream>
#include <cstdlib>
#define size 10
using namespace std;
void merge(int *arr,int f1,int l1,int f2,int l2)
{
     int arr1[size],len1,len2,ap1=f1,ap2=f2,ap3;
     for(ap3=0;ap1<=l1 && ap2<=l2;ap3++)
     {
          if(arr[ap1]<arr[ap2])
               arr1[ap3]=arr[ap1++];
          else
               arr1[ap3]=arr[ap2++];
     }
     while(ap1<=l1)
          arr1[ap3++]=arr[ap1++];
     while(ap2<=l2)
          arr1[ap3++]=arr[ap2++];
     for(int i=f1;i<=l2;i++)
     {
          arr[i]=arr1[i-f1];
     }
}
void mergesort(int *arr,int first,int last)
{
     int len=last-first+1;
     if(len==1) return;
     int mid;
     mid=(first+last)/2;
     mergesort(arr,first,mid);
     mergesort(arr,mid+1,last);
     merge(arr,first,mid,mid+1,last);
}
int  main()
{
     int arr[size],i;
     cout<<"Enter 10 numbers randomly :-"<<endl;
     for(i=0;i<size;i++)
          cin>>arr[i];
     mergesort(arr,0,size-1);
     cout<<endl<<"After sorting"<<endl;
     for(i=0;i<size;i++)
          cout<<arr[i]<<'\t';
     return 0;
}

Drops comments if any correction required !!!

Friday, July 29, 2011

Algorithm of Searching on Tree using c/c++

A search algorithm is an algorithm that accepts an argument a and tries to find a record whose key is a.The algorithm may return the

entire record on more commonly it may return a pointer to that record.

Search in which the entire table is constantly in main memory are called internal memory.Whereas those in which most of the table is kept in auxiliary storage are called external searches.

/*
IDE : CODEBLOCKS 10.05
FILE: Search.cpp*/
#include <iostream>
using namespace std;
class seqsearch
{
     int arr[100],key,n;
     public:
     seqsearch(){n=0;}
     void insert();
     void display();
     int search();
};
void seqsearch::insert()
{
     cout<<"Enter the data to be inserted:";
     cin>>arr[n];
     n++;
}
void seqsearch::display()
{
     if(n==0) cout<<"No data to display";
     for(int i=0;i<n;i++)
          cout<<arr[i]<<'\t';
}
int seqsearch::search()
{
     cout<<"Enter the key of data to be searched:";
     cin>>key;
     for(int i=0;i<n;i++)
     {
          if(key==arr[i]) return i;
     }
     return -1;
}
int main()
{
     seqsearch a1;
     int ch;
     do
     {
          cout<<"\n*****Menu****"<<endl;
          cout<<"1.Insert data"<<endl;
          cout<<"2.Display data"<<endl;
          cout<<"3.Search data"<<endl;
          cout<<"4.Exit"<<endl;
          cout<<"Enter your choice : ";
          cin>>ch;
          switch(ch)
          {
               case 1:
                    a1.insert();
                    break;
               case 2:
                    a1.display();
                    break;
               case 3:
                    int x;x=a1.search();
                    cout<<(x+1);
                    if(x==-1) cout<<"OOPS!!Data not found";
                    else     cout<<"The data is at "
                        <<x+1<<" position";
                break;
               case 4:
                    break;
          }
    }while(ch!=4);
    return 0;
}

Drop Comment if any correction required !!!

Saturday, June 25, 2011

Algorithm of Priority Queue Using c/c++

Priority Queue is a data structure to which the intrinsic ordering of elements does determine the results of basic operations.
There are two types of priority queue.They are
1.Ascending Priority Queue
2.Descending Priority Queue
                   An ascending priority queue is a collection of data to which item can be inserted arbitrarily but from which only smallest item can be removed.
                   Where as an descending priority queue is similar but allows deletion of only largest item.
Source Code For ascending priority queue
--------------------------------------------------
File Name : ascending_priority_queue.cpp
Id:code block 10.05
------------------------------------------
#include<cstdlib>
#include<iostream>
using namespace std;
const int MAX = 5;
int main()
{
    int a,head = 0,tail = -1,i,j;
    double queue[MAX],temp,item;
    while(1){
        cout<<"\n***************** MENU *********************\n1.Enque operation\n2.Deque operation\n3.View\n4.Exit\t";
    cin>>a;
        switch(a)
        {
            case 1:{
                    if(tail>=MAX-1)//enqueue operation 
                        cout<<"Queue is full"<<endl;
                    else
                        cout<<"Enter item : ";
                        cin>>item;
                        tail++;
                        queue[tail] = item;
                    }
                break;
            case 2:{
                cout<<"Dequeue operation ...\n";//Dequeue operation
                if(head>tail)
                    cout<<"Queue is full \n";
                else
                    temp = queue[head];
                    for(i=head;i<=tail;i++)
                    if(temp>queue[i])
                        temp = queue[i];
                    for(i=tail,j=tail;i>=head;i--)
                    {
                        if(temp == queue[i])
                            continue;
                        else
                        {
                            queue[j]=queue[i];
                            j--;
                        }
                    }
                    cout<<temp<<endl;
                    head++;
                }
            break;
            case 3:{
            cout<<"\nItem on queue ...\n";//display
                for(int i = head; i<=tail;i++)
                cout<<endl<<queue[i];

            }
            cout<<endl;
            break;
            case 4://end program
                exit(0);
                break;
        }
    }
    return 0;
}

Friday, June 24, 2011

Newton's Forward and Backward Interpolation Using c/c++

Differential Table Generator
Newton's Forward Interpolation Table and Newton's Backward Interpolation Table can be generated using c and c++ programming language.
Source Code For  Newton's Forward Interpolation Table and Newton's Backward Interpolation Table 

------------------------------------------
IDE :- Code Block 10.05
File Name :- difference_table.c
+++++++++++++++++++
#include<stdio.h>
#include<math.h>
int main()

{
    float x[10],y[15][15];
    int n,i,j;
//no. of items
    printf("Enter n : ");
    scanf("%d",&n);
    printf("X\tY\n");
    for(i = 0;i<n;i++){
            scanf("%f %f",&x[i],&y[i][0]);
    }
    //forward difference table
    for(j=1;j<n;j++)
        for(i=0;i<(n-j);i++)
            y[i][j] = y[i+1][j-1] - y[i][j-1];
    printf("\n***********Forward Difference Table ***********\n");
//display Forward Difference Table
    for(i=0;i<n;i++)
    {
        printf("\t%.2f",x[i]);
        for(j=0;j<(n-i);j++)
            printf("\t%.2f",y[i][j]);
        printf("\n");
    }
    //backward difference table
    for(j=1;j<n;j++)
//for j = 0 initially input is taken so we start from j=1
        for(i=n-1;i>(j-1);i--)
            y[i][j] = y[i][j-1] - y[i-1][j-1];
    printf("\n***********Backward Difference Table ***********\n");
//display Backward Difference Table
    for(i=0;i<n;i++)
    {
        printf("\t%.2f",x[i]);
        for(j=0;j<=i;j++)
            printf("\t%.2f",y[i][j]);
        printf("\n");
    }
return 0;
}
/**
Sample Run
Enter n : 4
X       Y
10      1.1
20      2.0
30      4.4
40      7.9

***********Forward Difference Table ***********
        10.00   1.10    0.90    1.50    -0.40
        20.00   2.00    2.40    1.10
        30.00   4.40    3.50
        40.00   7.90

***********Backward Difference Table ***********
        10.00   1.10
        20.00   2.00    0.90
        30.00   4.40    2.40    1.50
        40.00   7.90    3.50    1.10    -0.40
*/