Saturday, 30 May 2020

linked list with all possible operations using class with oops concept

#include<iostream>
//#include<conio.h>
#include<stdlib.h>
using namespace std;
class Node
{
public:
int info;
Node* next;
};
class List:public Node
{
Node *first,*last;
public:
List()
{
first=NULL;
last=NULL;
}
void create();
void insert();
void delet();
void display();
void search();
};
void List::create()
{
Node *temp;
temp=new Node;
int n;
cout<<"\nEnter an Element:";
cin>>n;
temp->info=n;
temp->next=NULL;
if(first==NULL)
{
first=temp;
last=first;
}
else
{
last->next=temp;
last=temp;
}
}
void List::insert()
{
Node *prev,*cur;
prev=NULL;
cur=first;
int count=1,pos,ch,n;
Node *temp=new Node;
cout<<"\nEnter an Element:";
cin>>n;
temp->info=n;
temp->next=NULL;
cout<<"\nINSERT AS\n1:FIRSTNODE\n2:LASTNODE\n3:IN BETWEEN FIRST&LAST NODES";
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
temp->next=first;
first=temp;
break;
case 2:
last->next=temp;
last=temp;
break;
case 3:
cout<<"\nEnter the Position to Insert:";
cin>>pos;
while(count!=pos)
{
prev=cur;
cur=cur->next;
count++;
}
if(count==pos)
{
prev->next=temp;
temp->next=cur;
}
else
cout<<"\nNot Able to Insert";
break;
}
}
void List::delet()
{
Node *prev=NULL,*cur=first;
int count=1,pos,ch;
cout<<"\nDELETE\n1:FIRSTNODE\n2:LASTNODE\n3:IN BETWEEN FIRST&LAST NODES";
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
if(first!=NULL)
{
cout<<"\nDeleted Element is "<<first->info;
first=first->next;
}
else
cout<<"\nNot Able to Delete";
break;
case 2:
while(cur!=last)
{
prev=cur;
cur=cur->next;
}
if(cur==last)
{
cout<<"\nDeleted Element is: "<<cur->info;
prev->next=NULL;
last=prev;
}
else
cout<<"\nNot Able to Delete";
break;
case 3:
cout<<"\nEnter the Position of Deletion:";
cin>>pos;
while(count!=pos)
{
prev=cur;
cur=cur->next;
count++;
}
if(count==pos)
{
    cout<<"\nDeleted Element is: "<<cur->info;
    prev->next=cur->next;
}
else
cout<<"\nNot Able to Delete";
break;
}
}
void List::display()
{
Node *temp=first;
if(temp==NULL)
{
cout<<"\nList is Empty";
}
while(temp!=NULL)
{ cout<<"START-->";
cout<<temp->info;
cout<<"-->";
temp=temp->next;
}
cout<<"NULL";
}
void List::search()
{
int value,pos=0;
int flag=0;
if(first==NULL)
{
cout<<"List is Empty";
return;
}
cout<<"Enter the Value to be Searched:";
cin>>value;
Node *temp;
temp=first;
while(temp!=NULL)
{
pos++;
if(temp->info==value)
{
flag=1;
cout<<"Element"<<value<<"is Found at "<<pos<<" Position";
return;
}
temp=temp->next;
}
if(flag==0)
{
cout<<"Element "<<value<<" not Found in the List";
}
}
int main()
{
List l;
int ch;
while(1)
{
cout<<"\n**** MENU ****";
cout<<"\n1:CREATE\n2:INSERT\n3:DELETE\n4:SEARCH\n5:DISPLAY\n6:EXIT\n";
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
l.create();
break;
case 2:
l.insert();
break;
case 3:
l.delet();
break;
case 4:
l.search();
break;
case 5:
l.display();
break;
case 6:
exit(0);
}
}
// getch();
}

complete linked list all possible operations in one in C++ using structure

//linked list
#include<iostream>
using namespace std;
void first();// for creating 1st node
void create();// for creating a node
void show();// for display nodes
void insert();// for inserting nodes at diff pos

void gpos();
void last();

void del(); //for deleting node
int count=0;// for counting of nodes
struct node
{
int data;
node*next;
};
struct node* start;
struct node* ptr1;

int main()
{
node s;
create();
int choice;
while(1)
{
cout<<"\n\n\n\t\t\t******MENU********";
cout<<"\n 1 insert\n 2 delete \n 3 DISPLAY\n 4 EXIT \n 5 no of nodes\n";
cin>>choice;
switch(choice)
{
case 1:insert();
break;
case 2:del();
break;
case 3:show();
break;
case 4: exit(0)  ;
break;
case 5:cout<<count;
break;
default: cout<<" wrong  choice\n:";
break;
}
}

return 0;    // getch();
}
void create()
{
// it will create 1st node
int value;
start=NULL;
ptr1=new node;

cout<<"enter to insert in 1st node\n";
cin>>value;
ptr1->data=value;
ptr1->next=NULL;
start=ptr1;
cout<<"created successfully\n";
count++;
}
void insert()
{

int pos;
cout<<"\n\nenter the position at which u want to insert\n";
cout<<"1  at first\n";
cout<<"2  at last\n";
cout<<"3  at given position \n";
cin>>pos;
switch(pos)
{
case 1:first();
break;
case 2:last();
break;
case 3:gpos();
break;
default: cout<<"wrong choice";
break;
}
cout<<"node is added successfully!!\n";
count++;
}
void first()
{
node *temp,*ptr3;
       // temp=start;
ptr3=new node;
cout<<"enter data\n";
cin>>ptr3->data;
ptr3->next=start;
start=ptr3;
}


void show()
{
node* temp;
temp=start;
if(temp==NULL)
{
cout<<"empty";
}
       else
{ cout<<"START-->";
while(temp!=NULL)
{
cout<<temp->data<<"-->";
temp=temp->next;
}
cout<<"NULL\n";
}
}

void gpos()
{
node *temp3,*ptr;
ptr=new node;
node *temp4;
temp3=start;
temp4=temp3;
if(temp3==NULL)
{
cout<<"list is empty\n";
}
else
{
int val,ch;
cout<<"enter the pos where element is to be inserted\n";
cin>>ch;
if(ch==1)
{
   first();
}
else
     {
for(int i=1;i<ch;i++)
{
temp4=temp3;
temp3=temp3->next;

}
cout<<"enter the item to inserted\n";
cin>>ptr->data;
ptr->next=temp4->next;
temp4->next=ptr;
}
}
}

void last()
{
node *temp,*temp2,*ptr3;
temp=start;
ptr3=new node;
cout<<"enter data\n";
cin>>ptr3->data;
if(temp==NULL)
{
cout<<"empty";
}
while(temp!=NULL)
{
temp2=temp;
temp=temp->next;
}
ptr3->next=NULL;
temp2->next=ptr3;
}

void del()
{


node *temp3;
node *temp4;
temp3=start;
temp4=temp3;
if(temp3==NULL)
{
cout<<"list is empty\n";
}
else
{
int val,ch;
cout<<"enter the pos of element to delete\n";
cin>>ch;
if(ch==1)
{
val=temp3->data;
   cout<<"deleted item\n"<<val;
   start=start->next;
   count--;
}
else if(ch<=count)
   {
for(int i=1;i<ch;i++)
{
temp4=temp3;
temp3=temp3->next;

}
val=temp3->data;
cout<<" deleted element is"<<val;
temp4->next=temp3->next;
count--;
}
else
{
cout<<"can not delete node because there are only "<<count<<"node\n";
}
}
}

nth number of fibbonacci series

// terms wise
// first you have  to enter test case then the nth term
// eg  2
    // 3 4
#include<iostream>
using namespace std;
int main()
{
long int n,t;
cin>>t;
for(int j=0;j<t;j++)
{
cin>>n;
int a=0,b=1;
unsigned int sum=0;
if(n==1)
cout<<a;
else if(n==2)
cout<<b;
else
{
for(int i=0;i<n-2;i++)
{
sum=a+b;
a=b;
b=sum;
}
cout<<sum<<"\n";
}
}
return 0;
}

file handling: deleting a record in c++

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdlib.h>
#include<stdio.h>
struct emp
{
int emp_id;
int sal;
char name[30];
}e1;
void main()
{       int emp_id;
ifstream fin;
ofstream fout;
fin.open("abc.dat",ios::binary);
if(!fin)
{
cout<<"file not found\n";
getch();
exit(1);
}
fout.open("temp.dat",ios::binary);
if(!fout)
{
cout<<"file not found\n";
getch();
exit(1);
}
cout<<"enter the emp id to delete\n";
cin>>emp_id;
while(!fin.eof())
{
if(!(e1.emp_id==emp_id))
{
fout.write((char*)&e1,sizeof(e1));
}
else
cout<<"yo";

}
fin.close();
remove("abc.dat");
fout.close();
rename("temp.dat","abc.dat");
getch();
}

file handling :: all functions in one

#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct stu
{
int roll;
char name[20];
char city[40];
}s;
void del()
{

int roll;
ofstream fout;
ifstream fin;
fin.open("skl.dat",ios::binary);
fout.open("temp.dat",ios::binary);
if(!fin)
{
cout<<"file not found\n";
getch();
exit(1);
}
cout<<"enter the roll u want to delete\n";
cin>>roll;
while(fin.read((char*)&s,sizeof(s)))
{
if(!(s.roll==roll))
{
       // cout<<" not found\n";
       // disp();
fout.write((char*)&s,sizeof(s));
}
       else
{
cout<<" id found \n";
cout<<"name is \n";
puts(s.name);
}

}
int x;
fin.close();
fout.close();
x=rename("temp.dat","skl.dat");
remove("skl.dat");
if(x==0)
{
cout<<"unable\n";
}

}
void create()
{
char x;
ofstream fout;
fout.open("skl.dat",ios::binary);
if(!fout)
{
cout<<"file not found\n";
getch();
exit(1);
}
do
{
cout<<"enter the name \n";
gets(s.name);
cout<<"enter the roll \n";
cin>>s.roll;
cout<<"enter the city \n";
gets(s.city);
fout.write((char*)&s,sizeof(s));
cout<<"if u want to add more press y \n";
cin>>x;
}while(x=='y');
}
void display()
{
ifstream fin;
fin.open("skl.dat",ios::binary);
if(!fin)
{
cout<<"file not found";
getch();
exit(1);
}
while(fin.read((char*)&s,sizeof(s)))
{
cout<<"The name is   ";
puts(s.name);
cout<<"\n";
cout<<"The roll is   ";
cout<<s.roll<<"\n";
cout<<"The city is   ";
puts(s.city);
cout<<"\n";
}
fin.close();
}
void search()
{
int roll1;
cout<<"enter the roll no which u want to search\n";
cin>>roll1;
ifstream fin;
fin.open("skl.dat",ios::binary);
fin.read((char*)&s,sizeof(s));
while(!fin.eof())
{
if(roll1==s.roll)
{
cout<<"FOUND\n";
cout<<"roll no"<<s.roll<<"\n";
cout<<"name   ";
puts(s.name);
cout<<"city   ";
puts(s.city);
break;
}
else
{
cout<<"not found\n";
break;
}
}
fin.close();
}

void main()
{
clrscr();
int ch;
char ch1='n';
do
{
cout<<"menu\n"    ;
cout<<"1 creat\n";
cout<<"2 display\n";
cout<<"3 searching\n";
cout<<"4  delete\n";
cout<<"5 exit\n";
cout<<"enter your choice\n";
cin>>ch;
switch(ch)
{
case 1: create();
break;
case 2: display();
break;
case 3: search();
break;
case 4: del();
break;
case 5: exit(0);
break;
default:
cout<<"wrong choice\n";
}
}while(ch1=='n');
getch();
}

file handling:writing some data in binary file

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<fstream.h>
struct stu
{
char name[20];
float per;
};
void main()
{
clrscr();
ofstream fout;
stu s1;
char ch;
fout.open("emp.dat",ios::out|ios::binary);
if(!fout)
{
cout<<"not found";
getch();
exit(1);
}
while(ch!='n')
{
cout<<"enter the name\n";
gets(s1.name);

cout<<"enter the percentage\n";
cin>>s1.per;
fout.write((char*)&s1,sizeof(s1));
cout<<"more record\n";
cin>>ch;
}
fout.close();


ifstream fin;
       // stu s1;
       // char ch;
fin.open("emp.dat",ios::in|ios::binary);
if(!fin)
{
cout<<"not found";
getch();
exit(1);
}
       // fflush(stdin);
while(fin.read((char*)&s1,sizeof(s1)))
{
cout<<"name   "<<s1.name<<"\n";
cout<<"percentage    "<<s1.per<<"\n";
}
fin.close();
getch();
}

simple linked list in C

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void create();
void display();
void append();
struct node
{
int data;
struct node* next;
};
struct node* root;
int main()
{
//clrscr();
int ch;
char ch1='y';
while(ch1=='y')
{
printf("MENU\n");
printf("1 create node\n2 display\n3 appending\n enter your choice......");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:display();
break;
case 3:append();
break;
default:printf("invalid");
break;
}
printf("\ndo you want to enter more\n");
scanf(" %c",&ch1);
}
//getch();
}
void create()
{

node *head;
head=(node*)malloc(sizeof(node));
printf("eneter the data");
int a;
scanf("%d",&a);
head->data=a;
head->next=NULL;
root=head;
}
void display()
{
struct node* temp;
temp=root;
if(temp==NULL)
printf("empty");
else
{
printf("START-->");
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->next;
}
printf("NULL");
}
}
void append()
{
struct node* temp;
temp=(node *)malloc(sizeof(node));
printf("enter data to append");
int d;
scanf("%d",&d);
temp->data=d;
temp->next=NULL;
if(root==NULL)
      root=temp;

else
{
struct node *p;
p=root;
while(p!=NULL)
{
p=p->next;
}
p->next=temp;
}
}

Tuesday, 26 May 2020

All in one program every function and algo in one in C

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

// swapping
void swap(int *a,int *b);
void swap2(int a,int b);

// 1 d array
void array();
// functions of 1 d array
int max(int arr[],int x);
int min(int arr[],int x);
void display(int arr[],int x);
void rev(int arr[],int x);
void insert1(int arr[],int n);
void insert2(int a[],int m);
void dupli(int a[],int n);  // out put galat aara hai
// sorting
void sort(int arr1[],int n);
void sort1(int arr[],int n);// selection sort
void sort2(int arr[],int n);//bubble sort
void sort3(int arr[],int n);// insertion sort


// 2 d array
void array2();
// functions of 2 d array
int max2(int arr[10][10],int r,int c);
int min2(int arr[10][10],int r,int c);
void display2(int arr[10][10],int r,int c);

// structure
void structure();
void dispstr(struct st *a,int per[]);// array dikkat kr ra hai


// strings
void string();
int lenstr(char str[]);// find length
void dispstr(char str[]);// display string
void revstr(char str[]);// reverse
void constr();// concatenation
void upperstr(char str[]);//change lower to upper
int copystr(char *p,char *q);//copy to anthor string

//structure
struct st
{
int roll;
char name[100];
int age;
float marks[5];
};

//main function
int main()
{

int ch;char ch1='y';int num1,num2;
clrscr();
label:
while(ch1=='y')
{

printf("\t\t\t\tMENU\n");
printf("1 for swapping by ref\n");
printf("2 for swapping by call by value\n");
printf("3 for 1 D array operations\n");
printf("4 for structure operation\n");
printf("5 for string opererations \n");
printf("6 for exit");
printf("7 for 2 D array\n");
printf("\nenter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("enter two values\n");
scanf("%d%d",&num1,&num2);
printf("before swapping a=%d and b=%d\n",num1,num2);
swap(&num1,&num2);
printf("after swapping a=%d and  b=%d\n",num1,num2);
break;
case 2: printf("enter two values\n");
scanf("%d%d",&num1,&num2);
printf("before swapping a=%d and b=%d\n",num1,num2);
swap2(num1,num2);
break;
case 3: array();
break;
case 4: structure();
break;
case 5: string();
break;
case 6: exit(0);
break;
case 7: array2();
break;
default:printf("\nwrong choice");
break;

}
printf("do you want to enter more in main section\n");
scanf(" %c",&ch1);
}
getch();
return 0;
}

// for arrays only
void array()
{

int arr1[100],n;
char ch1='y';
clrscr();
printf("enter the no of element\n");
scanf("%d",&n);
printf("enter the values for array element\n");
int i;
for(i=0;i<n;i++)
{
scanf("%d",&arr1[i]);
}
while(ch1=='y')
{

printf("menu\n");
printf("1 for max\n");
printf("2 for display\n");
printf("3 for min\n");
printf("4 for reversing an array\n");
printf("5 for main function\n");
printf("6 for exit\n");
printf("7 for inserting a element in a specified location\n");
printf("8 for inserting a array in a specified location\n");
printf("9 for soting the array\n");
printf("10 for checking duplicacy\n");
int ch,a;
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: a=max(arr1,n);
printf("max value in array is %d",a);
break;
case 2: display(arr1,n);
break;
case 3: a=min(arr1,n);
printf("min value in array is %d",a);
break;
case 4: rev(arr1,n);
break;
case 5: main();
break;
case 6: exit(0);
break;
case 7: insert1(arr1,n);
break;
case 8: insert2(arr1,n);
break;
case 9: sort(arr1,n);
break;
case 10:dupli(arr1,n);
break;
default: printf("wrong choice\n");
break;
}
printf("\nif you want to enter choice again in array section please enter y otherwise n\n");
scanf(" %c",&ch1);
}
//if(ch1!='y')
//exit(0);

}




// for array display
void display(int arr[],int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d \t",arr[i]);
}
//getch();
}

// for max item in array
int max(int arr[],int n)
{
int i;
int temp;
temp=arr[0];
for(i=0;i<n;i++)
{
if(temp<arr[i])
temp=arr[i];
}

  return temp;
}

// for min item in array
int min(int arr[],int n)
{
int i;
int temp;
temp=arr[0];
for(i=0;i<n;i++)
{
if(temp>arr[i])
temp=arr[i];
}

  return temp;
}

// for reversing array
void rev(int arr[],int x)
{  //       int no=x;
int i,j;
i=0;
j=x-1;
while(j>i)
{
  swap(&arr[i],&arr[j]);
  i++;
  j--;
}
display(arr,x);

}

// insertion of 1 element
void insert1(int arr[],int n)
{
printf("enter location and value\n");
int loc,val;
scanf("%d%d",&loc,&val);
for(int i=n-1;i>=loc-1;i--)
{
arr[i+1]=arr[i];
}
arr[loc-1]=val;

printf("after insertion\n");
for(i=0;i<=5;i++)
{
printf("%d\n",arr[i]);

}

}

// for inserting a array in anthor array
void insert2(int a[],int m)
{
printf("enter location \n");
int loc;
scanf("%d",&loc);
int b[100];int n;
printf("enter the number of element in 2nd array\n");
scanf("%d",&n);
printf("enter 2 array\n");
for(int i=0;i<n;i++)
{
scanf("%d",&b[i]);
}

printf("you have entered array 2\n");
for(i=0;i<n;i++)
{
printf("%d\n",b[i]);

}
for(i=m-1;i>=loc-1;i--)
{
a[i+n]=a[i];
}
for(int j=0;j<n;j++)
{
a[loc-1]=b[j];
loc++;
}
printf("after insertion\n");
for(i=0;i<=m;i++)
{
printf("%d\n",a[i]);

}


}

// sorting of 1 d array
void sort(int arr1[],int n)
{
char ch1='y';
while(ch1=='y')
{

printf("menu\n");
printf("1 for selection sort\n");
printf("2 for display\n");
printf("3 for bubble sort\n");
printf("4 for reversing an array\n");
printf("5 for main function\n");
printf("6 for for insertion sort\n");
printf("7 for exit\n");
int ch;
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: sort1(arr1,n);
break;
case 2: display(arr1,n);
break;
case 3: sort2(arr1,n);
break;
case 4: rev(arr1,n);
break;
case 5: main();
break;
case 6: sort3(arr1,n);
break;
case 7: exit(0);
break;
default: printf("wrong choice\n");
break;
}
printf("\nif you want to enter choice again in sorting section please enter y otherwise n\n");
scanf(" %c",&ch1);
}

}
// selection sort
void sort1(int arr[],int n)
{
int temp;
for(int i=n-1;i>0;i--) // for travesing in aaray
{
int max_index=0;
for(int j=0;j<i;j++)// for finding max index
{
      if(arr[max_index]<arr[j])
      {
      max_index=j;
      }
}
if(arr[max_index]>arr[i])
{
  temp=arr[max_index];
  arr[max_index]=arr[i];
  arr[i]=temp;
}
  }
display(arr,n);
}
// sorting by bubble sort
void sort2(int arr[],int n)
{
     int temp;
     for(int i=n-1;i>0;i--)
     {
for(int j=0;j<i;j++)
{
   if(arr[j]>arr[j+1])
     {
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
     }
}
     }
display(arr,n);
}
// sorting by insertion
void sort3(int arr[],int n)
{
int temp;
for(int i=1;i<n;i++)
{
int key=arr[i];
int j=i-1;
while(j>=0&&arr[j]>key)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;

}
 display(arr,n);
}

// for finding duplicate element in array
void dupli(int a[],int n)
{
int count=0,num;
for(int i=n-1;i>0;i--)
{
for(int j=i;j>0;j--)
{
   if(a[i]==a[j])
   count++;

}
if(count>1)
num=a[i];
}
if(count==1)
printf("no dublicate element\n");
else
printf("%d has %d number of dublicate element\n",num,count);

}


// for 2 d array only
void array2()
{

int arr1[10][10],r,c;
char ch1='y';
clrscr();
printf("enter the no of rows and colomns\n");
scanf("%d%d",&r,&c);
printf("enter the values for matrix\n");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
scanf("%d",&arr1[i][j]);
}
}

while(ch1=='y')
{

printf("menu\n");
printf("1 for max\n");
printf("2 for display\n");
printf("3 for min\n");
printf("4 for reversing an array\n");
printf("5 for main function\n");
printf("6 for exit\n");
printf(" 7 for sum of all elements\n");
//printf("7 for inserting a element in a specified location\n");
//printf("8 for inserting a array in a specified location\n");
int ch,a;
printf("enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: a=max2(arr1,r,c);
printf("max value in array is %d",a);
break;
case 2: display2(arr1,r,c);
break;
case 3: a=min2(arr1,r,c);
printf("min value in array is %d",a);
break;
case 4:// rev(arr1,r,c);
break;
case 5: main();
break;
case 6: exit(0);
break;
       // case 7: insert1(arr1,n);
   // break;
       // case 8: insert2(arr1,n);
// break;
default: printf("wrong choice\n");
break;
}
printf("\nif you want to enter choice again in array section please enter y otherwise n\n");
scanf(" %c",&ch1);
}
//if(ch1!='y')
//exit(0);

}


// display 2 d array
void display2(int arr[10][10],int r,int c)
{
printf("you  have entered\n");
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
printf("%d\t",arr[i][j]);
}
printf("\n");
}

}

// max in 2 d array
int max2(int arr[10][10],int r,int c)
{
int large=arr[0][0];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
    if(large<arr[i][j])
    large=arr[i][j];
   // if(small>arr[i][j])
   // small=arr[i][j];
}
}
       // printf("large%d\n",large);
       // printf("small%d\n",small);
       return large;
}

// min in 2 d array
int min2(int arr[10][10],int r,int c)
{
       // int large=arr[0][0];
int small=arr[0][0];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
  //  if(large<arr[i][j])
   // large=arr[i][j];
    if(small>arr[i][j])
    small=arr[i][j];
}
}
     // printf("large%d\n",large);
      // printf("small%d\n",small);
      return small;
}

// swapping by reference
void swap(int *a,int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
}

// swapping for call by value
void swap2(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("after swapping a=%d and b=%d\n",a,b);
}


// structure
void structure()
{
int no;int per[5];
int a1,a2,a3,a4,a5;
printf("\nhere you can store information of 5 student and can get per of student\n");
struct st a[5];
printf("\nfirstly enter the info\n");
for(int i=0;i<1;i++)
{

printf("enter info for %d student",i+1);
printf("\nenter the name\n");
scanf(" %s",&a[i].name);
       // gets(a[0].name);

printf("enter age\n");
scanf("%d",&a[i].age);
printf("\nenter roll\n");
scanf("%d",&a[i].roll);
printf("enter the marks for 5 sub out of 100\n");
     printf("marks for phy\n");
     scanf("%d",&a[i].marks[0]);
     printf("%d",a[i].marks[0]);
     a1=a[i].marks[0];
     printf("%d",a1);
     printf("marks for math\n");
     scanf("%d",&a[i].marks[1]);
     a2=a[i].marks[1];
     printf("marks for pc\n");
     scanf("%d",&a[i].marks[2]);
     a3=a[i].marks[2];
     printf("marks for pee\n");
     scanf("%d",&a[i].marks[3]);
     a4=a[i].marks[3];
     printf("marks for pic\n");
     scanf("%d",&a[i].marks[4]);
     a5=a[i].marks[4];

per[i]=(a1+a2+a3+a4+a5);
}
//int sum;
// sum=a1+a2+a3+a4+a5;
//printf("1st element%d",sum);
display(per,5);
dispstr(a,per);


}

void dispstr(struct st *a,int per[])
{
printf("you have entered \n");
for(int k=0;k<1;k++)
{
printf("The name of %d student is %s \n",k+1,a[k].name);
printf("The age is %d\n",a[k].age);
printf("The roll is %d \n",a[k].roll);
display(per,5);
float p;
p=per[k]*0.01;
printf("The percentage of student %d is %f\n\n\n\n",k+1,p);
}
}


// for string input
void string()
{
char str[25];    int len;
printf("enter the string \n");
//gets(str);//ye work nahi kr rha hai
scanf(" %s",str);
//puts(str); // ye kaam kar ra hai
char ch1='y';int ch;
while(ch1=='y')
{
printf("\t\t\t\tMENU");
printf("\n 1 length counting\n");
printf("\n 2 reverse the string\n");
printf("\n 3 for copying the string in anthor variable\n");
printf("\n 4 for concatenation \n");
printf("\n 5 for going to main function \n");
printf("\n 6 for exit");
printf("\n 7 for display");
printf("\n 8 for converting lower case to uppercase");
printf("\nenter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: len=lenstr(str);
printf("length of string is %d\n",len);
break;
case 2: revstr(str);
break;
case 3: char q[200];
copystr(str,q);
printf("%s",q);
break;
case 4: constr();
       // strcat(a,b);
//printf("%s",a);
break;
case 5: main();
break;
case 6: exit(1);
break;
case 7: dispstr(str);
break;
case 8: upperstr(str);
break;
default: printf("wrong choice\n");
break;
}
printf("\ndo want to enter more choice in string section y or n\n");
scanf(" %c",&ch1);
}


}

// length of string
int lenstr(char str[])
{
int len=0;
while(str[len]!='\0')
{
len++;
}
return len;
}

// display string
void dispstr(char str[])
{
puts(str);

}

// string reverse
void revstr(char str[])
{
int len;
len=lenstr(str);//printf("%d",len);
int i=0;
int j=len-1;
while(i>j)// ye loop nahi chal ra hai
{
  swap2(str[i],str[j]);
}
puts(str);
}

// copy a string using pointer
int copystr(char *p,char *q)
{
while(*p!='\0')
{
*q=*p;
p++;
q++;
}
*q='\0';
return 0;
}

//lower to upper
void upperstr(char str[])
{
int i=0;
while(str[i]!='\0')
{
if((str[i]>='a')&&(str[i]<='z'))
{
str[i]=str[i]-32;
}
i++;
}
puts(str);
}

// concatination
void constr()
{
char a[10],b[10];
printf("enter two strings\n");
scanf(" %s%s",a,b);
int l1,l2;
l1=strlen(a);
l2=strlen(b);
for(int i=0;i<=l2;i++)
{
a[l1+i]=b[i];
}
puts(a);
}
/*void addstr(char *p,char *q)
{
int len1=0;
int len2=0;
while(*p!='\0')
{
len1++;
p++;
}
printf("len of 1 is %d\n",len1);
while(*q!='\0')
{
len2++;
q++;
}
printf("len of 2 is %d\n",len2);
for(int i=0;i<len2;i++)
{
printf("%c",*p);
   // *p=q[i];
     // p++;
}
*(p+1)='\0';
printf("%s",p);
}*/

Monday, 25 May 2020

linked list

//linked list
#include<iostream>
using namespace std;
void first();
struct node* start;
struct node* ptr1;

struct node
{
int data;
node*next;
};
void create()
{

int value;
start=NULL;
ptr1=new node;

cout<<"enter to insert in 1st node\n";
cin>>value;
ptr1->data=value;
ptr1->next=NULL;
start=ptr1;

}
void show()
{
node* temp;
temp=start;
if(temp==NULL)
{
cout<<"empty";
}
       else
{
while(temp!=NULL)
{
cout<<temp->data<<"-->";
temp=temp->next;
}
cout<<"NULL";
}
}
/*void second_pos()
{

int val;
node *ptr2;
ptr2=new node;
cout<<"enter theitem to insert";
cin>>val;
     // node *temp;

ptr2->data=val;
ptr2->next=ptr1->next;
ptr1->next=ptr2;
 }*/
void del()
{


node *temp3;
node *temp4;
temp3=start;
temp4=temp3;
if(temp3==NULL)
{
cout<<"list is empty\n";
}
else
{
int val,ch;
cout<<"enter the pos of element to delete\n";
cin>>ch;
if(ch==1)
{
val=temp3->data;
   cout<<"deleted item\n"<<val;
   start=start->next;
}
else
     {
for(int i=1;i<ch;i++)
{
temp4=temp3;
temp3=temp3->next;

}
val=temp3->data;
cout<<" deleted element is"<<val;
temp4->next=temp3->next;
}
}
}
void between()
{
node *temp3,*ptr;
ptr=new node;
node *temp4;
temp3=start;
temp4=temp3;
if(temp3==NULL)
{
cout<<"list is empty\n";
}
else
{
int val,ch;
cout<<"enter the pos where element is to be inserted\n";
cin>>ch;
if(ch==1)
{
    // val=temp3->data;
  // cout<<"deleted item\n"<<val;
   //start=start->next;
   first();
}
else
     {
for(int i=1;i<ch;i++)
{
temp4=temp3;
temp3=temp3->next;

}
cout<<"enter the item to inserted\n";
cin>>ptr->data;
ptr->next=temp4->next;
temp4->next=ptr;
}
}
}
void first()
{
node *temp,*ptr3;
       // temp=start;
ptr3=new node;
cout<<"enter data\n";
cin>>ptr3->data;
ptr3->next=start;
start=ptr3;

}
void last()
{
node *temp,*temp2,*ptr3;
temp=start;
ptr3=new node;
cout<<"enter data\n";
cin>>ptr3->data;
if(temp==NULL)
{
cout<<"empty";
}
while(temp!=NULL)
{
temp2=temp;
temp=temp->next;
}
ptr3->next=temp->next;
temp2->next=ptr3;

}
void insert()
{

int pos;
cout<<"enter the position at which u want to insert\n";
cout<<"1  at first\n";
cout<<"2  at last\n";
cout<<"3  in between \n";
cin>>pos;
switch(pos)
{
case 1:first();
break;
case 2:last();
break;
case 4:between();
break;
default: cout<<"wrong choice";
break;
}
}



int main()
{
node s;
create();
int choice;
while(1)
{
cout<<"\t\t\t******MENU********";
cout<<"\n 1 insert\n 2 delete \n 3 DISPLAY\n 4 EXIT \n";
cin>>choice;
switch(choice)
{
case 1:insert();
break;
case 2:del();
break;
case 3:show();
break;
case 4: exit(0)  ;
break;
default: cout<<" wrong  choice\n:";
break;
}
}

return 0;    // getch();
}

Sunday, 24 May 2020

pure virtual functions in c++

#include<iostream>
using namespace std;

class Base
{
int x;
public:
virtual void fun() = 0;
int getX() { return x; }
};

// This class inherits from Base and implements fun()
class Derived: public Base
{
int y;
public:
void fun() { cout << "fun() called"; }
};

int main(void)
{
Derived d;
d.fun();
return 0;
}

virtual function

// CPP program to illustrate
// concept of Virtual Functions

#include <iostream>
using namespace std;

class base {
public:
virtual void print()
{
cout << "print base class" << endl;
}

void show()
{
cout << "show base class" << endl;
}
};

class derived : public base {
public:
void print()
{
cout << "print derived class" << endl;
}

void show()
{
cout << "show derived class" << endl;
}
};

int main()
{
base* bptr;
derived d;
bptr = &d;

// virtual function, binded at runtime
bptr->print();

// Non-virtual function, binded at compile time
bptr->show();
}

operator overloading

#include<iostream>
using namespace std;
 
class Complex {
private:
    int real, imag;
public:
    Complex(int r = 0, int i =0)  {real = r;   imag = i;}
     
    // This is automatically called when '+' is used with
    // between two Complex objects
    Complex operator + (Complex const &obj) {
         Complex res;
         res.real = real + obj.real;
         res.imag = imag + obj.imag;
         return res;
    }
    void print() { cout << real << " + i" << imag << endl; }
};
 
int main()
{
    Complex c1(10, 5), c2(2, 4);
    Complex c3 = c1 + c2; // An example call to "operator+"
    c3.print();

Saturday, 23 May 2020

class template

#include <iostream>
using namespace std;

template <class T>
 class Test
 {
    T a,b;
    public:
    void get()
    {
        cin>>a>>b;
    }
    T sum();
 };

 template <class T>
 T Test<T>:: sum()
 {
     return a+b;
 }
int main() {
   
    Test  <int>A;
    Test <float>B;
    Test <char>C;
    cout<<"enter two int numbers";
    A.get();
    cout<<A.sum();

cout<<"enter two float numbers";
    B.get();
    cout<<B.sum();
   
    cout<<"enter two char numbers";
    C.get();
    cout<<C.sum();
return 0;
}

Template overloading

#include <iostream>
using namespace std;

template <class t>
t sum(t a,t b)
{
    return a+b;
}
template <class t>
t sum(t a,t b,t c)
{
    return a+b+c;
}
template <class t>
t sum(t a,t b,t c,t d)
{
    return a+b+c+d;
}
int main() {
   
    cout<<sum(10,20)<<"\n";
    cout<<sum(10,20,30)<<"\n";
    cout<<sum(10.2,20.2,9.4)<<"\n";
return 0;
}

Template arrray class

#include <iostream>
using namespace std;

template <typename T>
class Array {
private:
T *ptr;
int size;
public:
Array(T arr[], int s);
void print();
};

template <typename T>
Array<T>::Array(T arr[], int s) {
ptr = new T[s];
size = s;
for(int i = 0; i < size; i++)
ptr[i] = arr[i];
}

template <typename T>
void Array<T>::print() {
for (int i = 0; i < size; i++)
cout<<" "<<*(ptr + i);
cout<<endl;
}

int main() {
int arr[5] = {1, 2, 3, 4, 5};
Array<int> a(arr, 5);
a.print();
return 0;
}

Template function in modern c++

#include<iostream>
using namespace std;

template <typename T>
T Max(T x, T y)
{
   return (x > y)? x: y;
}
template <typename T>
void Swap(T &n1, T &n2)
{
T temp;
temp = n1;
n1 = n2;
n2 = temp;
}


int main()
{
      //using template
       int result= Max<int>(4,9);
cout<<result<<"\n";
cout << Max<double>(3.0, 7.0) << endl; // call myMax for double
       cout << Max<char>('g', 'e') << endl; 
       int a1=10;
       int b1=20;
       cout<<"before a="<<a1<<"and b="<<b1<<endl;
      Swap(a1,b1);
      cout<<"after a="<<a1<<"and b="<<b1<<endl;
}

Friday, 22 May 2020

run time change the color of body

run time change the color of body: Check out what ARPIT JAIN has created on SoloLearn

animation using html css and js

animation using html css and js: Check out what ARPIT JAIN has created on SoloLearn

ranged based for loop in modern c++

#include <iostream>
#include <vector>
int main() {
 
  std::vector<int> int_list;
  int_list.push_back(1);
  int_list.push_back(2);
  int_list.push_back(3);
//   int int_list[4];
 
  for (int& x : int_list) {
    // This version of the loop will modify each item directly, by reference!
    x = 99;
  }
 
  for (int x : int_list) {
    std::cout << "This item has value: " << x << std::endl;
  }
 
  std::cout << "Everything was replaced with 99!" << std::endl;

  return 0;
}