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");
  }