Menu:

ADA (8) OOT (5)

Saturday, September 17, 2016

Constructors With Default Arguments

Constructors With Default Arguments

Similar to functions, it is also possible to declare constructors with default arguments. Consider the following example:
power(int 9,int 3);

In the above example, the default value for the first argument is nine and two for second.

power p1 (3);

In this statement, object p1 is created and 9 raise to 3 expression n is calculated. Here, one argument is absent hence default value 9 is taken, and its third power is calculated. Consider the following example on the above discussion:


9.10 Write a program to declare default arguments in a constructor. Obtain the power of the number.
Explanation: In the above program, the class power is declared. It has three integer member variables and one member function show(). The show() function is used to display the values of member data variables. The constructor of class power is declared with two default arguments. In the function main(), p1 and p2 are two objects of class power. The p1 object is created without argument. Hence, the constructor uses default arguments in pow() function. The p2 object is created with one argument. In this call of constructor, the second argument is taken as default. Both the results are shown in output.

Tuesday, April 26, 2016

Binary Tree using linked list with preorder traversal.

Code:-
#include<stdio.h>




typedef struct node


{


    int data;


    struct node *left;


    struct node *right;


}node;




node *create()


{


    node *p;


    int x;


    printf("Enter data(-1 for no data):");


    scanf("%d",&x);




    if(x==-1)
return NULL;




    p=(node*)malloc(sizeof(node));


    p->data=x;




    printf("Enter left child of %d:\n",x);


    p->left=create();


    printf("Enter right child of %d:\n",x);


    p->right=create();




    return p;


}




void preorder(node *t)              //address of root node is passed in t


{


    if(t!=NULL)


    {
printf("\n%d",t->data);  
//visit the root
preorder(t->left);          //preorder traversal on left subtree
preorder(t->right);         //preorder traversal om right subtree
}


}




int main()


{


    node *root;


    root=create();




    printf("\nThe preorder traversal of tree is:\n");


    preorder(root);


    return 0;


}



Output:-
Enter data(-1 for no data):5
Enter left child of 5:
Enter data(-1 for no data):8
Enter left child of 8:
Enter data(-1 for no data):-1
Enter right child of 8:
Enter data(-1 for no data):-1
Enter right child of 5:
Enter data(-1 for no data):9
Enter left child of 9:
Enter data(-1 for no data):-1
Enter right child of 9:
Enter data(-1 for no data):-1

The preorder traversal of tree is:

5
8
9

Tuesday, April 12, 2016

Friend Funtion in C

#include<iostream>
using namespace std;
class A
{
private:

int z;


public:
void b(int v)
{z=v;}
friend void print(A);
};

void print(A a)
{
int m=a.z+10;
cout<<m;
}

int main()
{
A s;
s.b(5);
print(s);
return 0;
}

Selection Sort using C

                                                                                                                                                                                         #include<stdio.h>

int main()
{
int ar[20],n,pass,min,t,p,i,s,k;
printf("enter no of element");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}

for(p=1;p<=n-1;p++)
{
pass=p-1;min=p-1;
for(s=p;s<=n-1;s++)
{
if(ar[min]>ar[s])
{
min=s;
}
for(k=0;k<=(n-1);k++)
{
printf("%d",ar[k]);
}
printf("\nmin=%d,\npass=%d",ar[min],ar[pass]);
}
t=ar[min];ar[min]=ar[pass];ar[pass]=t;
for(k=0;k<=(n-1);k++)
{
printf("%d",ar[k]);
}
printf("\nmin=%d,\npass=%d",ar[min],ar[pass]);
}
printf("\n\n");


return 0;
}

Linear Search using C

#include<stdio.h>
int main()
{
int ar[10]={3,5,6,4,3,7,8,2,4,6};
int i,num,flag=0;
printf("Enter an element to search");
scanf("%d",&num);
for(i=0;i<10;i++)
{
if(ar[i]==num)
{
flag=1;
printf("Element found at %d",i);
}
}
if(flag==0)
{
printf("Element not found");
}
return 0;
}

Program for fractional Knapsack in C

#include<stdio.h>
int main()
{
int num,i,weight[20],price[20];
printf("Enter Total no of elements:-");
scanf("%d",&num);
printf("Enter weight and price:");
for(i=0;i<num;i++)
{
scanf("%d%d",&weight[i],&price[i]);
}
int value[20],temp,j;
for(i=0;i<num;i++)
{
value[i]=price[i]/weight[i];
}


for(i=0;i<num;i++)
{
for(j=i+1;j<num;j++)
{
if(value[i]<value[j])
{
temp=value[i];
value[i]=value[j];
value[j]=temp;

temp=weight[i];
weight[i]=weight[j];
weight[j]=temp;

temp=price[i];
price[i]=price[j];
price[j]=temp;
}
}
} //sorting done
for(i=0;i<num;i++)
{
printf("%d\t",weight[i]);
printf("%d\t",price[i]);
printf("%d\n",value[i]);
} //printing
int cap;
int knap[20];
int max=0;
int a=0;
int f=a;
printf("Enter capicity of knapsack:");
scanf("%d",&cap);

   for(i=0;i<num;i++)
   {
if(cap!=900)//for fractional
{
printf("\n");
f=a;
if(weight[i]<=cap)
{
printf("\nWEight:%d",weight[i]);
printf("\tIts value per ml:%d",value[i]);
for(j=a;j<f+weight[i];j++)                      //j is for inserting element in array just after previously inserted
{ //f is working as j.Because j goes incremented.
knap[j]=1;
printf("\n\tknap[%d]:%d",j+1,knap[j]);
cap--;
a++;
}
max=value[i]*weight[i]+max;
printf("\nInserted profit got :%d",price[i]);
weight[i]=0; //for fractional
} //Now for fractional:-
if(weight[i]>cap)
{ temp=cap;
printf("\nWEight:%d",weight[i]);
printf("\tIts value per ml:%d",value[i]);
printf("\nCapacity left in knap=%d",cap);
for(j=a;j<f+temp;j++) //here weight[i] is replaced by cap becoz weight is bigger than cap
{
knap[j]=1;
printf("\n\tknap[%d]:%d",j+1,knap[j]);
//temp because ca is decreased
cap--;
a++;
}
max=value[i]*temp+max;
printf("\nInserted profit got fractionly: %d",value[i]*temp);  //Here this is becoz we have to show all profit by
cap=900; //inserting fractional element
} //900 works as null
}
}

printf("\nMax price got:%d\n",max);
return 0;
}

Program to do Binary Search using C

#include<stdio.h>
int main()
{
int ar[10]={0,1,2,3,4,5,6,7,8,9};
int lo=0;
int mid,item,i;
int up=9;
int flag=0;

printf("\nEnter an item to search:-");
scanf("%d",&item);

for(i=0;i<10;i++)
{
mid=(lo+up)/2;
if(ar[mid]==item)
{

flag=1;
}
else
{
if(ar[mid]<item)
{
lo=mid+1;
}
if(ar[mid]>item)
{
up=mid-1;
}
}
}
if(flag==0)
{
printf("\nitem not found\n\n");
}
else
printf("\nElement found %d\n\n",mid);

return 0;
}