|
Wednesday, October 2, 2013
Print a Line with Empty Main()
Sunday, September 29, 2013
Quine
Quine
Computing
A quine is a computer program which takes no input and produces a copy of its own source code as its only output.
There are two method doing this
1.Using File Handling
2.Using String
Method 1
#include <stdio.h>
#include <string.h>
main()
{
FILE *fp;
char buff[255];
fp = fopen("input.txt", "r");
if( fp != NULL ){
while ( !feof(fp ) ){
memset(buff, '\0', sizeof( buff) );
fgets(buff, 255, (FILE*)fp);
printf("%s", buff );
}
fclose(fp);
}
}
Method 2
#include <iostream>
#include<string>
using namespace std;
int main()
{ string s="#include <iostream>@"
"#include<string>@"
"using namespace std;@"
"int main()@"
"{ string s=;@"
"for(int i=0;i<s.length();i++)@"
"if(s[i]=='@')@"
"cout<<endl;@"
"else@"
" cout<<s[i];@"
" return 0;@"
"}@";
for(int i=0;i<s.length();i++)
if(s[i]=='@')
cout<<endl;
else
cout<<s[i];
return 0;
}
Thursday, September 26, 2013
Permutation of a given String.
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
string s;
cin>>s;
cout<<"\n Permutations Are\n";
do
{
cout<<s<<"\n";
}while(next_permutation(s.begin(),s.end()));
return 0;
}
Permutation of a Given Values.
Q. Let given values are 1,2,3.
The permutations are
123
132
213
231
312
321
Source Code is:-
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
vector<char> v;
int n;
char num;
cout<<"\n Emter N::";
cin>>n;
cout<<"\n Enter Elements:";
for(int i=0;i<n;i++)
{
cin>>num;
v.push_back(num);
}
cout<<"\n Elements Are::";
for(int i=0;i<v.size();i++)
cout<<" "<<v[i];
cout<<"\n Permutations Are\n";
do
{
for(int i=0;i<v.size();i++)
{ if(i%5==0)
cout<<"\n";
cout<<" "<<v[i];
}
cout<<"\t";
}while(next_permutation(v.begin(),v.end()));
return 0;
}
The permutations are
123
132
213
231
312
321
Source Code is:-
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
vector<char> v;
int n;
char num;
cout<<"\n Emter N::";
cin>>n;
cout<<"\n Enter Elements:";
for(int i=0;i<n;i++)
{
cin>>num;
v.push_back(num);
}
cout<<"\n Elements Are::";
for(int i=0;i<v.size();i++)
cout<<" "<<v[i];
cout<<"\n Permutations Are\n";
do
{
for(int i=0;i<v.size();i++)
{ if(i%5==0)
cout<<"\n";
cout<<" "<<v[i];
}
cout<<"\t";
}while(next_permutation(v.begin(),v.end()));
return 0;
}
Compress a String.
Ques. How to compress string in the given format?
Write a function that takes as input a string such as "aabbccdef" and outputs a2b2c2def" or "a4bd2g4" for "aaaabddgggg".
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{ int c;
string s,temp;
cin>>s;
sort(s.begin(),s.end());
for(int i=0;i<s.length();++i)
{if(s[i]!=s[i+1])
{
c=count(s.begin(),s.end(),s[i]);
cout<<s[i];
if(c>1)
{cout<<c;}
}
}
return 0;
}
Write a function that takes as input a string such as "aabbccdef" and outputs a2b2c2def" or "a4bd2g4" for "aaaabddgggg".
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{ int c;
string s,temp;
cin>>s;
sort(s.begin(),s.end());
for(int i=0;i<s.length();++i)
{if(s[i]!=s[i+1])
{
c=count(s.begin(),s.end(),s[i]);
cout<<s[i];
if(c>1)
{cout<<c;}
}
}
return 0;
}
Sunday, September 22, 2013
Shortest Program to perform operation of Singly Linked List.
#include <iostream>
#include <list>
#include<stdlib.h>
using namespace std;
int main ()
{
std::list<int> l;
std::list<int>::iterator it;
do
{int c,d,n,pos;
cout<<"\n\t\t Single Linked List Operations. \n 1.Insert \t 2.Delete \t 3.Display \t4.Exit.\n\n Enter Your Choice:";cin>>c;
switch(c)
{case 1: cout<<"\n\t Insert Operations\n\t 1.Begining \t 2.End \t 3.At a given position\nEnter your choice:";cin>>d;
switch(d)
{case 1:cout<<"\n Enter Element::";cin>>n;l.push_front(n);break;case 2:cout<<"\n Enter Element::";cin>>n;l.push_back(n);break;
case 3:cout<<"\n Enter Element::";cin>>n;cout<<"\n Enter Position:";cin>>pos;it = l.begin();for(int i=0;i<pos-1;i++)++it;l.insert(it,n);break;
default:cout<<"\n Wrong Choice.!!!";break;
}break;
case 2:if(l.size()==0){cout<<"\nEmpty List.";break;}else{cout<<"\n Enter Position::";cin>>pos;it = l.begin();for(int i=0;i<pos-1;i++)++it;l.erase(it);break;}
case 3:if(l.size()==0){cout<<"\nEmpty List.";break;}else{for (it=l.begin(); it!=l.end(); ++it)std::cout << ' ' << *it<<"\n";break;}
case 4:exit(1);break;
default:cout<<"Wrong Choice!!!!\n";break;}
}while(1);
return 0;
}
Shortest Program to perform Stack Operations Using () Header File.
**This program may not run on Older Versions of GCC. Use 4.8.1 GCC Compiler to run this program.
#include<iostream>
#include<stack>
#include<cstdlib>
using namespace std;
int main()
{
stack<int> s;
int ch,n;
cout<<"\n \t\t STACK OPERATIONS \n\n 1.PUSH \t2.POP\t 3.DISPLAY\t4.EXIT.\n\n Enter Your Choice::";
cin>>ch;
switch(ch)
{
case 1:cout<<"\n Element::";cin>>n;s.emplace(n);main();break;
case 2:s.pop();main();break;
case 3:for(int i=0;i<s.size();i++)
cout<<"\n"<<s.top();
main();break;
case 4:exit(0);
default:cout<<"\n\t\n Wrong Choice !!";
main();break;
}
return 0;
}
Wap to perform Single Linked List Operations.
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
struct node *start;
int ins_beg()
{
struct node *new_node;
new_node=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the element:");
scanf("%i",&new_node->info);
if(start==NULL)
{
new_node->link=NULL;
start=new_node;
}
else
{
new_node->link=start;
start=new_node;
}
printf("\n Element Inserted sucessfully.\n");
main();
}
int ins_end()
{
struct node *new_node,*lp;
new_node=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the element:");
scanf("%i",&new_node->info);
if(start==NULL)
{
new_node->link=NULL;
start=new_node;
}
else
{
lp=start;
while(lp->link!=NULL)
{
lp=lp->link;
}
lp->link=new_node;
new_node->link=NULL;
}
printf("\n Element Inserted sucessfully.\n");
main();
}
int ins_pos()
{
struct node *new_node,*tptr;
int pos,i=0;
printf("\n Enter the position:");
scanf("%i",&pos);
if(pos==1)
{ins_beg();
main();}
else
{
tptr=start;
for(i=1;i<(pos-1)&&tptr!=NULL;i++)
tptr=tptr->link;
if(tptr==NULL)
{printf("\n Invalid Position.");
main();}
else
{ new_node=(struct node*)malloc(sizeof(struct node));
printf("\n Enter the element:");
scanf("%i",&new_node->info);
new_node->link=tptr->link;
tptr->link=new_node;
printf("\n Item Inserted Sucessfully.");
main();
}
}
main();
}
int disp()
{
struct node *t;
if(start==NULL)
{
printf("\n List is empty.");
return;
}
t=start;
printf("\n Elements are:");
while(t!=NULL)
{
printf("\n\n%i",t->info);
t=t->link;
}
main();
}
int del()
{
int pos;
struct node *tptr,*ptr;
ptr=start;
printf("\n Enter the Position::");
scanf("%d",&pos);
if(pos==1)
{
start=start->link;
}
else
{ int i=0;
while(i<pos-1 && ptr!=NULL)
{
tptr=ptr;
ptr=ptr->link;
i++;
}
tptr->link=ptr->link;
}
main();
}
int main()
{
do
{
int c,d;
printf("\n\t\t Single Linked List Operations. \n 1.Insert \t 2.Delete \t 3.Display \t4.Exit.\n\n Enter Your Choice:");
scanf("%d",&c);
switch(c)
{
case 1:printf("\n\t Insert Operations\n\t 1.Begining \t 2.End \t 3.At a given position\nEnter your choice:");
scanf("%d",&d);
switch(d)
{
case 1:ins_beg();break;case 2:ins_end();break;case 3:ins_pos();break;default:printf("\n Wrong Choice.!!!");main();break;
}
case 2:del();break;
case 3:disp();break;
case 4:exit(1);break;
default:printf("Wrong Choice!!!!\n");break;
}
}while(1);
return 0;
}
Thursday, September 19, 2013
Wap to compute the area of a isosceles triangle.
#include<stdio.h>
#include<math.h>
int main() {
int a, b, c;
float area, s;
printf("Enter the sides of a triangle:\n");
scanf("%d%d%d", &a, &b, &c);
if ((a + b > c && a + c > b && b + c > a) && (a > 0 && b > 0 && c > 0)) {
if (a == b || b == c || a == c) {
s = (a + b + c) / 2.0;
area = sqrt((s * (s - a) * (s - b) * (s - c)));
printf("\nArea of an isosceles triangle is: %2f\n", area);
} else {
printf("\nTriangle is not an isosceles ");
}
} else {
printf("\n Triangle formation not possible\n");
}
return 0;
}
WAP to classify the triangle as equilateral, isosceles and scalene.
#include<stdio.h>
#include<math.h>
int main() {
int a, b, c;
float s, area;
printf("Enter the values of the sides of the triangle: \n");
scanf("%d %d %d", &a, &b, &c);
if ((a + b > c && a + c > b && b + c > a) && (a > 0 && b > 0 && c > 0))
{
s = (a + b + c) / 2.0;
area = sqrt((s * (s - a) * (s - b) * (s - c)));
if (a == b && b == c)
{
printf("Equilateral Triangle. \n");
printf("Area of Equilateral Triangle is: %f", area);
}
else if (a == b || b == c || a == c)
{
printf("Isosceles Triangle. \n");
printf("Area of an Isosceles Triangle: %f", area);
}
else
{
printf("Scalene Triangle. \n");
printf("Area of Scalene Triangle: %f", area);
}
}
else {
printf("Triangle formation not possible");
}
return 0;
}
Print Your Name without using semicolon in program in C/C++
C
#include<stdio.h>
int main()
{
if(printf("Your Name"))
}
C++
#include<iostream>
using namespace std;
int main()
{
if(cout<<"Your Name")
return 0;
}
#include<stdio.h>
int main()
{
if(printf("Your Name"))
}
C++
#include<iostream>
using namespace std;
int main()
{
if(cout<<"Your Name")
return 0;
}
Infix to Post fix Conversion
#define SIZE 50 /* Size of Stack */
#include <ctype.h>
char s[SIZE];
int top = -1; /* Global declarations */
push(char elem) { /* Function for PUSH operation */
s[++top] = elem;
}
char pop() { /* Function for POP operation */
return (s[top--]);
}
int pr(char elem) { /* Function for precedence */
switch (elem) {
case '#':
return 0;
case '(':
return 1;
case '+':
case '-':
return 2;
case '*':
case '/':
return 3;
}
}
main() { /* Main Program */
char infx[50], pofx[50], ch, elem;
int i = 0, k = 0;
printf("\n\nRead the Infix Expression ? ");
scanf("%s", infx);
push('#');
while ((ch = infx[i++]) != '\0') {
if (ch == '(')
push(ch);
else if (isalnum(ch))
pofx[k++] = ch;
else if (ch == ')') {
while (s[top] != '(')
pofx[k++] = pop();
elem = pop(); /* Remove ( */
} else { /* Operator */
while (pr(s[top]) >= pr(ch))
pofx[k++] = pop();
push(ch);
}
}
while (s[top] != '#') /* Pop from stack till empty */
pofx[k++] = pop();
pofx[k] = '\0'; /* Make pofx as valid string */
printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);
}
Stack Operations.
#include<stdio.h>
#include<stdlib.h>
#define SIZE 10
int item,i;
int push(int stack[SIZE],int &tos,int item)
{
if(tos==SIZE-1)
{printf("\n Stack Overflow.");
}
tos=tos+1;
stack[tos]=item;
return 0;
}
int pop(int stack[SIZE],int &tos)
{
int no;
if(tos==-1)
printf("\n Stack is empty Cannot delete any Element...\n");
else
{no=stack[tos];
tos=tos-1;}
return 0;
}
int display(int stack[SIZE],int tos)
{ if(tos==-1)
printf("\n Stack Is Empty......\n");
else{
printf("\n Stack is:\n");
for(i=tos;i>=0;i--)
{ printf("\n __\n|");
printf("%d",stack[i]);
printf("|");}}
}
int main()
{
int ch,stack[SIZE],n;
static int tos=-1;
do
{
printf(" \n STACK OPERATIONS \n\t1.FILL STACK(before pushing) 2.PUSH\t 3.POP\t4.DISPLAY\t5.EXIT\n");
scanf("%d",&ch);
switch(ch)
{ case 1:printf("\n Enter the no of elements to be inserted:");
scanf("%d",&n);
printf("\n Enter elements:");
for(i=0;i<n;i++)
{scanf("%d",&stack[i]);tos++;}
break;
case 2:
printf("\n Enter the element to be inserted:");
scanf("%d",&item);
push(stack,tos,item);
break;
case 3:
pop(stack,tos);
break;
case 4:display(stack,tos);
break;
case 5:printf("\n Terminating......");
exit(0);
break;
default:printf("\n Wrong Choice!!!");
break;
}printf("\n Press Any Key to Continue.......\n ");
}while(n!=5);
return 0;
}
Wednesday, September 18, 2013
Program to find the transitive closure of a matrix using Warshall's Algorithm.
#include<stdio.h>
int i,j;
#define V 4
void transitiveClosure(int graph[V][V])
{
int i, j, k;
for (k = 0; k < V; k++)
{
for (i = 0; i < V; i++)
{
for (j = 0; j < V; j++)
{
graph[i][j] = graph[i][j] || (graph[i][k] && graph[k][j]);
}
}
}
printf ("Following matrix is transitive closure of the given graph\n");
for ( i = 0; i < V; i++)
{
for ( j = 0; j < V; j++)
printf ("%d ", graph[i][j]);
printf("\n");
}
}
int main()
{
int graph[V][V] = { {1, 1, 0, 1},
{0, 1, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1}
};
transitiveClosure(graph);
return 0;
}
Program to print the Union And Intersection of a Program.
#include<stdio.h>
void Union(int set1[10],int set2[10],int m,int n);
void Intersection(int set1[10],int set2[10],int m,int n);
int main()
{
int a[10],b[10],m,n,i,j;
int ch;
printf("\nEnter the number of elements in first set:\n");
scanf("%d",&m);
printf("\nEnter the elements:\n");
for(i=0;i<m;i++)
{
scanf("%d",&a[i]);
}
printf("\nElement of First set:\n\n");
for(i=0;i<m;i++)
{
printf("%d\t",a[i]);
}
printf("\nEnter the number of elements in second set:\n");
scanf("%d",&n);
printf("\nEnter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&b[i]);
}
printf("\nElement of second set\n");
for(i=0;i<n;i++)
{
printf("%d\t",b[i]);
}
for(;;)
{
printf("\n\nMenu\n\n1.Union\n2.Intersection");
printf("\n3.exit");
printf("\nEnter your choice:\n");
scanf("%d",&ch);
switch(ch) {
case 1:
Union(a,b,m,n);
break;
case 2:
Intersection(a,b,m,n);
break;
case 3:
exit(0);
}
getch();
}
}
void Union(int a[10],int b[10],int m,int n)
{
int c[20],i,j,k=0,flag=0;
for(i=0;i<m;i++)
{
c[k]=a[i];
k++;
}
for(i=0;i<n;i++)
{
flag=0;
for(j=0;j<m;j++)
{
if(b[i]==c[j])
{
flag=1;
break;
}
}
if(flag==0)
{
c[k]=b[i];
k++;
}
}
printf("\nElement of resultant set\n\n");
for(i=0;i<k;i++)
{
printf("\t%d",c[i]);
}
}
void Intersection(int a[10],int b[10],int m,int n)
{
int c[20],i,j,k=0,flag=0;
for(i=0;i<m;i++)
{
flag=0;
for(j=0;j<n;j++)
{
if(a[i]==b[j])
{
c[k]=a[i];
k++;
}
if(k==0)
{
printf("\n\nResultant set is null set!\n");
}else{
printf("\nElement of resultant set\n");
for(i=0;i<k;i++)
{
printf("\t%d",c[i]);
}
Count Number of Digits in N! factorial.
#include<stdio.h>
#include<math.h>
double count_digit(long a)
{
double sum; long i;
if(a==0) return 1;
else
{
sum=0;
for(i=1;i<=a;i++)
sum+=log10(i);
return floor(sum)+1;
}
}
int main()
{
double sum; long n;
clrscr();
while(scanf("%ld",&n)==1)
{
sum = count_digit(n);
printf("%.0lf\n",sum);
}
getch();
}
Printing all possible subsets using Bit Mask
#include<stdio.h>
void PrintAllSubset(int n)
{
int mask,pos,i,j,no = 1<<n;
for(i=1;i<no;i++)
{
for(j=0;j<n;j++)
if((i&(1<<j)) > 0)
printf("%d ",j+1);
printf("\n");
}
}
int main()
{
int n;
while(scanf("%d",&n) == 1)
{
PrintAllSubset(n);
}
return 0;
}
Write a shortest program in C++ to sort a given array.
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int n;
cin>>n;
int arr[n];
std::cout<<"\n Enter n elements in any order:";
for(int i=0;i<n;i++)
std::cin>>arr[i];
std::sort(arr,arr+n);
for(int i=0;i<n;i++)
std::cout<<arr[i]<<" ";
return 0;
}
Writing a Simple Program in C++ 4.3.2.
In Turbo C++ , we write a simple program as follows:-
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a;
cin>>a;
cout<<a;
getch();
}
But in latest C++ 4.3.2 Edition ,program is written as :-
#include<iostream>
using namespace std;
int main()
{
int a;
std::cin>>a;
std::cout<<a;
return 0;
}
#include<iostream.h>
#include<conio.h>
void main()
{clrscr();
int a;
cin>>a;
cout<<a;
getch();
}
But in latest C++ 4.3.2 Edition ,program is written as :-
#include<iostream>
using namespace std;
int main()
{
int a;
std::cin>>a;
std::cout<<a;
return 0;
}
- It is necessary to add using namespace std; in the beginning of the program.
- (.h) is not written after a header file name.
- <conio.h>,<graphics.h> etc. are omitted header files in C++.
- A main function should return a value.
Or simply program could be written as:-
#include<iostream>
using namespace std;
int main()
{
int a;
cin>>a;
cout<<a;
return 0;
}
using namespace std;
int main()
{
int a;
cin>>a;
cout<<a;
return 0;
}
Input by Fastest Method
Header File:-#include<cstdio> (For c++)
#include<wchar.h> (For C)
Input for Character Variables :-
Use getchar_unlocked() for fastest input.
Eg.
char ch;
ch=getchar_unlocked();
Input for Integer Variables :-
Define a function that will take the input as:-
#define get getchar_unlocked
inline int fastinput( )
{
int n = 0 , s = 1 ; char p = get( ) ;
if( p == '-' ) s=-1;
while( ( p < '0' || p > '9' ) && p != EOF && p != '-' )
p = get( ) ;
if( p == '-' )
s = -1 , p = get( ) ;
while( p >= '0' && p <= '9' )
{
n = ( n << 3 ) + ( n << 1 ) + ( p - '0' ) ;
p = get( ) ;
}
return n * s ;
}
Subscribe to:
Posts (Atom)