Thursday, 1 October 2015

Adding arrays from different classes using friend function [cpp]

Q:write a CPP program to declare 3 classes perform the addition of the data members in first 2 classes and result in the 3rd class using friend function

DEFINITION:
friend function:In object-oriented programming, a friend function that is a "friend" of a given class is allowed access to private and protected data in that class that it would not normally be able to as if the data was public . Normally, a function that is defined outside of a class cannot access such information.

PROGRAM:

#include <iostream>
#include<conio.h>
using namespace std;
void add();
class A{
    int a[10],limit;
public:
    A(int m){
        limit=m;
        cout<<"enter array"<<endl;
        for(int i=0;i<limit;i++)
        cin>>a[i];
    }
    friend void add();


};

class B{
    int a[10],limit;
public:
    B(int m){
        limit=m;
        cout<<"enter array"<<endl;
        for(int i=0;i<limit;i++)
        cin>>a[i];
    }
    friend void add();


};

class C{
    int a[10],limit;
public:
    C(){
        limit=0;
        }
    friend void add();

    void addToC(int k){
    a[limit]=k;
    limit++;
    }
    void showC(){
    for(int i=0;i<limit;i++)
        cout<<a[i]<<" ";
    }
};

int main()
{

    add();
}

void add(){
    int limit;
    cout<<"limit:";
    cin>>limit;
    A a(limit);
    B b(limit);
    C c;
    for(int i=0;i<limit;i++)
            c.addToC(a.a[i]+b.a[i]);
    c.showC();

}

Monday, 8 June 2015

[c] STACK USING ARRAY

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

//stack using array
int main()
{
    int stack[10],size,top=0,choice,element;
    printf("enter the stack size");
    scanf("%d",&size);
    while(1){

    printf("\n choose option \n 1.push \n 2. pop \n 3.display \n 4. exit");

    scanf("%d",&choice);
     system("cls");
    switch(choice){
    case 1:
        if(top<size){
           printf("enter element to be inserted");
           scanf("%d",&element);
           stack[top]=element;
           top++;
        }
        else{
            printf("stack full!");
        }
        break;
   case 2:
        if(top>0){
            top--;
            printf("the deleted element is:%d",stack[top]);
        }
        else{
            printf("stack empty");
        }
        break;
   case 3:
        for(element=0;element<top;element++){

            printf(" %d",stack[element]);
        }
        break;
   case 4:
        exit(0);
    }
    }
    return 0;

}

Tuesday, 12 May 2015

[CPP] STACK CLASS THAT CAN HANDLE INT AS WELL AS CHAR (& MORE) - GENERIC CLASS

DESCRIPTION 

 A C++ program to implement a generic stack class.  Include a default constructor, destructor and member functions  push( ) to put values into the stack, pop( ) to remove values from stack, isempty( ) and isfull( ) for testing whether stack is empty or full. Use array implementation of stack. Write a main program to test your class for integer and character type data.

PROGRAM


#include <iostream>
using namespace std;


template<class T>
class stack{

private:
    T a[10];
    int top;

public:
  stack(){
        top=-1;
    }

    int isfull(){
        if(top==9)
           return 1;
           return 0;
                } 


Saturday, 9 May 2015

[ C++ ] WHAT IS THE DIFFERENCE BETWEEN INLINE FUNCTIONS AND MACROS?

Both Inline function  and Macro works the same in almost all scenarios there  are lot of key difference between them.
Before we get into those topics what is this inline and macro actually?
They are kind of like functions but instead of control passing which happens in functions these will replace the actual line of call by the function body..

for example

--------------------------------------------------------------------------------------------------------------------------

MACRO:

    #define MAX(x,y) x>y?x:y

would give the largest among 2 parameters on the call of  MAX(a,b)

If we are to write 
                  
                          int j=MAX(2,3);

  The preprocessor would replace the statement as
                           
                           int j=3;
before execution


INLINE:



inline int add(int a, int b)

    {

    return (a+b);
    
      }



after compiling the code each and every call of this function add in the program is replaced by the body of function

Thursday, 7 May 2015

[C] BINARY TREE OPERATIONS

PROGRAM

#include<conio.h>
#include<stdio.h>
struct node{
            struct node *lchild;
            int info;
   sstruct node *rchild;
               };
struct node search();
struct node insert();
struct node del();
struct node getnodes();
void display();

[C++] CALL BY ADDRESS AND CALL BY REFERENCE FOR SWAPING


CALL BY ADDRESS




PROGRAM

#include <iostream>
#include <conio.h>

void main()
{
int a = 10, b = 5;
swap(&a,&b);
cout << a << "&" << b;

}

void swap(int *m,int *n){
int temp;
temp = *m;
*m = *n;
*n = temp;
}

OUTPUT


  • 5&10


                              CALL BY REFERENCE


PROGRAM

#include <iostream>
#include <conio.h>

void main()
{
int a = 10, b = 5;
swap(a,b);
cout << a << "&" << b;

}

void swap(int &m,int &n){
int temp;
temp = m;
m = n;
n = temp;
}


OUTPUT

  • 5&10

SPARESE MATRIX[READING & ADDITION]

DESCRIPTION

In numerical analysis, a sparse matrix is a matrix in which most of the elements are zero 
so storing these much zeros ore wastage of memory and hence sparse matrices are represented using triplet  representation in computers
triplet  representation is a 3 column representation of a sparse matrix in which first row represent the whole matrix and preceding each row represent each element in it 

PROGRAM

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

int m,n;
void read(int c[5][5]);
void triple(int a[5][5],int t[10][3]);
int triplesum(int t1[10][3],int t2[10][3]);

void main()
{
 int a[5][5],b[5][5],t1[10][3],t2[10][3],i,j;
 clrscr();
 read(a);
 triple(a,t1);
 printf("The triple representation is:\n");
 for(i=0;i<=t1[0][2];i++)
  {
   for(j=0;j<3;j++)
   printf("%d ",t1[i][j]);
   printf("\n");
  }

QUICK SORT [WITHOUT RECURSION]

 DESCRIPTION

Quick sort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. Developed by Tony Hoare in 1959  (published 1962), it is still a very commonly used algorithm for sorting. When implemented well, it can be about two or three times faster than its main competitors, merge sort and heap sort.
Quick sort is a comparison sort, meaning that it can sort items of any type for which a "less-than" relation (formally, a total order) is defined. In efficient implementations it is not a stable sort, meaning that the relative order of equal sort items is not preserved. Quick sort can operate in-place on an array, requiring small additional amounts of memory to perform the sorting.
Mathematical analysis of quick sort shows that, on average, the algorithm takes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare


PROGRAM

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

int partition(int a[],int l,int u);
int n;
void main()
{
   int a[20],n,i,top=0,l,u,s,lower[10],upper[10];
   clrscr();
   printf("Enter the size of array:");
   scanf("%d",&n);
   printf("Enter the elements to be sorted:");
   for(i=1;i<=n;i++)
   scanf("%d",&a[i]);
   if(n>1)
       {
            top=top+1;
            lower[top]=1;
            upper[top]=n;
       }
      while(top!=0)
      {
            l=lower[top];
            u=upper[top];
            top=top-1;
            s=partition(a,l,u);
            if(l<s-1)
             {
              top=top+1;
              lower[top]=1;
              upper[top]=s-1;
             }
            if(s+1<u)
             {
              top=top+1;
              lower[top]=s+1;
              upper[top]=u;
             }

      }
   printf("The sorted array is:");
   for(i=1;i<=n;i++)
   printf("%d ",a[i]);
   getch();
 }



  int partition(int a[],int l,int u)
  {
   int i,j,temp,p,flag;
    i=l;
    j=u+1;
    p=a[l];
    flag=1;
   while((flag==1)&&(j>1))
    {
      i=i+1;
      while(a[i]<p&&i<=n)
       {
            i++;
       }
      j=j-1;
      while(a[j]>p&&j>l)
       {
            j--;
       }
      if(i<j)
       {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
       }
      else
       flag=0;
    }
     a[l]=a[j];
     a[j]=p;
     return j;
 }

OUTPUT
Enter the limit of the array : 4
enter the element to be sorted :9 5 7 2

the sorted array is :2 5 7 9

QUICK SORT [RECURSSIVE]


DESCRIPTION


Quick sort (sometimes called partition-exchange sort) is an efficient sorting algorithm, serving as a systematic method for placing the elements of an array in order. Developed by Tony Hoare in 1959  (published 1962), it is still a very commonly used algorithm for sorting. When implemented well, it can be about two or three times faster than its main competitors, merge sort and heap sort.
Quick sort is a comparison sort, meaning that it can sort items of any type for which a "less-than" relation (formally, a total order) is defined. In efficient implementations it is not a stable sort, meaning that the relative order of equal sort items is not preserved. Quick sort can operate in-place on an array, requiring small additional amounts of memory to perform the sorting.
Mathematical analysis of quick sort shows that, on average, the algorithm takes O(n log n) comparisons to sort n items. In the worst case, it makes O(n2) comparisons, though this behavior is rare

PROGRAM

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

int partition(int a[],int l,int u);
void quicksort(int a[],int l,int u);
int n;
void main()
{
   int a[20],i;
   clrscr();
   printf("Enter the size of array:");
   scanf("%d",&n);
   printf("Enter the elements to be sorted:");
   for(i=1;i<=n;i++)
   scanf("%d",&a[i]);
   quicksort(a,1,n);
   printf("The sorted array is:");
   for(i=1;i<=n;i++)
   printf("%d ",a[i]);
   getch();
 }



  int partition(int a[],int l,int u)
  {
   int i,j,temp,flag,p;
    i=l;
    j=u+1;
    p=a[l];
    flag=1;
   while(flag==1&&j>1)
    {
      i=i+1;
      while(a[i]<p&&i<=n)
       {
            i++;
       }
      j=j-1;
      while(a[j]>p&&j>1)
       {
            j--;
       }
      if(i<j)
       {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
       }
      else
       flag=0;
    }
     a[l]=a[j];
     a[j]=p;
     return j;
 }


  void quicksort(int a[],int l,int u)
    {
     int i,s;
     for(i=1;i<=n;i++)
     if(l<u)
      {
       s=partition(a,l,u);
       quicksort(a,l,s-1);
       quicksort(a,s+1,u);
      }
    }

OUTPUT

enter the limit of the array:5
enter the elements : 8
76
9
12
4
the sorted elements are 4 8 9 12 76


CLICK HERE TO VIEW THE SAME CODE  IN JAVA



SELECTION SORT

PROGRAM

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
  int a[20],n,i,j,temp;
  clrscr();
  printf("\nEnter the limit of the array:");
  scanf("%d",&n);
  printf("Enter the elements:");
  for(i=0;i<n;i++)
  scanf("%d",&a[i]);
  for(i=0;i<n;i++)
  {
    for(j=i+1;j<n;j++)
     {
      if(a[i]>a[j])
       {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
       }
     }
   }
   printf("\nThe sorted array is:");
   for(i=0;i<n;i++)
    printf("%d ",a[i]);
    getch();
 }
 OUTPUT

Enter the limit of the array:5                                                 
Enter the elements:8                                                           
3                                                                              
5                                                                               
1                                                                              
9                                                                              
                                                                                
The sorted array is:1 3 5 8 9