Thursday, October 10, 2019

ARRAY (INSERTION,DELETION,REVERSING)

INSERTION

#include<stdio.h>

int main() {
   int arr[30], element, num, i, location;

   printf("\nEnter no of elements :");
   scanf("%d", &num);

   for (i = 0; i < num; i++) {
      scanf("%d", &arr[i]);
   }

   printf("\nEnter the element to be inserted :");
   scanf("%d", &element);

   printf("\nEnter the location");
   scanf("%d", &location);

   //Create space at the specified location
   for (i = num; i >= location; i--) {
      arr[i] = arr[i - 1];
   }

   num++;
   arr[location - 1] = element;

   //Print out the result of insertion
   for (i = 0; i < num; i++)
      printf("n %d", arr[i]);

   return (0);
}
************************************************
[2]DELETION
#include<stdio.h>

int main() {
   int arr[30], num, i, loc;

   printf("\nEnter no of elements :");
   scanf("%d", &num);

   //Read elements in an array
   printf("\nEnter %d elements :", num);
   for (i = 0; i < num; i++) {
      scanf("%d", &arr[i]);
   }

   //Read the location
   printf("\n location of the element to be deleted :");
   scanf("%d", &loc);

   /* loop for the deletion  */
   while (loc < num) {
      arr[loc - 1] = arr[loc];
      loc++;
   }
   num--;  // No of elements reduced by 1

   //Print Array
   for (i = 0; i < num; i++)
      printf("\n %d", arr[i]);

   return (0);

}

**************************************
REVERSING
#include<stdio.h>

int main() {
   int arr[30], i, j, num, temp;

   printf("\nEnter no of elements : ");
   scanf("%d", &num);

   //Read elements in an array
   for (i = 0; i < num; i++) {
      scanf("%d", &arr[i]);
   }

   j = i - 1;   // j will Point to last Element
   i = 0;       // i will be pointing to first element

   while (i < j) {
      temp = arr[i];
      arr[i] = arr[j];
      arr[j] = temp;
      i++;             // increment i
      j--;          // decrement j
   }

   //Print out the Result of Insertion
   printf("\nResult after reversal : ");
   for (i = 0; i < num; i++) {
      printf("%d \t", arr[i]);
   }

   return (0);
}
****************************
DELETION OF DUPLICATE
#include<stdio.h>

int main() {
   int arr[20], i, j, k, size;

   printf("\nEnter array size : ");
   scanf("%d", &size);

   printf("\nAccept Numbers : ");
   for (i = 0; i < size; i++)
      scanf("%d", &arr[i]);

   printf("\nArray with Unique list  : ");
   for (i = 0; i < size; i++) {
      for (j = i + 1; j < size;) {
         if (arr[j] == arr[i]) {
            for (k = j; k < size; k++) {
               arr[k] = arr[k + 1];
            }
            size--;
         } else
            j++;
      }
   }

   for (i = 0; i < size; i++) {
      printf("%d ", arr[i]);
   }

   return (0);

}*********************

Saturday, October 5, 2019

Reversing Array using pointer

Reversing Array using pointer***********

#include <stdio.h>
#include <stdlib.h>
int main()

{

int *a,n,i,j,temp;

printf("Enter size of array:");
scanf("%d",&n);

a=calloc(sizeof(int),n);
printf("Enter %d Elements:",n);
for(i=0;i<n;i++)

{

scanf("%d\t",a+i);

}



for(i=0,j=n-1;i<j;i++,j--)

{

temp=*(a+i);
*(a+i)=*(a+j);
*(a+j)=temp;
}

printf("After reversing the array:\n");
for(i=0;i<n;i++)

{

printf("%d",*(a+i));

}


return 0;

}

Sorting Using Pointer



Sorting Using Pointer
**************************************
#include <stdio.h>
void sort(int n, int* ptr)
{
    int i,j,t;
    
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
         if(*(ptr+j) < *(ptr+i))
         {
             t            =  *(ptr+i);
             *(ptr+i) =  *(ptr+j);
             *(ptr+j) =    t;
             
         }
        }
    }

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

printf("%d\t",*(ptr+i));
}

int main()
{
    int n=5;
    int arr[]={5,44,11,6,7};
    sort(n,arr);
    return 0;
}

Thursday, October 3, 2019

Polymorphism

Run time Polymorphism(Function Overriding)
****************************************************
Function overriding means when the child class contains the method which is already present in the parent class.
#include<iostream>
using namespace std;
class A {
    public:
    void disp(){
    cout<<"super class function"<<endl;
    }
};
class B : public A
{
    public:
    void disp(){
        cout<<"Sub class function"<<endl;
        
    }
};

int main()
{
    A obj;
    obj.disp();
    
    B obj2;
    obj2.disp();
    
    return 0;
}

Output:
Super class function
Sub class function

::::::::Compile time Polymorphism:::::::::

The polymorphism which is implemented at the compile time is known as compile-time polymorphism. Method overloading is an example of compile-time polymorphism.

Method overloading: Method overloading is a technique which allows you to have more than one function with the same function name but with different functionality.

#include <iostream>
using namespace std;
class Add{
    public:
    int sum(int num1,int num2)
    {    
        return num1+num2;
    }
    int sum(int num1,int num2,int num3)
    {
        return num1+num2+num3;
    }
};

int main()
{
    Add obj;
    cout<<"output:"<<obj.sum(10,20)<<endl;
    cout<<"output:"<<obj.sum(10,20,30)<<endl;
 return 0;

}