May 11, 2012

C program to merge two arrays.

Arrays in C.
Write a C program to merge two arrays.
In this program we check the elements of arrays A, B and put that elements in the resulted array C in sorted manner.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
void main( )
{
    int n,m,i,j,k,c[40],a[20],b[20];
    clrscr ();
    printf("Enter how many elements for array A?:\n");
    scanf("%d",&n);
    printf  ("Enter how many elements for array B?:\n");
   scanf("%d",&m);
    printf("Enter elements for A:-\n");
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);
    printf("Enter elements for B:-\n");
    for(j=0;j<m;j++)
    scanf("%d",&b[j]);
    i=j=k=0;
    while(i<n&&j<m)
    {
        if(a[i]<b[j])
        c[k++]=a[i++];
        else
        if(a[i]>b[j])
        c[k++]=b[j++];
        else
        
        {
            c[k++]=b[j++];
            i++;
            j++;
        }
    }
    
    if(i<n)
    {
        int t;
        for(t=0;t<n;t++)
        
        c[k++]=a[i++];
    }
    if(j<m)
    {
        int t;
        for(t=0;t<m;t++)
        {
            c[k++]=b[j++];
        }
    }
    printf("\n\n Merged Array C:\n\n")
    for(k=0;k<(m+n);k++)
    printf("\t \n %d ",c[k]);
    getch();
}
Read more Similar C Programs
Data Structures

Learn C Programming
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

May 10, 2012

K & R C Chapter 6 Exercise Solutions.

We have already provided solutions to all the exercises in the book "C Programming Language (2nd Edition)" popularly known as K & R C book.

In this blog post I will give links to all the exercises from Chapter 6 of the book for easy reference.

Chapter 6: Structures

  1. Exercise 6-1. Our version of getword does not properly handle underscores, string constants, comments, or preprocessor control lines. Write a better version.
    Solution to Exercise 6-1.

  2. Exercise 6-2.Write a program that reads a C program and prints in alphabetical order each group of variable names that are identical in the first 6 characters but different somewhere thereafter. Don't count words within strings and comments. Make 6 a parameter that can be set from the command line.
    Solution to Exercise 6-2.

  3. Exercise 6-3.Write a cross-referencer that prints a list of all words in a document, and, for each word, a list of the line numbers on which it occurs. Remove noise words like "the," "and," and so on.
    Solution to Exercise 6-3.

  4. Exercise 6-4. Write a program that prints the distinct words in its input sorted into decreasing order of frequency of occurrence. Precede each word by its count.
    Solution to Exercise 6-4.

  5. Exercise 6-5. Write a function undef that will remove a name and definition from the table maintained by lookup and install .
    Solution to Exercise 6-5.

  6. Exercise 6-6. Implement a simple version of the #define processor (i.e., no arguments) suitable for use with C programs, based on the routines of this section. You may also find getch and ungetch helpful.
    Solution to Exercise 6-6.
You can purchase the book from here or here.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

May 8, 2012

C Program to search the linked list.

Data structures using C,
Write c program to search the linked list.
Linked list is a data structure in which the objects are arranged in a linear order. In this program, we sort the list elements in ascending order. Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.
/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct l_list
{
    int info;
    struct link *next;
}
start, *node;

int search(int);
void main()
{
    int no,i,item,pos;
    clrscr();
    start.next=NULL;
    node=&start;
    printf("How many nodes, you want in linked list? ");
    scanf("%d",&no);
    printf("");
    for(i=0;i<no;i++)
    {
        node->next=(struct l_list *)malloc(sizeof(struct l_list));
        printf("Enter element in node %d: ",i+1);
        scanf("%d",&node->info);
        node=node->next;
    }
    node->next=NULL;
    printf("Linked list(only with info field) is:");
    
    node=&start;
    while(node->next!=NULL)
    {
        printf("%d    ",node->info);
        node=node->next;
    }
    printf("
    
    Enter item to be searched : ");
    scanf("%d",&item);
    pos=search(item);
    if(pos<=no)
    printf("Your item is at node %d",pos);
    else
    printf("Sorry! item is no in linked list.a");
    getch();
}

int search(int item)
{
    int n=1;
    node=&start;
    while(node->next!=NULL)
    {
        if(node->info==item)
        break;
        else
        n++;
        node=node->next;
    }
    return n;
}
Read more Similar C Programs
Data Structures

Learn C Programming
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link

C Program to find the day of birth.

Write a C Program to find the day of birth when date of birth is given.
In this program we find the day of birth like Monday, Sunday.. when date of birth is given.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.


/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

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

main()
{
    clrscr();
    int d,m,y,year,month,day,i;
    
    printf("Enter the date :");
    scanf("%d%d%d",&d,&m,&y);
    if( d>31 || m>12 || (y<1900 || y>=2000) )
    {
        if(y>=2000)
        {
            printf("\n\n YOU ARE TOO SMALL To run this program!\n\n");
        }
        printf("INVALID INPUT");
        getch();
        exit(0);
    }
    
    year = y-1900;
    year = year/4;
    year = year+y-1900;
    
    switch(m)
    {
        case 1:
        case 10:
        month = 1;
        break;
        case 2:
        case 3:
        case 11:
        month = 4;
        break;
        case 7:
        case 4:
        month = 0;
        break;
        case 5:
        month = 2;
        break;
        case 6:
        month = 5;
        break;
        case 8:
        month = 3;
        break;
        case 9:
        case 12:
        month = 6;
        break;
    }
    year = year+month;
    year = year+d;
    day  = year%7;
    
    switch(day)
    {
        case 0:
        printf("Day is SATURDAY\n");
        break;
        case 1:
        printf("Day is SUNDAY\n");
        break;
        case 2:
        printf("Day is MONDAY\n");
        break;
        case 3:
        printf("Day is TUESDAY\n");
        break;
        case 4:
        printf("Day is WEDNESDAY\n");
        break;
        case 5:
        printf("Day is THURSDAY\n");
        break;
        case 6:
        printf("Day is FRIDAY\n");
        break;
    }
    
    getch();
    return 0;
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


To browse more C Programs visit this link

C program to print all the possible permutations of given digits

Write a C program to print all the possible permutations of given digits.
Permutations means possible way of rearranging in the group or set in the particular order.
Example:
Input:1, 2, 3
Output:1 2 3, 1 3 2, 2 1 3, 3 1 2, 2 3 1, 3 2 1
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int lev=-1,n,val[50],a[50];
void main()
{
    int i,j;
    clrscr();
    printf("Enter how many numbers?\n");
    scanf("%d",&n);
    printf("\nEnter %d numbers:\n\n",n);
    for(i=0;i<n;i++)
    {
        val[i]=0;
        j=i+1;
        scanf("%d\n\n",&a[j]);
    }
    visit(0);
    getch();
}
visit(int k)
{
    int i;
    val[k]=++lev;
    if(lev==n)
    {
        for(i=0;i<n;i++)
        printf("%2d",a[val[i]]);
        printf("        ");
    }
    for(i=0;i<n;i++)
    if(val[i]==0)
    visit(i);
    lev--;
    val[k]=0;
    
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!



To browse more C Programs visit this link

May 7, 2012

C Program to toss a coin using random function.

Write a c program to toss a coin using random function.
In this Program,We use the rand()%2 function that will compute random integers 0 or 1.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.


/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main(void) {
    int toss = 0;
    int call = 0;
    srand(time(NULL));
    
    toss = rand() % 2;
    
    printf("Say head or tail! press 0 for head and 1 for tail:\n\n");
    scanf("%d", &call);
    if(call==0 || call==1)
    {
        if(toss == call)
        {
            if(toss==1)
            printf("You called it correctly ... it is tail\n");
            else
            printf("You called it correctly ... it is head\n");
        }
        else
        {
            if(toss==1)
            printf("No way ...it is head !\n");
            else
            printf("No way ... it is tail!\n");
        }
    }
    else
    printf("Invalid call!!!!!\n\n");
    return 0;
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!


To browse more C Programs visit this link

C program to demonstrate assert macro.

Write a C program to demonstrate assert macro.
assert macro defined in assert.h library. assert is mainly used for debugging. assert function checks the conditions at run time, and program executes only if the assert is true otherwise program aborts and shows the error message.
Read more about C Programming Language . and read the C Programming Language (2nd Edition). by K and R.

/***********************************************************
* You can use all the programs on  www.c-program-example.com
* for personal and learning purposes. For permissions to use the
* programs for commercial purposes,
* contact info@c-program-example.com
* To find more C programs, do visit www.c-program-example.com
* and browse!
* 
*                      Happy Coding
***********************************************************/

#include <stdio.h>
#include <assert.h>
#include<conio.h>
int main()
{
    int num1, num2;
    clrscr();
    printf("Enter two numbers:\n\n");
    scanf("%d %d", &num1, &num2);
    assert(num2 != 0);
    printf("%d/%d = %.2f\n", num1, num2, num1/(float)num2);
    return 0;
}
Read more Similar C Programs
Learn C Programming
Recursion
Number System
You can easily select the code by double clicking on the code area above.

To get regular updates on new C programs, you can

You can discuss these programs on our Facebook Page. Start a discussion right now,

our page!

Share this program with your Facebook friends now! by liking it


(you can send this program to your friend using this button)

Like to get updates right inside your feed reader? Grab our feed!

To browse more C Programs visit this link