Pages

Write a program to create single link list.

Wednesday, 29 May 2013

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
struct node
{
       int data;
       struct node *link;
};
struct node *n, *first, *this1;
int main()
{
    char ch;
    first=NULL;
    printf("Enter q to quit: ");
    ch=getche();
    while(ch!='q')
    {
           n=(struct node *)malloc(sizeof(struct node));
           printf("\nEnter data to store in node: ");
           scanf("%d", &n->data);
           n->link=NULL;
           if(first==NULL)
           {
                  first=n;
           }
           else
           {
                  this1=first;
                  while(this1->link!=NULL)
                  {
                          this1=this1->link;
                  }
                  this1->link=n;
           }
           printf("Press q to quit: ");
           ch=getche();
    }
    printf("\n");
    this1=first;
           while(this1!=NULL)
           {
                   printf("%d\n", this1->data);
                   this1=this1->link;
           }
    getch();
   
}

Write a program to multiply two matrix and show result on screen

Tuesday, 28 May 2013

#include <stdio.h>
#include <conio.h>
int main()
{
    int i, j, k, a1[5][5], a2[5][5], a3[5][5], r1, r2, c1, c2, sum;
    printf("Enter number of rows for fisrt metrix: ");
    scanf("%d", &r1);
    printf("Enter number of columns for fisrt metrix: ");
    scanf("%d", &c1);
    printf("Enter number of rows for second metrix: ");
    scanf("%d", &r2);
    printf("Enter number of columns for second metrix: ");
    scanf("%d", &c2);
    if(c1!=r2)
    {
           printf("Multiplication cannot take place, columns of first metrix should be equal to second row.");
    }
    else
    {
        // enter elements in metrix
        for(i=0; i<r1; i++)
                 {
                        for(j=0; j<c1; j++)
                        {
                                 printf("Enter element of row %d and colimn %d: ", i, j);
                                 scanf("%d",&a1[i][j]);
                        }
                 }
        for(i=0; i<r2; i++)
                 {
                        for(j=0; j<c2; j++)
                        {
                                 printf("Enter element of row %d and colimn %d: ", i, j);
                                 scanf("%d", &a2[i][j]);
                        }
                 }
                
        for(i=0; i<r1; i++)
        {
                 for(j=0; j<c2; j++)
                 {
                          sum=0;
                          for(k=0; k<c1; k++)
                          {
                                   sum=sum+a1[i][k]*a2[k][j];
                                   a3[i][j]=sum;
                          }
                 }
        }
             //show elements of metrix.
        printf("\n");
        for(i=0; i<r1; i++)
                 {
                        for(j=0; j<c1; j++)
                        {
                                 printf("%d\t", a1[i][j]);
                        }
                        printf("\n");
                 }
        printf("\n");
        for(i=0; i<r2; i++)
                 {
                        for(j=0; j<c2; j++)
                        {
                                 printf("%d\t", a2[i][j]);
                        }
                        printf("\n");
                 }
        printf("Multiplication of array: \n");
        for(i=0; i<r2; i++)
                 {
                        for(j=0; j<c2; j++)
                        {
                                 printf("%d\t", a3[i][j]);
                        }
                        printf("\n");
                 }
    }
    getch();
}

Write a program in c language to find first and second largest number from array.

Monday, 27 May 2013

#include <stdio.h>
#include <conio.h>
int main()
{
    int i, a[10], loc1=0, loc2=0, max1=0, max2=0;
    for(i=1; i<=5; i++)
    {
             printf("Enter number: ");
             scanf("%d", &a[i]);
    }
    for(i=1; i<=5; i++)
    {
             if(max1<a[i])
             {
                    max2=max1;
                    max1=a[i];
                    loc2=loc1;
                    loc1=i;
             }
             if(max2<a[i] && a[i]<max1)
             {
                     max2=a[i];
                     loc2=i;
             }
    }
    printf("First largest is %d  at %d and Second largest is %d at %d",max1, loc1, max2, loc2);
    getch();
}


Write a C program to implement the following: 1. Take an int array of 100 elements. Populate it using random number generator built-in function. 2. User will be given the following choices: a) To insert a new element in the array. b) To delete an element in the array. c) To search an item in the array. d) To update an item in the array. e) To show the elements of the array. f) To quit from the program.

#include <stdio.h>
#include <conio.h>
#include <iostream.h>
int main()
{
    int i, a[100], loc, item, size=99;
    char option;
    for(i=0; i<=size; i++)
    {
             a[i]= rand() % 20;
    }
    printf("Press a, b, c, d, e, f\na)  To insert a new element in the array. \nb)  To delete an element in the array.  \nc)  To search an item in the array. \nd)  To update an item in the array. \ne)  To show the elements of the array. \nf)  To quit from the program.");
    option=getche();
    while(option!='f')
    {
    switch(option)
    {
           case 'a':
                if(size==100)
                {
                             printf("you can never add more elements because array in completlty filled.");
                }
                else
                {
                    printf("\nEnter location where you want to enter number: ");
                    scanf("%d", &loc);
                    printf("\nEnter number: ");
                    scanf("%d", &item);
                    for(i=size+1; i>loc; i--)
                                {
                                         a[i]=a[i-1];
                                }
                   a[loc]=item;
                   size++;
                   break;
                }
    case 'b':
         printf("\nEnter location where you want to delete number: ");
    scanf("%d", &loc);
    for(i=loc; i<=size; i++)
    {
             a[i]=a[i+1];
    }
    size--;
    break;
    case 'c':
         printf("\nEnter number which you want to search from array: ");
    scanf("%d", &item);
    for(i=0; i<=size; i++)
    {
             if(item==a[i])
             {
                         loc=i;
                         break;
             }
    }
    if(i>size)
    {
           printf("\nNumber is not found in array.");
    }
    else
    {
        printf("\n%d is found at location %d", item, loc);
    }
    break;
    case 'd':
         printf("\nEnter location where you want to update number: ");
    scanf("%d", &loc);
    printf("Enter number: ");
    scanf("%d", &item);
    a[loc]=item;
    for(i=0; i<=size; i++)
    {
             printf("%d ", a[i]);
    }
    break;
    case 'e':
    printf("\n");
    for(i=0; i<=size; i++)
    {
             printf("%d ", a[i]);
    }
    break;
         default:
         printf("\nYou have entered invalid entry.");
}
printf("\nPress a, b, c, d, e, f\na)  To insert a new element in the array. \nb)  To delete an element in the array.  \nc)  To search an item in the array. \nd)  To update an item in the array. \ne)  To show the elements of the array. \nf)  To quit from the program.");
option=getche();
}
}
 

Search Box

Most Reading

Contact Form

Name

Email *

Message *