> September 2011 ~ Online tutorial

mark list program in c


The following program get Marks Card Details and shows Min and Max marks as the output.

#include<stdio.h>
#include<conio.h>

struct markscard {
                                        int rollno;
                                        int marks;
                                        char name[35];
                                        char addres[35];
                                 } ;

void main()
{
 struct markscard mcrd[10];
 int i, maxm, mimm;

 clrscr();
 for(i=1;i<=10;i++)
 {
        printf("n%d Roll Number : ",i);
        scanf("%d", &mcrd[i].rollno);
        printf("  Name        : ");
        fflush(stdin);
        gets(mcrd[i].name);
        printf("  Address     : ");
        fflush(stdin);
        gets(mcrd[i].addres);
        printf("  Marks       : ");
        scanf("%d", &mcrd[i].marks);
 }

 clrscr();
 printf("nThe Data you entered are :");
 printf("n#  Roll#tNametAddresstMarks");

 maxm = 0;
 mimm = mcrd[1].marks;

 for(i=1;i<=10;i++)
 {
        printf("n%d  %dt%-20st%-20st%d",i,mcrd[i].rollno,mcrd[i].name,mcrd[i].addres,mcrd[i].marks);
        if (mcrd[i].marks >= maxm)
        maxm = mcrd[i].marks;
        if (mcrd[i].marks <= mimm)
        mimm = mcrd[i].marks;
 }

 printf("nnHighest Marks : %d",maxm);
 printf("nLowest Marks  : %d",mimm);
 getch();
}
 

print without using semicolon

    #include "conio.h"
    #include "stdio.h"
    void main()
    {
    if(printf("hello")&&getch())
    }

multiplication tables using c

0*10 Multiplication Table C code.



Sample Code

    #include<stdio.h>
    #include<conio.h>
    main()
    {
    int row,column;
    clrscr();
    puts("tttMULTIPLICATION TABLE");
    puts("nttt********************");
    for(row=1;row<=10;row++)
    {
    printf("nn");
    for(column=1;column<=10;column++)
    printf("%6d", row*column);
    }
    getch();
    }

armstrong number in c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

void main()
{
        long num1,num2=0,mcount=0, nums;
        char str1[35], strc;

        clrscr();
        printf("Enter a number : ");
        scanf("%ld",&num1);

         ltoa(num1,str1,10);
         while (mcount <= (strlen(str1)-1))
         {
          strc = str1[mcount];
          nums = atoi(&strc);
          num2 = num2+pow(nums,3);
          mcount = mcount+1;
         }

         if (num1 == num2)
         {
          printf("Amstrong Number : %ld",num2);
         }
         else
         {
          printf("Not a Amstrong Number.");
         }
   getch();
}
 

doubly linked list program in c

doubly linked list program in c

        This a sample program of double linked list! the advantage of double over single is that singly linked list has the node inserted only at one end. and the pointer corresponds to the next pointer. but in a doubly linked list, the node pointer points to the both previous and the next node. singly linked list has two nodes doubly linked list has three nodes A doubly linked list makes sense when you need to traverse the list in both directions. You aren't able to do that with a singly linked list.



Sample Code

    #include <stdio.h>
    #include <stdlib.h>
    
    struct dllist
{
     int number;
     struct dllist *next;
     struct dllist *prev;
    };
    
    struct dllist *head, *tail;
    
    void append_node(struct dllist *lnode);
    void insert_node(struct dllist *lnode, struct dllist *after);
    void remove_node(struct dllist *lnode);
    
    int main(void) {
     struct dllist *lnode;
     int i = 0;
    
     /* add some numbers to the double linked list */
     for(i = 0; i <= 5; i++) {
      lnode = (struct dllist *)malloc(sizeof(struct dllist));
      lnode->number = i;
      append_node(lnode);
     }
    
     /* print the dll list */
     for(lnode = head; lnode != NULL; lnode = lnode->next) {
      printf("%dn", lnode->number);
     }
    
     /* destroy the dll list */
     while(head != NULL)
      remove_node(head);
    
     return 0;
    }
    
    void append_node(struct dllist *lnode)
{
     if(head == NULL)
 {
      head = lnode;
      lnode->prev = NULL;
     }
else
{
      tail->next = lnode;
      lnode->prev = tail;
     }
    
     tail = lnode;
     lnode->next = NULL;
    }
    
    void insert_node(struct dllist *lnode, struct dllist *after)
{
     lnode->next = after->next;
     lnode->prev = after;
    
     if(after->next != NULL)
      after->next->prev = lnode;
     else
      tail = lnode;
    
     after->next = lnode;
    }
    
    void remove_node(struct dllist *lnode)
{
     if(lnode->prev == NULL)
      head = lnode->next;
     else
      lnode->prev->next = lnode->next;
    
     if(lnode->next == NULL)
      tail = lnode->prev;
     else
      lnode->next->prev = lnode->prev;
    }


    

single linked list program in c


Single linked list inplementation using different functions

1. Insert a number at the beginning
2. Insert a number at last
3. Insert a number at a particular location in list
4. Print the elements in the list
5. Print the total number of elements in the list
6. Delete a node in the linked list
7. Reverse a linked list
8. Get out of linked list



Sample Code

    /* PROGRAM IMPLEMENTATION OF SINGLE LINKED LIST */
    
    #include"stdio.h"
    //#define NULL 0
    /* STRUCTURE CONTANING A DATA PART AND A LINK PART */
    
    struct node
    {
    int data;
    struct node *next;
    }*p;
    
    /* P IS A GLOBAL POINTER CONTAINS THE ADRESS OF THE FIRST NODE IN
    LIST
    */
    
    /*THIS FUNCTION DELETES A NODE */
    
    delnode(int num)
    {
    struct node *temp, *m;
    temp=p;
    while(temp!=NULL)
    {
    if(temp->data==num)
    {
    if(temp==p)
    {
    p=temp->next;
    free(temp);
    return;
    }
    else
    {
    m->next=temp->next;
    free(temp);
    return;
    }
    }else
    {
    m=temp;
    temp= temp->next;
    }
    
    }
    printf("    ELEMENT %d NOT FOUND    ", num);
    }/*THIS FUNCTION ADDS A NODE AT THE LAST OF LINKED LIST */
    
    append( int num )
    {
    struct node *temp,*r;
    /* CREATING A NODE AND ASSIGNING A VALUE TO IT */
    
    temp= (struct node *)malloc(sizeof(struct node));
    temp->data=num;
    r=(struct node *)p;
    
    if (p == NULL) /* IF LIST IS EMPTY CREATE FIRST NODE */
    {
    p=temp;
    p->next =NULL;
    }
    else
    { /* GO TO LAST AND ADD*/
    
    while( r->next != NULL)
    r=r->next;
    r->next =temp;
    r=temp;
    r->next=NULL;
    }
    }/* ADD A NEW NODE AT BEGINNING */
    
    addbeg( int num )
    {
    /* CREATING A NODE AND INSERTING VALUE TO IT */
    
    struct node *temp;
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    
    /* IF LIST IS NULL ADD AT BEGINNING */
    if ( p== NULL)
    {
    p=temp;
    p->next=NULL;
    }
    
    else
    {
    temp->next=p;
    p=temp;
    }
    }
    
    /* ADD A NEW NODE AFTER A SPECIFIED NO OF NODES */
    
    addafter(int num, int loc)
    {
    int i;
    struct node *temp,*t,*r;
    r=p;         /* here r stores the first location */
    if(loc > count()+1 || loc <= 0)
    {
    printf("    insertion is not possible :    ");
    return;
    }
    if (loc == 1)
/* if list is null then add at beginning */
    {
    addbeg(num);
    return;
    }
    else
    {
    for(i=1;i<loc;i++)
    {
    t=r;
/* t will be holding previous value */
    r=r->next;
    }
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    t->next=temp;
    t=temp;
    t->next=r;
    return;
    }
    }/* THIS FUNCTION DISPLAYS THE CONTENTS OF THE LINKED LIST */
    
    display(struct node *r)
    {
    r=p;
    if(r==NULL)
    {
    printf("NO ELEMENT IN THE LIST :");
    return;
    }
    /* traverse the entire linked list */
    while(r!=NULL)
    {
    printf(" -> %d ",r->data);
    r=r->next;
    }
    printf("");
    }
    //THIS FUNCTION COUNTS THE NUMBER OF ELEMENTS IN THE LIST
    count()
    {
    struct node *n;
    int c=0;
    n=p;
    while(n!=NULL)
    {
    n=n->next;
    c++;
    }
    return(c);
    }
    //THIS FUNCTION REVERSES A LINKED LIST
    reverse(struct node *q)
    {
    struct node *m, *n,*l,*s;
    m=q;
    n=NULL;
    while(m!=NULL)
    {
    s=n;
    n=m;
    m=m->next;
    n->next=s;
    }
    p=n;
    }
    
    
    /* THIS IS THE MAIN PROGRAM */
    
    main()
    {
    int i;
    p=NULL;
    while(1) /* this is an indefinite loop */
    {
    printf("    1.INSERT A NUMBER AT BEGINNING;<BR>);
    printf("    2.INSERT A NUMBER AT LAST:<BR>);
    printf("    3.INSERT A NUMBER AT A PARTICULAR LOCATION INlIST:<BR>);
    printf("    4.PRINT THE ELEMENTS IN THE LIST :<BR>);
    printf("    5.PRINT THE NUMBER OF ELEMENTS IN THE LIST <BR>);
    printf("    6.DELETE A NODE IN THE LINKED LIST:<BR>);
    printf("    7.REVERSE A LINKED LIST :<BR>);
    printf("    8.GET OUT OF LINKED LIST (BYEE BYEE):<BR>);
    printf("    PLEASE, ENTER THE NUMBER:");
    
    scanf("%d",&i);
/* ENTER A VALUE FOR SWITCH */
    
    switch(i)
    {
    case 1:
    {
    int num;
    printf("
    PLEASE ENTER THE NUMBER :-");
    scanf("%d",&num);
    addbeg(num);
    break;
    }
    case 2:
    {
    int num;
    printf("
    PLEASE ENTER THE NUMBER :-");
    scanf("%d",&num);
    append(num);
    break;
    }
    
    case 3:
    {
    int num, loc,k;
    printf("
    PLEASE ENTER THE NUMBER :-");
    scanf("%d",&num);
    printf("
    PLEASE ENTER THE LOCATION NUMBER :-");
    scanf("%d",&loc);
    addafter(num,loc);
    break;
    }
case 4:
    {
    struct node *n;
    printf("
    
    THE ELEMENTS IN THE LIST ARE : <BR>);
    display(n);
    break;
    }
    
    case 5:
    {
    struct node *n;
    display(n);
    printf(" TOTAL NO OF ELEMENTS IN THE LSIT ARE %d",count());
    break;
    }
case 6:
    {
    int num;
    printf("
    PLEASE ENTER A NUMBER FROM THE LIST :");
    scanf("%d",&num);
    delnode(num);
    break;
    }
    case 7:
    {
    reverse(p);
    display(p);
    break;
    }
    case 8:
    {
    exit();
    }
    }/* end if switch */
    }/* end of while */
    }/* end of main */

WINDOWING TO VIEWPORT MAPPING




WINDOWING TO VIEWPORT MAPPING

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
main()
  {
                    float sx,sy;
                    int w1,w2,w3,w4,x1,x2,x3,x4,y1,y2,y3,y4,v1,v2,v3,v4;
                    int gd=DETECT,gm;
                    initgraph(&gd,&gm,"c:\\tc\\bgi");
                    printf("Enter The Coordinate x1,y1,x2,y2,x3,y3\n");
                    scanf("%d%d%d%d%d%d",&x1,&y1,&x2,&y2,&x3,&y3);
                    cleardevice();
                    w1=5;
                    w2=5;
                    w3=635;
                    w4=465;
                    rectangle(w1,w2,w3,w4);
                    line(x1,y1,x2,y2);
                    line(x2,y2,x3,y3);
                    line(x3,y3,x1,y1);
                    getch();
                    v1=425;
                    v2=75;
                    v3=550;
                    v4=250;
                    sx=(float)(v3-v1)/(w3-w1);
                    sy=(float)(v4-v2)/(w4-w2);
                    rectangle(v1,v2,v3,v4);
                    x1=v1+floor(((float)(x1-w1)*sx)+.5);
                    x2=v1+floor(((float)(x2-w1)*sx)+.5);
                    x3=v1+floor(((float)(x3-w1)*sx)+.5);
                    y1=v2+floor(((float)(y1-w2)*sy)+.5);
                    y2=v2+floor(((float)(y2-w2)*sy)+.5);
                    y3=v2+floor(((float)(y3-w2)*sy)+.5);
                    line(x1,y1,x2,y2);
                    line(x2,y2,x3,y3);
                    line(x3,y3,x1,y1);
                    getch();
                    return 0;
 }



OUTPUT

Enter The Coordinate x1,y1,x2,y2,x3,y3

100
200
300
400
500
350





 





















































 









         
        





















COHEN-SUTHERLAND 2D LINE CLIPPING


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<math.h>
float cxl,cxr,cyt,cyb;
code(float ,float);
void clip(float ,float,float,float);
void  rect(float ,float,float,float);
 main()
    {
                    float x1,y1,x2,y2;
                    int g=0,d;
                    initgraph(&g,&d,"c:\\tc\\bin");
                    settextstyle(1,0,1);
                    outtextxy(40,15,"BEFORE CLIPPING");
                    printf("\n Please Enter Left,Bottom,Right,Top Of Clip Window");
                    scanf("%f%f%f%f",&cxl,&cyb,&cxr,&cyt);
rect(cxl,cyb,cxr,cyt);
                    getch();
printf("\n Enter  The Line Coordinate");
                    scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
line(x1,y1,x2,y2);
                    getch();
                    cleardevice();
                    settextstyle(1,0,1);
                    outtextxy(40,15,"AFTER CLIPPING");
                    clip(x1,y1,x2,y2);
                    getch();
                    closegraph();
   }

void clip(float x1,float y1,float x2,float y2)
 {
                    int c,c1,c2;
                    float x,y;
                    c1=code(x1,y1);
                    c2=code(x2,y2);
                    getch();
         


                    while((c1!=0)||(c2!=0))
                     {
                             if((c1&c2)!=0)
                             goto out;
                             c=c1;
                              if(c==0)
                                       c=c2;
                             if((c&1)==1)
                                {
                                       y=y1+(y2-y1)*(cxl-x1);
                                       x=cxl;
                               }
                             else
                             if((c&2)==2)
                               {
                                       y=y1+(y2-y1)*(cxl-x1)/(x2-x1);
                                       x=cxr;
                                }
                             else
                             if((c&8)==8)
                                {
                                       x=x1+(x2-x1)*(cyb-y1)/(y2-y1);
                                       y=cyb;
                              }
                             else
                             if((c&4)==4)
                              {
                                       x=x1+(x2-x1)*(cyt-y1)/(y2-y1);
                                       y=cyt;
                               }
                            if(c==c1)
                             {
                                       x1=x;
                                       y1=y;
                                       c1=code(x,y);
                             }
                             else
                             {
                                       x2=x;
                                       y2=y;
                                       c2=code(x,y);
                             }
                     }
                   



out:
                              rect(cxl,cyb,cxr,cyt);
                             line(x1,y1,x2,y2);
    }

code(float x ,float y)
 {
                    int c=0;
                    if(x<cxl)                          c=1;
                    else
                              if(x>cxr)               c=2;
                    else
                              if(y<cyb)               c=c|8;
                    else
                              if(y>cyt)               c=c|4;
                    return c;
}


void rect(float xl,float yb,float xr,float yt)
  {
                    line(xl,yb,xr,yb);
                    line(xr,yb,xr,yt);
                    line(xr,yt,xl,yt);
                    line(xl,yt,xl,yb);
 }


















OUTPUT


BEFORE CLIPPING


Please Enter Left , Bottom , Right , Top Of The Clip window

200
200
400
400
         

Please Enter The Line Coordinates (X1, Y1, X2, Y2)

150
300
400
450





 






















AFTER CLIPPING







 

dda line drawing algorithm in c

DDA Line drawing Algorithm

The digital differential analyzer (DDA)  is a scan-conversion line algorithm. In this algorithm, we sample the line at unit intervals in one coordinate and determine corresponding integer values nearest the line path of the other coordinate and plot those coordinate (pixel) in computer screen. Consider first a line with positive slope. If the slope is less than or equal to 1, we sample at unit x intervals (dx = 1) and computer each successive y value as y(k+1) = y(k) + m. Subscript k takes integer values starting from 1, for the first point, and increases by 1 until the final endpoint is reached. For lines with a positive slope greater than 1, we reverse the roles of x and y.That is, we sample at unit y intervals (dy = 1) and calculate each succeeding x value as x(k+1) = x(k) + (1/m)




Program

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
void DDA(int x1,int y1,int x2,int y2,int col,int del);
void main()
{
int gd=DETECT,gm,x1,x2,y1,y2;
initgraph(&gd,&gm,"");
printf("Enter (x1,y1) and (x2,y2) : ");
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
DDA(x1,y1,x2,y2,14,20);
getch();
closegraph();
}
void DDA(int x1,int y1,int x2,int y2,int col,int del)
{
int dx,dy,s,i,xi,yi,x,y;
x=x1,y=y1;
dx=x2-x1;
dy=y2-y1;
if(dx>dy)
s=dx;
else
s=dy;
xi=dx/s;
yi=dy/s;
putpixel(x,y,col);
for(i=0;i<s;i++)

{
x+=xi;
y+=yi;

putpixel(x,y,col);
delay(del);

}
}


Output









bresenham’s ellipse drawing algorithm c program



         
BRESENHAM’S ELLIPSE DRAWING ALGORITHM
#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "graphics.h"
main()
{
                    int gd=DETECT,gm;
                    int xcenter,ycenter,rx,ry;
                    int p,x,y,px,py,rx1,ry1,rx2,ry2;
                    initgraph(&gd,&gm,"c:\\tc\\bgi");
                    printf("Enter The Radius Value:\n");
                    scanf("%d%d",&rx,&ry);
                    printf("Enter The xcenter and ycenter Values:\n");
                    scanf("%d%d",&xcenter,&ycenter);
                    ry1=ry*ry;
                    rx1=rx*rx;
                    ry2=2*ry1;
                    rx2=2*rx1;

/* REGION 1 */

x=0;
                    y=ry;
                    plotpoints(xcenter,ycenter,x,y);
                    p=(ry1-rx1*ry+(0.25*rx1));
                    px=0;
                    py=rx2*y;
                    while(px<py)
                    {
                              x=x+1;
                             px=px+ry2;
                             if(p>=0)
                                       y=y-1;
                                       py=py-rx2;


                               if(p<0)
                                       p=p+ry1+px;
                              else
                                        p=p+ry1+px-py;
                    plotpoints(xcenter,ycenter,x,y);

/* REGION 2*/
                  
p=(ry1*(x+0.5)*(x+0.5)+rx1*(y-1)*(y-1)-rx1*ry1);
                   while(y>0)
                   {
                             y=y-1;
                             py=py-rx2;
                              if(p<=0)
                             {
                                       x=x+1;
                                       px=px+ry2;
                              }
                              if(p>0)
                                       p=p+rx1-py;
                             else
                                      p=p+rx1-py+px;
                     plotpoints(xcenter,ycenter,x,y);
                   }
               }
                    getch();
                    return(0);
            }

int plotpoints(int xcenter,int ycenter,int x,int y)
{
                    putpixel(xcenter+x,ycenter+y,6);
                    putpixel(xcenter-x,ycenter+y,6);
                    putpixel(xcenter+x,ycenter-y,6);
                    putpixel(xcenter-x,ycenter-y,6);

}




OUTPUT


Enter The Radius Value(Rx,Ry)           :     10      30
                   

Enter The xcenter and ycenter Values  :     300    150























         
RESULT:


Buy it