Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

April 22, 2014

Sorted Linked List implementation in C

include "stdio.h"
include "stdlib.h"
include "conio.h"



void del(int data);
void insert(int value);
void display();


struct node
{
    int data;
    struct node *link;
};

struct node *top=NULL,*temp, *temp1, *temp2, *temp3;

int main()
{
    int choice,data;

 
    while(1) //infinite loop is used to insert/delete infinite number of elements in linked list
    {
     
        printf("\n1.Insert\n2.Delete\n3.Display\n4.Exit\n");
        printf("\nEnter ur choice:");
        scanf("%d",&choice);
        switch(choice)
        {
        case 1:
         
         
            printf("Enter a new element :");
            scanf("%d",&data);
            insert(data);
            break;
         
        case 2:
     
        printf("Enter the value to be deleted from sorted linked list :");
            scanf("%d",&data);
         
            del(data);
            break;
         
        case 3:
            display();
            break;
        case 4:
            exit(0);
        }
     
    }  
getch();
return 0;
}

void insert(int data)
{

temp=(struct node *)malloc(sizeof(struct node));
temp->data=data;

if(top == NULL)
{

            temp->link=NULL;
            top=temp;
         
}
else            // top not null
{
temp1 = top ;
while(temp1 != NULL)
{
if(temp1->data >= data)   // list element is smaller ...

{
if(temp1 == top)   // list element is head ...
{
  temp->link = temp1;

top = temp;
break;

}
else // list element is not head ..
{

temp->link = temp1;
temp2->link = temp;
break;
}

}
else
{

if(temp1->link == NULL)
{
temp->link = NULL;
temp1->link = temp;
break;

}
else
{
temp2 = temp1;
temp1 = temp1->link;
}

}

}

}
          // creating a space for the new element.
                 
}

void del(int data)
{
     struct node *temp,*var;
     temp=top;
 int i=0;
     while(temp!=NULL)
     {
          if(temp->data == data)
          {      i = 1;   // Flag ..
                if(temp==top)
                {
                     top=temp->link;
                     free(temp);
                   break;
                }
                else
                {
                     var->link=temp->link;
                     free(temp);
                     break;
                 
                }
          }
          else
          {
               var=temp;
               temp=temp->link;
          }
     }
     if(i == 1)
     {
printf("data deleted from list is %d",data);
 
     }
     else
     {
      printf("\n The required data, %d is not found in the list. go look somewhere else",data);

     }
}





void display()
{
         temp=top;
            if(temp==NULL)
            {
                printf("\nStack is empty\n");
            }
         
            while(temp!=NULL)
            {
                printf(" %d ->",temp->data);
                temp=temp->link;
            }
             
}

March 12, 2014

Priority Queue Implementation in C using Arrays

#include <stdio.h>

#include <conio.h>

#define size 5

int queue[5][2] = {0};

int top = -1;

int bottom;

void push(int value, int pr)

{

int i,j,k;

if(top < size-1)

{

if(queue[top][1] > pr)

{

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

{

if(queue[i][1] > pr)

{

break;

}

}

for(j=top;j>=i;j--)

{

queue[j+1][0] = queue[j][0];

queue[j+1][1] = queue[j][1];

}

top++;

queue[i][0] = value;

queue[i][1] = pr;

}

else

{

top++;

queue[top][0] = value;

queue[top][1] = pr;

}

}

else

{

printf("queue overflow \n");

}

}

void pop()

{

int i;

if(queue[0][0] == 0)

{

printf("\n The queue is empty  \n");

}

else

{

printf("After , dequeue the following value is erased \n  %d \n", queue[0][0]);

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

{

queue[i][0] = queue[i+1][0];

queue[i][1] = queue[i+1][1];

}

queue[top][0] = 0;

queue[top][1] = 0;

top--;

}

}

void display()

{ int i,j;

printf("Element\tPriority \n");

for(i=size - 1;i>=0;i--)

{

for(j=0;j<2;j++)

{

printf(" %d\t",queue[i][j]);

}

printf("\n");

}

}

int main()

{

int i,j, ch=0 ,value = 0,pr=0;

while(1)

{

printf("\n Please Enter the choice. \n");

printf("1 for Enqueue \n 2 for Dequeue \n 3 for display\n  5 for exit: \t \n");

scanf("%d",&ch);

switch(ch)

{

case 1:

printf("\n Please Enter the number to be inserted: \t ");

scanf("%d", &value);

printf("\n Please Enter the priority: \t ");

scanf("%d", &pr);

push(value,pr);

break;

case 2:

pop();

break;

case 3:

display();

break;

case 5:

exit(0);

default:

printf("You entered wrong choice\n");

}

}

}

February 25, 2014

C program for Stack implementation using array

#include "stdio.h"
#include "conio.h"

#define size 5

int stack[size] = {0};

int top = -1;

void push(int value)
{
if(top < size)
{
top++;
stack[top] = value;
}
else
{
printf("Stack overflow \n");
}
}

void pop()
{
if(top >= 0)
{
printf("The popped element is:\t %d \n", stack[top]);
stack[top] = 0;
top--;
}

else
{
printf("Stack underflow \n");
}


}
void display()
{

                 int i;

for(i=size - 1;i>=0;i--)
{
printf(" %d \n",stack[i]);
}
}

void tos()
{
printf("The value at top is %d", stack[top]);
}

int main()
{

int i,j, ch=0 ,value = 0;

while(1)
{
printf("\n Please Enter the choice. \n");
printf("1 for push\n 2 for pop \n 3 for display\n 4 for top \t  \n 5 for exit");
scanf("%d",&ch);

switch(ch)
{
case 1:
printf("\n Please Enter the number to be inserted: \t");
scanf("%d", &value);
push(value);
break;

case 2:

pop();

break;

case 3:

display();

break;


case 4:
tos();

break;

case 5:

exit(0);



}
}

}

October 10, 2013

C program to print different Star Formations using For Loop


Here is the code to print different star formations using for loop.

#include "stdio.h"
int main()
{
    int num,i,j,k,m,n,o,p;
     printf("Please enter a number to print the star formations \n");


     scanf("%d", &num);   
   
for(i=0;i<num;i++)
    {
    for(j=0;j<num;j++)
        {
                printf("*");
        }
                printf("\n");
    }

printf("\n");

j=0,i=0,k=0;
// Hollow Star formation
    for(i=0;i<num;i++)
    {
    for(j=0;j<num;j++)
        {
            if(i==0 || i==num-1)
            {
                printf("*");   
            }
            else if(j==0 || j==num-1)
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }
        printf("\n");
    }


     // downward descending formation
     printf("\n");
     j=0,i=0,k=0;
     for(i=num;i>0;i--)
        {
            for(j=i;j>0;j--)
            {
                printf("*");
            }
            printf("\n");
        }

printf("\n");
j=0,i=0,k=0;
 
    for(i=num;i>=0;i--)
        {
            for(k=i;k<num;k++)
            {
            printf(" ");
            }
            for(j=0;j<=i-1;j++)
            {
                printf("*");
            }
            printf("\n");
        }


printf("\n");
j=0,i=0,k=0;
// Star formation right angle triangle.
        for(i=num;i>0;i--)
        {
            for(j=0;j<i-1;j++)
            {
                printf(" ");
            }
            for(k=j;k<num;k++)
            {
            printf("*");
            }
            printf("\n");
        }


printf("\n");
j=0,i=0,k=0;
    for(i=0;i<num;i++)
    {
    for(j=0;j<=i;j++)
        {
                printf("*");
        }
                printf("\n");
    }

     return 0;
}











The output of the above code is following:

st

C program to print the hollow stars square

Here is the code for the C program to print the star formation of a hollow square. The program will input an integer value from the user and print the hollow star formations.
Here is the code.

#include “stdio.h”
int main()
{
int num,i,j;
printf("Please enter a number to print the hollow star formation \n");
scanf("%d", &num);
for(i=0;i<num;i++)
{
for(j=0;j<num;j++)
{
if(i==0 || i==num-1)
{
printf("*");
}
else if(j==0 || j==num-1)
{
printf("*");
}
else
{
printf(" "); // space is printed ..
}
}
printf("\n");
}
return 0;
}


























Here is the sample output.
star

As can be seen in the output above, for the user input, the hollow star square will be formed. The key to understand the logic of the code is as follows.
The outerloop controls the number of  lines and inner loop controls the number of stars.

October 8, 2013

C program to display integers in Ascending without using Loop

This program run sorts and display the integers in ascending order. It finds the largest and smallest integer values respectively.



#include "stdio.h"
#include "conio.h"


int main()



{


    int a=0, b=0, c=0,d=0,e=0, f=0, grade;
    printf("Enter four numbers\n");
   
    scanf("%d",&a);
    scanf("%d",&b);
    scanf("%d",&c);
    scanf("%d",&d);
   
    if(a>b && a>c && a>d)
    {
           printf("1st value that you entered is largest\n");
           e=1;
    }
    else if(b>a && b>c && b>d)
    {
           printf("2nd value that you entered is largest\n");
           e=2;
    }
    else if(c>a && c>b && c>d)
    {
           printf("3rd value that you entered is largest\n");   
           e=3;
    }
    else if(d>a && d>b && d>c)
    {
           printf("4th value that you entered is largest\n");
           e=4;
    }

// For finding smallest value

     if(a
    {
           printf("1st value that you entered is smallest\n");
           f=1;
    }
    else if(b
    {
           printf("2nd value that you entered is smallest\n");
           f=2;
    }
    else if(c
    {
           printf("3rd value that you entered is smallest\n");
           f=3;
    }
    else if(d
    {
           printf("4th value that you entered is smallest\n");
           f=4;
    }

//Sorting //

    if(e==1 && f==2)
    {
            if(c
            {
                   printf("Sorted order is \n %d %d %d %d", b,c , d, a);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", b,d , c, a);
            }
    }
    else
    if(e==1 && f==3)
    {
            if(b
            {
                   printf("Sorted order is \n %d %d %d %d", c,b , d, a);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", c,d , b, a);
            }
    }
    else
    if(e==1 && f==4)
    {
             if(b
            {
                   printf("Sorted order is \n %d %d %d %d", d,b , c, a);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,c , b, a);
            }
    }
   
    // for e=2
   
        if(e==2 && f==1) // a is smallest, b is largest
    {
            if(c
            {
                   printf("Sorted order is \n %d %d %d %d",a, c , d, b);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", a,d , c, b);
            }
    }
    else
    if(e==2 && f==3)// c is smallest
    {
            if(a
            {
                   printf("Sorted order is \n %d %d %d %d", c,a , d, b);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", c,d , a, b);
            }
    }
    else
    if(e==2 && f==4)  // d is smallest
    {
             if(a
            {
                   printf("Sorted order is \n %d %d %d %d", d,a , c, b);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,c , a, b);
            }
    }
   
    // for c is largest i.e.. e=3
            if(e==3 && f==1) // a is smallest, c is largest
    {
            if(b
            {
                   printf("Sorted order is \n %d %d %d %d",a, b , d, c);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", a,d , b, c);
            }
    }
    else
    if(e==3 && f==2)// b is smallest
    {
            if(a
            {
                   printf("Sorted order is \n %d %d %d %d", b,a , d, c);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", b,d , a, c);
            }
    }
    else
    if(e==3 && f==4)  // d is smallest
    {
             if(a
            {
                   printf("Sorted order is \n %d %d %d %d", d,a , b, c);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,b , a, c);
            }
    }   
   
       
    // for d is largest i.e.. e=4
   
   
   
            if(e==4 && f==1) // a is smallest
    {
            if(b
            {
                   printf("Sorted order is \n %d %d %d %d",a, b , c, d);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", a,c , b, d);
            }
    }
    else
    if(e==4 && f==2)// b is smallest
    {
            if(a
            {
                   printf("Sorted order is \n %d %d %d %d", b,a , c, d);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", b,c , a, d);
            }
    }
    else
    if(e==4 && f==3)  // d is smallest
    {
             if(a
            {
                   printf("Sorted order is \n %d %d %d %d", d,a , b, d);
            }
            else
            {
                printf("Sorted order is \n %d %d %d %d", d,b , a, d);
            }
    }       

    getch();
    return 0;
   
   
}


Here is the output.


 

September 9, 2013

C Program Phases Writing And Executing A Program

System Software
The system software controls the computer. It communicates with computer’s hardware (key board, mouse, modem, sound card etc) and controls different aspects of operations. Sub categories of system software are:
  • Operating system
  • Device drivers
  • Utilities
Operating system

An operating system (sometimes abbreviated as "OS") is the program that manages all the other programs in a computer. It is a integrated collection of routines that service the sequencing and processing of programs by a computer. Note: An operating system may provide many services, such as resource allocation, scheduling, input/output control, and data management.

Device drivers

The device driver software is used to communicate between the devices and the computer. We have monitor, keyboard and mouse attached to almost all PC’s; if we look at the properties of these devices we will see that the operating system has installed special software to control these devices. This piece of software is called device driver software. When we attach a new device with the computer, we need software to communicate with this device. These kinds of software are known as device drivers e.g. CD Rom driver, Sound Card driver and Modem driver. Normally manufacturer of the device provide the device driver software with the device. For scanners to work properly with the computers we install the device driver of the scanner. Nowadays if you have seen a scanner, it comes with TWAIN Drivers. TWAIN stands for Technology Without An Interesting Name.

September 4, 2013

Installation of Dev C++


Here is the step by step procedure for the installation of Dev C++
First, download the Dev C++ from the following link.
http://sourceforge.net/projects/orwelldevcpp/files/latest/download

Then after download execute the setup.  The screenshots are of different versions.

1

Select Language.

 2
Click I agree.

September 3, 2013

Introduction to Programming

Definition

"A program is a precise sequence of steps to solve a particular problem.”It means that when we say that we have a program, it actually means that we know about a complete set activities to be performed in a particular order. The purpose of these activities is to solve a given problem. Alan Perlis, a professor at Yale University, says:"It goes against the grain of modern education to teach children to program. What fun is there in making plans, acquiring discipline in organizing thoughts, devoting attention to detail and learning to be self-critical? ". It is a sarcastic statement about modern education, and it means that the modern education is not developing critical skills like planning, organizing and paying attention to detail. Practically, in our day to day lives we are constantly planning, organizing and paying attention to fine details (if we want our plans to succeed). And it is also fun to do these activities. For example, for a picnic trip we plan where to go, what to wear, what to take for lunch, organize travel details and have a good time while doing so.

C program to Read From a File

#include <stdio.h> #include <stdlib.h> void main() {     FILE *fptr;     char filename[15];     char ch;   ...