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

Program to do bubble sort using C

#include<stdio.h>
int main()
{
int a[10]={1,4,3,7,8,3,9,4,9,5};
int i,j,t;





for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
for(i=0;i<10;i++)
{

printf("%d",a[i]);
}
return 0;
}

Program to find minimum Number of coin in C

#include<iostream>
using namespace std;
int main()
{
int x,n,no=0,i,a[9]={1000,500,100,50,20,10,5,2,1};
cout<<"Enter amount:";
cin>>n;
for(i=0;i<=8;i++)
{
x=n/a[i];
no=no+x;
n=n%a[i];
}
cout<<"Number of coins required="<<no;
}

Program for Job scheduling using C

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

int n,i;
int task[100],start[100],end[100],j,t,g;
printf("Enter no task");
scanf("%d",&n);
int a=1;
for(i=0;i<n;i++)
{
task[i]=a;

printf("Enter start time for t%d\n",a);
scanf("%d",&start[i]);
printf("Enter End time for t%d\n",a);
scanf("%d",&end[i]);
a++;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(start[i]>start[j])
{
t=start[i];
start[i]=start[j];
start[j]=t;

t=end[i];
end[i]=end[j];
end[j]=t;

t=task[i];
task[i]=task[j];
task[j]=t;
}
}
}


//printing sorting
printf("Sorted start time\n");
for(i=0;i<n;i++)
{ printf("%d",task[i]);
printf("\t%d",start[i]);
printf("\t%d\n",end[i]);
}
printf("\n");

//calculating machine
int z;
int m[100];int f=0;
for(i=0;i<n;i++)
{
z=0;g=0;
if(i==0) //creating 1st machine
{
m[f]=end[i];

printf("Task %d is submitted to machine m%d\n",task[i],f);
}
else
{ //comparing machine end time with start
for(j=0;j<=f;j++)
{
if(m[j]<=start[i]&&g==0)
{
m[j]=end[i];
printf("Task %d is submitted to machine m%d\n",task[i],j);
z=1;g=1;
}
} //loop for creating machine (not 1st machine)
if(z==0)
{
f++;
m[f]=end[i];
printf("Task %d is submitted to machine m%d\n",task[i],f);
}


}
}
return 0;
}

Tuesday, March 8, 2016

Program to show Multiple inheritance in C++

#include<iostream>
using namespace std;
class A
{
public:
void showdata()
{
cout<<"class A";
}
};
class B
{
public:
void showdata1()
{
cout<<"Class B";
}
};
class C:public A,public B
{
public:
void showdata2()
{
cout<<"Class c";
}
};
int main()
{
C obj;
obj.showdata();
obj.showdata1();
obj.showdata2();
return 0;
}

Program to show Multilevel Inheritance

#include<iostream>
using namespace std;
class A
{
public:
void showdata()
{
cout<<"Class a";
}
};
class B:public A
{
public:
void showdata1()
{
cout<<"Class B";
}
};
class C:public B
{
public:
void showdata2()
{
cout<<"Class C";
}
};
int main()
{
C obj;
obj.showdata();
obj.showdata1();
obj.showdata2();
return 0;
}

Program to show Single level inheritance in C++

#include<iostream>
using namespace std;
class A
{
public:
void showdata()
{
cout<<"\nBase Class A";
}
};
class B:public A
{
public:
void showdata1()
{
cout<<"\nDrived Class B";
}
};
int main()
{
B obj;
obj.showdata();
obj.showdata1();
return 0;
}