Wednesday, 10 September 2014


#include <stdio.h>
#include<conio.h>
int main ()
{
    int number[30];
    int i, j, a, n;

    printf("Enter the value of N\n");
    scanf("%d", &n);
    printf("Enter the numbers \n");
    for (i = 0; i < n; ++i)
    scanf("%d", &number[i]);
    for (i = 0; i < n; ++i)
    {
        for (j = i + 1; j < n; ++j)
        {
            if (number[i] < number[j])
            {
                a = number[i];
                number[i] = number[j];
                number[j] = a;
            }
        }
    }
    printf("The numbers arranged in descending order are given below\n");
    for (i = 0; i < n; ++i)
    {
        printf("%d\n", number[i]);
    }
}
Enter the value of N
3
Enter the numbers
45
65
25
The numbers arranged in descending order are given below
65
45
25

--------------------------------
Process exited after 13.55 seconds with return value 3
Press any key to continue . . .
#include<stdio.h>
#include<conio.h>
int main()
{
     int ar[100],j,n,i,tmp;
   
     printf(" Enter the size of the array \t");
     scanf("%d",&n);
   
     printf("Now enter the elements in the array \t");
     for(i=0;i<n;i++)
     {
           scanf("%d",&ar[i]);
     }
   
     printf("\n Array is - ");
     for(i=0;i<n;i++)
     {
           printf("\t %d",ar[i]);
     }
   
     for(i=0;i<n;i++)
     {
           for(j=0;j<n-i;j++)
           {
                 if(ar[j]>ar[j+1])
                 {
                       tmp=ar[j];
                       ar[j]=ar[j+1];
                       ar[j+1]=tmp;
                 }
           }
     }
   
     printf("\n\n Array in the ascending order is - \n");
     for(i=0;i<n;i++)
     {
           printf("\t %d",ar[i]);
     }
     getch();
     return 0
}

; Enter the size of the array    4
Now enter the elements in the array     45
65
23
32

 Array is -      45      65      23      32

 Array in the ascending order is -
         0       23      32      45

LARGEST ELEMENT IN AN ARRAY

//CODE
#include<stdio.h>
int main()
{
 int a[10],i,n,large;
 printf("\n Enter the size of the array: ");
 scanf("%d",&n);
 printf("\n Enter the elements of the array: \n");
 for(i=0;i<n;i++)
 {
  scanf("%d",&a[i]);
 }
 large=a[0];
 i=1;
 while(i<n)
 {
  if(large<a[i])
  {
   large=a[i];
   i++;
  }
  else
  {
   i++;
  }
 }
 printf("\n The largest element in the array is %d \n",large);
 return 0;
}

//OUTPUT
Enter the size of the array:6
Enter the elements of the array:
5
8
9
4
1
7
The largest element in the array is 9

INSERTION OF AN ELEMENT IN A DESIRED POSITION IN A 1D ARRAY

//CODE
#include<stdio.h>
int main()
{
int i,n,num,pos,a[10];
printf("\n Enter the size of the array:");
scanf("%d",&n);
printf("\n Enter the elements of the array:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n Enter the number to be inserted:");
scanf("%d",&num);
printf("\n Enter the position at which the number has to be added:");
scanf("%d",&pos);
for(i=n;i>=pos;i--)
{
a[i+1]=a[i];
}
a[pos]=num;
printf("\n The array after insertion of %d is:",num);
for(i=0;i<n+1;i++)
{
printf("\n a[%d] = %d",i,a[i]);
}
return 0;
}

//OUTPUT
Enter the size of the array:5
Enter the elements:9
7
8
2
3
4
Enter the number to be inserted:10
Enter the position at which the number has to be added:3
The array after insertion of 10 is:
a[0]=9
a[1]=7
a[2]=8
a[3]=10
a[4]=2
a[5]=3
a[6]=4
please find Error in this code-/* (Descending) Ritu
C program to sort the array in the Descending order */
#include<stdio.h>
#include<conio.h>
void main()
{
  int ar[100],j,n,i,tmp;
     printf(" Enter the size of the array: ");
     scanf("%d",&n);
     printf("Enter the elements in the array: ");
     for(i=0;i<n;i++)
     {
           scanf("%d",&ar[i]);
     }
     printf("\n Array is - ");
     for(i=0;i<n;i++)
     {
           printf("\n%d",ar[i]);
     }
     for(i=0;i<n;i++)
     {
           for(j=0;j<n-i;j++)
           {
                 if(ar[j]<ar[j+1])
                 {
                       tmp=ar[j+1];
                       ar[j+1]=ar[j];
                       ar[j]=tmp;
                 }
           }
     }
     printf("\n Array in the Descending order is -\n");
     for(i=1;i<=n;i++)
     {
           printf("\n%d",ar[i]);
     }
     getch();
}

MATRIX MULTIPLICATION

//CODE

#include<stdio.h>
int main()
{
int a[10][10],b[10][10],s[10][10];
int sum=0,i,j,k,r1,c1,r2,c2;
printf("\n Enter the rows of 1st matrix:");
scanf("\n %d",&r1);
printf("\n Enter the columns of 1st matrix:");
scanf("\n %d",&c1);
printf("\n Enter the rows of 2nd matrix:");
scanf("\n %d",&r2);
printf("\n Enter the columns of 2nd matrix:");
scanf("\n %d",&c2);
if(r2==c1)
{
printf("\n Enter the matrix a:\n ");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n Enter the matrix b:\n ");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\n The matrix a is as follows:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf(" \t %d",a[i][j]);
}
printf("\n");
}
printf("\n The matrix b is as follows:\n");
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf(" \t %d",b[i][j]);
}
printf("\n");
}
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
sum=0;
for(k=0;k<c2;k++)
{
sum=sum+(a[i][k]*b[k][j]);
}
s[i][j]=sum;
}
}
printf("The resultant matrix is: \n");
for(i=0;i<r1;i++)
{
for(j=0;j<c2;j++)
{
printf("\t %d",s[i][j]);
}
printf("\n");
}
}
else
{
printf("\n Matrix cannot be multiplied...Try again");
}
getch();
return 0;
}

//OUTPUT
Enter the rows of 1st matrix:2
Enter the columns of 1st matrix:3
Enter the rows of 2nd matrix:3
Enter the columns of 2nd matrix:2
Enter the matrix a:
2
1
3
5
6
4
Enter the matrix b:
9
8
7
3
2
5
The matrix a is as follows:
2 1 3
5 6 4
The matrix b is as follows:
9 8
7 3
2 5
The resultant matrix is:
25 19
87 58
//maximum
#include <stdio.h>
#include<conio.h>
void main()
{
  int ar[100],max,size,c,loc=1;
  printf("Enter the number of elements in array\n");
  scanf("%d",&size);
  printf("Enter %d Number\n", size);
  for (c=0;c<size;c++)
    scanf("%d",&ar[c]);
  max=ar[0];
  for (c=1;c<size; c++)
  {
    if (ar[c]>max)
    {
       max=ar[c];
       loc=c+1;
    }
  }
  printf("Maximum element is at location %d and it's value is %d.\n",loc,max);
  getch();
}
output:

// SORT THE ELEMENT IN  ASCENDING ORDER

#include<stdio.h>
#include<conio.h>
int main(void)
{
    int arr[10],i,j,n,temp,xchanges;
   
    printf("enter the element");
    scanf("%d",&n);
   
    for(i=0;i<n;i++)
    {
    printf("enter the element %d \n",i+1);
    scanf("%d",&arr[i]);
    }
   
    for(i=0;i<n-1;i++)
    {
                      xchanges=0;
           for(j=0;j<n-1-i;j++)
           {
                               if(arr[j]>arr[j+1])
                               {
                               temp=arr[j];
                               arr[j]=arr[j+1];
                               arr[j+1]=temp;
                               temp=0;
                               xchanges++;
                               }
                              
                              
           }
           if(xchanges==0)
                               break;
                     
    }
    printf("sort Ascending order is :-\n");
    for(i=0;i<n;i++)
    printf("%d ",arr[i]);
    printf("\n ");
    getch();
}


                                                               OUTPUT        



 

SORTING IN ASCENDING ORDER

//CODE

#include<stdio.h>
int main()
{
 int a[10],i,n,pass,tmp=0;
 printf("\n Enter the size of the array:");
 scanf("%d",&n);
 printf("\n Enter the elements of the array:\n");
 for(i=0;i<n;i++)
 {
  scanf("%d",&a[i]);
 }
 pass=1;
 while(pass<n)
 {
  for(i=0;i<n-pass;i++)
  {
   if(a[i]>a[i+1])
   {
    tmp=a[i];
    a[i]=a[i+1];
    a[i+1]=tmp;
   }
  }
  pass=pass+1;
 }
 printf("\n The sorted array in ascending order is as follows:\n");
 for(i=0;i<n;i++)
 {
  printf("%d \n",a[i]);
 }
 getch();
 return 0;
}
//OUTPUT
Enter the size of the array:5
Enter the elements of the array:
46
79
82
43
16
The sorted array in ascending order is as follows:
16
43
46
79
82
 

SORTING IN DESCENDING ORDER

//CODE

#include<stdio.h>
int main()
{
int a[10],i,n,pass,tmp=0;
printf("\n Enter the size of the array:");
scanf("%d",&n);
printf("\n Enter the elements of the array:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
pass=1;
while(pass<n)
{
for(i=0;i<n-pass;i++)
{
if(a[i]<a[i+1])
{
tmp=a[i];
a[i]=a[i+1];
a[i+1]=tmp;
}
}
pass=pass+1;
}
printf("\n The sorted array in decending order is as follows:\n");
for(i=0;i<n;i++)
{
printf("%d \n",a[i]);
}
getch();
return 0;
}
//OUTPUT

Enter the size of the array:5
Enter the elements:
46
79
81
23
56
The sorted array in descending order is as follows:
81
79
56
46
23
Remark: Can Anyone find error in this code......
/*c program to insert an element an array user specified loaction*/
#include <stdio.h>
#include<conio.h>
int main()
{
   int ar[100],pos,c,n,val;
   printf("Enter number of elements in array\n");
   scanf("%d", &n);
   printf("Enter %d elements\n",n);
   for (c = 0; c < n; c++)
      scanf("%d", &ar[c]);
   printf("Enter the location where you wish to insert an element\n");
   scanf("%d", &pos);
   printf("Enter the value to insert\n");
   scanf("%d", &val);
   for (c=n-1;c>= pos-1;c--)
      ar[c+1] = ar[c];
   ar[pos-1]=val;
   printf("array is\n");
   for (c=0;c<=n;c++)
      printf("%d\n",ar[c]);
      getch();
   return 0;  
}

Output:
Remark: Can Anyone find error in this code......
//C program to arrange or sort the array in the ascending order
#include<stdio.h>
#include<conio.h>
void main()
{
     int ar[100],j,n,i,tmp;
     
     printf(" Enter the size of the array \t");

     scanf("%d",&n);
     
     printf("Enter the elements in the array \t");
     for(i=0;i<n;i++)
     {
           scanf("%d",&ar[i]);
     }
     printf("\n Array is -");
     for(i=0;i<n;i++)
     {
           printf("\n5%d",ar[i]);
     }    
     for(i=0;i<n;i++)
     {
           for(j=0;j<n-i;j++)
           {
                 if(ar[j]>ar[j+1])
                 {
                       tmp=ar[j];

                       ar[j]=ar[j+1];

                       ar[j+1]=tmp;
                 }
           }
     }    
     printf("\nArray in the ascending order is-\n");
     for(i=0;i<n;i++)
     {
           printf("\n%d",ar[i]);
     }
     getch();
}

Wednesday, 3 September 2014

Advancement of topic "Test Class"

//This code will work properly with the duplicacy of elements in the array
#include<stdio.h>
#include"conio.h"
float main()
{
int list[100],n,i,x,t,p[100],j; //Declaration of variable;
printf("Enter the size of list: ");
scanf("%d",&n); //Entering the size of array 'list'
printf("Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&list[i]); //Insertion of elements in array 'list'
}
printf("Enter the element to be here: ");
scanf("%d",&x); //Entering the element whose index has to be find
p[0]=-1;
j=0;
for(i=0;i<n;i++)
{
if(list[i]==x) //Matching each element of array 'list' with the entered element
{
p[j]=i; //Assinging the index value to variable p if the match success
j++;
}
}
i=0;
do
{
if(p[i]>=0)
{
printf("\nIndex of element %d is %d",x,p[i]); //Position of element i.e. index value in array 'list'
                        printf("\tPosition of element %d is %d",x,p[i]+1); //Position of element i.e. normal position in array 'list'
}
else if(p[i]<=0)
{
printf("Try again");
}
i++;
}while(i<j);
        getch();                                                    //getch() function is not required in DEV C++ compiler
return 0;
}

search number and show adress in list

# include <stdio.h>
# include <conio.h>
  int main()
  {
      int x,list[10],i,n,p;
       
          printf("enter the size of list\n");
       
          scanf("%d",n);//save list number in varable n
       
          printf("enter the number of elementry\n");
   
      for(i=0;i<n;i++)//intialize the element in for looping
       
           scanf("%d",list[i]);//save number in array list[i]
       
               p=0;
       
          printf("enter the element");
       
          scanf("%d",x);
   
      for(i=0;i<n;i++)//check search number and show help for looping
      {
   
             if(list[i]==x)//checkgiven number for array index
   
                   p=i;
      }
   
             if(p=0)//if number given then number adress print
   
                  printf("show the element address %d is %d\n",x,p);
   
               else //if no not search then try again
   
                   printf("try again");
   
                      getch();//prog processing end then back main screen
                   
                      return 0;
 }
                   




                        OUTPUT
   
              enter the size of list
                   5
              enter the number of element
                  2
                  5
                  4
                  3
                  6
              show the element adress
               4 is 2

/* INSERTION, DELETION AND DISPLAY OF DOUBLY LINKED LIST*/
 #include<iostream.h>
 #include<conio.h>
 class linklist
 {
 private:
 struct dnode     
 {
 dnode * prev;
 int data;
 dnode * next;
 }*p;
 public:
 linklist();
 void append(int num);
 void addatbeg(int num);
 void addafter(int loc,int num);
 void deleten(int num);
 void display();
 ~linklist();
 };
 linklist::linklist()
 {
 p=NULL;
 }
 void linklist::append(int num)// CREATE A NODE
 {
 dnode * q,*r;
 q=p;
 if(q==NULL)// IF CREATE NODE IS FIRST NODE THEN IF BODY WILL EXECUTE,OTHERWISE ELSE BODY EXECUTE
 {
 q=new dnode;
 q->prev=NULL;
 q->data=num;
 q->next=NULL;
 p=q;
 }
 else
 {
 while(q->next!=NULL)
 q=q->next;
 r=new dnode;
 r->data=num;
 r->next=NULL;
 r->prev=q;
 q->next=r;
 }
 }
 void linklist::addatbeg(int num)
 {
 dnode * q;
 q=new dnode;
 q->prev=NULL;
 q->data=num;
 q->next=p;
 q->prev=q;
 p=q;
 }
 void linklist::addafter(int loc,int num)
 {
 dnode * q;
 q=p;
 for(int i=0;i<loc;i++)// SEARCH THE LOCATION WHERE THE NEW NODE TO BE INSERTED
 {
 q=q->next;
 if(q==NULL)
 {
 cout<<"\n There are lessthan"<<loc<<"Elements";
 return;
 }
 }
 q=q->prev;
 dnode * temp=new dnode;// ASSIGN THE NEW NODE
 temp->data=num;
 temp->prev=q;
 temp->next=q->next;//CREATE THE LINK
 temp->next->prev=temp;
 q->next=temp;
 }
 void linklist::deleten(int num)
 {
 dnode * q=p;
 while(q!=NULL)// FOUND THE DELETED NODE
    {
     if(q->data==num)
     {
     if(q==p)
 {
 p=p->next;
  p->prev=NULL;
 }
else   
       {
if(q->next==NULL)
  q->prev->next=NULL;
  else
  {
  q->prev->next=q->next;
  q->next->prev=q->prev;
  }
 delete q;
 }
 return;
 }
 q=q->next;
 }
 cout<<num<<"not found";
 }
 void linklist::display()//DISPLAY THE LIST
 {
 dnode * temp=p;
 cout<<endl;
 while(temp!=NULL)
 {
 cout<<temp->data<<" ";
 temp=temp->next;
 }
 }
 linklist::~linklist()//
 {
 dnode * q;
 while(q->next!=NULL)
 {
 q=p->next;
 delete p;
 p=q;
 }
 }
 void main()
 {
 linklist l;
 clrscr();
 l.append(14);
 l.append(22);
 l.append(23);
 l.append(43);
 l.append(12);
 cout<<"\n Elements in doubly linkledlist:";
 l.display();
 l.addatbeg(8);
 l.addatbeg(13);
 cout<<"\n Elements in doubly linkedlist after insertion at begining:";
 l.display();
 l.addafter(4,66);
 l.addafter(2,96);
 cout<<"\n Elements in doubly linkedlist after inserting particular location:";
 l.display();
 l.deleten(8);
 l.deleten(13);
 l.deleten(66);
 cout<<"\n After deleting some Elements:";
 l.display();
 getch();
 }
/* OUTPUT:
          Elements in doubly linkedlist:14 22 23 43 12
          Elements in doubly linkdelist after insertion at begining:8 13 14 22 23 43 12
           Elements in doubly linkedlist after inserting particular location:8 13 96 14 66 22 23 43 12
           After deleting some Elements: 96 14 22 23 43
           */

test1

#include<stdio.h>
int main()
{
    int list[100],n,i,x,p;
    printf("enter the size of list.....");
    scanf("%d",&n);
    printf("enter the element\n");
    for(i=0;i<n;i++)
    {
    scanf("%d",&list[i]);
    }
    p=-1;
    printf("enter the element to be search");
    scanf("%d",&x);
   for(i=0;i<n;i++)
    {
     if(list[i]==x)
       {
         p=i;
       }
     }             
   
     if(p>=0)
      {
        printf("%d is found at index %d ",x,p);
      }
      else
       {
         printf("%d not found in the list try again",x);
        }
    getch();
    return 0;
    }


output:enter the size of list.....5
enter the element
56
78
90
43
45
enter the element to be search 90
90 is found at index 2

enter the size of list.....3
enter the element
50
60
70
enter the element to be search10
10 not found in the list try again

Final submission of lab prog.(Searching an element and their position in an array)

#include<stdio.h>
#include<conio.h>
int main()
{
    int list[100],n,x,i,p;
    printf("\n enter the size of list");
    scanf("%d",&n);
    printf("\n enter the elements");
    for(i=0;i<n;i++)
    scanf("%d",&list[i]);
    p=0;
    printf("\n enter the element to be search");
    scanf("%d",&x);
    for(i=0;i<n;i++)
    {
                    if(list[i]==x)
                    p=i;
    }
    if(p>0)
    {
           printf("\nthe element %d is at position %d",x,p+1);
    }
    else
    {
        printf("\n wcta");
    }
getch();
return 0;
}