Translate

Friday, 31 August 2012

Flow Charts


Flowchart
Flowchart is the graphical representation of an algorithm. It is used to show all the steps of an algorithm in a sequence pictorially. An algorithm may be converted to a flowchart. The flowchart is then converted into a program written in any programming language.

Advantages of Flowcharts
Flowchart is used for the following reasons:
1.    Flowchart represents an algorithm in graphical symbols.
2.    It shows the steps of an algorithm in an easy way.
3.    It is used to understand the flow of the program.


Flowchart Symbols
Following are the symbols used in Flowchart
1.    Start/End
Oval symbol is used to represent the start or end of the flowchart.
 2.    Input/Output
Parallelogram symbol is used to represent an input or output step.

3.     Process
Rectangle symbol is used to represent a process step. A process may be a calculation or assignment etc.

4.     Selection           
Diamond symbol is used to represent a selection step. A condition is given in the diamond. If condition is true then flow of control will go in one direction. If condition is false then control will go in other direction.

5. Flow Lines    
Arrow symbols are used to represent the direction of flow in the flowchart. There are four types of flow lines.

6. Connector      
Circle symbol is used to combine different flow lines.

Examples:
Example 1
Develop a flowchart to input two numbers from the user, calculate its sum and then display the result.


Example 2
Develop a flowchart to input 5 numbers from the user, calculate its average and then display the result.

Subscribe to lillian
Powered by groups.yahoo.com

  

Questions And Answers In C++

Question : What is run-time error, logical error and syntax error?

Answer : Syntax error - The errors which are traced by the compiler during compilation, due to wrong grammar for the language used in the program, are called syntax errors.
For example, cin<<a; // instead of extraction operator insertion operator is used.
Run time Error - The errors encountered during execution of the program, due to unexpected input or output are called run-time error.
For example - a=n/0; // division by zero

Logical Error - These errors are encountered when the program does not give the desired output, due to wrong logic of the program.
For example : remainder = a+b // instead of using % operator + operator is used.

Question : What is the role of #include directive in C++?

Answer : The preprocessor directive #include tells the complier to insert another file into your source file. In effect, #include directive is replaced by the contents of the file indicated.

Question : What is the role of #define in C++?

Answer : It is a preprocessor directive to define a macro in a C++ program. Macros provide a mechanism for token replacement with or without a set of formal, function line parameters. For example :
#define PIE 3.1416
#define AVG(A,B,C) (A+B+C)/3

Question : What is the difference between while and do-while loop?

Answer : While is an Entry Controlled Loop, the body of the loop may not execute even once if the test expression evaluates to be false the first time, whereas in do..while, the loop is executed at least once whether the condition holds true the first time or not. more details


Question : What is the difference between local variable and global variable?

Answer : Local variables are those variables which are declared within a function or a compound statement and these variables can only be used within that function/scope. They cannot be accessed from outside the function or a scope of it's declaration. This means that we can have variables with the same names in different functions/scope. Local variables are local to the function/scope in which they are declared.
Global variables are those variables which are declared in the beginning of the program. They are not declared within a function. So, these variables can be accessed by any function of the program. So, global variables are global to all the functions of the program.
 


Question : What is pointer?

Answer : Pointer is an address of a memory location. A variable, which holds an address of a memory location, is known as a Pointer variable (or Simply Pointer). For example int *P; more details

Wednesday, 29 August 2012

//Addition of two POLYNOMIAL's

#include<iostream.h>
#include<conio.h>
void main()
{
    int co[10],i,p,k,co2[10],co1[10];
    char po[9];
    clrscr();
    for(i=0;i<10;i++)
    {
        co[i]=0;
    }
    cout<<"\nPlease enter the power of the polynomial\ni.e. the highiest power"<<endl;
    cin>>p;
    cout<<"\nPlease enter the polynomial \nAs:2 x*x+ 3 x+ 7\n"<<endl;
    for(i=0;i<p+1;i++)
    {
        cin>>co[i];
        if(i!=p)
        cin>>po;
    }
    for(i=0;i<p+1;i++)
    {
        cout<<co[i];
        for(k=0;k<p-i;k++)
        {
            cout<<"x";
            if(k!=(p-(i+1)))
            cout<<"*";
        }
        if(i!=p)
        cout<<"+";
    }
    cout<<"\nPlease enter the power of the polynomial\ni.e. the highiest power"<<endl;
    cin>>p;
    cout<<"\nPlease enter the polynomial \nAs:2 x*x+ 3 x+ 7\n"<<endl;
    for(i=0;i<p+1;i++)
    {
        cin>>co2[i];
        if(i!=p)
        cin>>po;
    }
    for(i=0;i<p+1;i++)
    {
        cout<<co2[i];
        for(k=0;k<p-i;k++)
        {
            cout<<"x";
            if(k!=(p-(i+1)))
            cout<<"*";
        }
        if(i!=p)
        cout<<"+";
    }
         cout<<endl;
    for(i=0;i<=p;i++)
    {
        co1[i]=co2[i]+co[i];
    }
    cout<<"\nThe addition of polynomial is:....."<<endl;
     for(i=0;i<p+1;i++)
    {
        cout<<co1[i];
        for(k=0;k<p-i;k++)
        {
            cout<<"x";
            if(k!=(p-(i+1)))
            cout<<"*";
        }
        if(i!=p)
        cout<<"+";
    }
    getch();
}

//Addition of two POLYNOMIAL's

#include<iostream.h>

#include<conio.h>

void main()

{

    int co[10],i,p,k,co2[10],co1[10];

    char po[9];

    clrscr();

    for(i=0;i<10;i++)

    {

        co[i]=0;

    }

    cout<<"\nPlease enter the power of the polynomial\ni.e. the highiest power"<<endl;

    cin>>p;

    cout<<"\nPlease enter the polynomial \nAs:2 x*x+ 3 x+ 7\n"<<endl;

    for(i=0;i<p+1;i++)

    {

        cin>>co[i];

        if(i!=p)

        cin>>po;

    }

    for(i=0;i<p+1;i++)

    {

        cout<<co[i];

        for(k=0;k<p-i;k++)

        {

            cout<<"x";

            if(k!=(p-(i+1)))

            cout<<"*";

        }

        if(i!=p)

        cout<<"+";

    }

    cout<<"\nPlease enter the power of the polynomial\ni.e. the highiest power"<<endl;

    cin>>p;

    cout<<"\nPlease enter the polynomial \nAs:2 x*x+ 3 x+ 7\n"<<endl;

    for(i=0;i<p+1;i++)

    {

        cin>>co2[i];

        if(i!=p)

        cin>>po;

    }

    for(i=0;i<p+1;i++)

    {

        cout<<co2[i];

        for(k=0;k<p-i;k++)

        {

            cout<<"x";

            if(k!=(p-(i+1)))

            cout<<"*";

        }

        if(i!=p)

        cout<<"+";

    }

         cout<<endl;

    for(i=0;i<=p;i++)

    {

        co1[i]=co2[i]+co[i];

    }

    cout<<"\nThe addition of polynomial is:....."<<endl;

     for(i=0;i<p+1;i++)

    {

        cout<<co1[i];

        for(k=0;k<p-i;k++)

        {

            cout<<"x";

            if(k!=(p-(i+1)))

            cout<<"*";

        }

        if(i!=p)

        cout<<"+";

    }

    getch();

}

Saturday, 21 July 2012

Cpp Programs

  http://www.hoven.in/Content/Resources/Images/cpp.png

 

  • C++ Program to print your name 10 times 


#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int i;

for(i=1;i<=10;++i)

cout<<"Your Name"<<"\n";

getch();

}

 

  • C++ Program to print fibonacci series 

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
unsigned long n,first,second,third;
first=0;
second=1;
cout<<"How many numbers?";
cin>>n;
cout<<"Fabonacci series\n"<<first<<" "<<second;

for(int i=2;i<n;++i)
{
third=first+second;
cout<<" "<<third;
first=second;
second=third;
}
getch();

  • C++ Program to Print following series: 1 -4 7 -10..........-40 


#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,a=-1,b;
for(i=1;i<=40;i+=3)
{
     a*=-1;
     b=i;
     b*=a;
     cout<<b<<" ";
}
getch();
}

  • c++ program to find sum of series 1^2+3^2+5^2+......+n^2 

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int n,i;
long sum=0;

cout<<"1^2+3^2+5^2+......+n^2\n\n  Enter Value of n:";
cin>>n;

for(i=1;i<=n;i+=2)
sum+=(i*i);

cout<<"\n Sum of given series is "<<sum;
getch();
}

  • C++ Program to find sum of square of n natural numbers 


#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
unsigned long n,i,sum=0,d;
cout<<"Enter any number";
cin>>n;
for(i=1;i<=n;++i)
{
d=pow(i,i);
sum+=d;
}
++sum;
cout<<endl<<"Sum= "<<sum;
getch();
}

C++ Program to calculate roots of quadratic equation ax^2+bx+c=0 


#include<iostream.h>
#include<conio.h>

#include<math.h>
//to claculate square root


void main()

{

clrscr();
  //to clear the screen
float root1,root2,a,b,c,d;

cout<<"Quadratic Equation is ax^2=bx+c=0";

cout<<"  Enter values of a,b and c:";

cin>>a>>b>>c;



d=(b*b)-(4*a*c);

if(d>0)

{

     cout<<"\nTwo real and distinct roots";
     root1=(-b+sqrt(d))/(2*a);
     root2=(-b-sqrt(d))/(2*a);
     cout<<"\nRoots are "<<root1<<" and "<<root2;
}



else

if(d==0)

{

      cout<<"\nTwo real and equal roots";
      root1=root2=-b/(2*a);
      cout<<"\nRoots are "<<root1<<" and "<<root2;
}

else

   cout<<"\nRoots are COMPLEX and IMAGINARY....!!!";
getch();
//to stop the screen
}




  • C++ Program to print three numbers in descending order 

#include<iostream.h>
#include<conio.h>



void main()

{

clrscr();

int a,b,c,big1,big2,big3;

cout<<"Enter three numbers:";

cin>>a>>b>>c;



 big1=a;
if(b>big1)

big1=b;

else

if(c>big1)

big1=c;



if(big1==a)

{

if(b>c)

{

big2=b;

big3=c;

}

else

{

big2=c;

big3=b;

}

}



else

{

if(big1==b)

if(a>c)

{

big2=a;

big3=c;

}

else

{

big2=c;

big3=a;

}



else

{

if(a>b)

{

big2=a;

big3=b;

}

else

{

big2=b;

big3=a;

}

}

}

cout<<"\n\n\tNumbers in descending order......\n\t\t";

cout<<big1<<" "<<big2<<" "<<big3;

getch();

}

  • C++ program to swap to numbers without using temp variable 

#include<iostream.h>
#include<conio.h>


void main()
{
clrscr();
int a,b;
cout<<"Enter value of a:";
cin>>a;
cout<<"Enter value of b:";
cin>>b;
if(a>b)
{
b=a-b;
a=a-b;
b=a+b;
}
else
if(b>a)
{
a=b-a;
b=b-a;
a=b+a;
}
cout<<"\na="<<a;
cout<<"\nb="<<b;
getch();
}

  • C++ Program to do Addition,subtraction and multiplication of two numbers using function.

#include<iostream.h>
#include<conio.h>

int res;
void main()
{
clrscr();
int sum(int,int);
int sub(int,int);
int mul(int,int);
int a,b,m,su,s;
cout<<"Enter two numbers:";
cin>>a>>b;

s=sum(a,b);
su=sub(a,b);
m=mul(a,b);
cout<<"Sum:"<<s<<"\nSubtraction:"<<su<<"\nMultiplication:"<<m;
getch();
}
sum(int a,int b)
{
res=a+b;
return(res);
}
sub(int a,int b)
{
res=a-b;
return(res);
}
mul(int a,int b)
{
res=a*b;
return(res);
}

C++ program to do linear search in Array 

#include<iostream.h>
#include<conio.h>


void main()
{
 clrscr();
 int a[20],n,x,i,flag=0;

 cout<<"How many Elements?";
 cin>>n;
 cout<<"\nEnter Elements of the Array\n";


 for(i=0;i<n;++i)
  cin>>a[i];

 cout<<"\nEnter Element to search:";
 cin>>x;


 for(i=0;i<n;++i)
 {
  if(a[i]==x)
  {
   flag=1;
   break;
  }
 }

 if(flag)
  cout<<"\nElement is Found at position "<<i+1;
 else
  cout<<"\nElement not found";
 getch();
}

C++ Program to explain Binary search in Array 

#include<conio.h>
#include<iostream.h>


void main()
{
int search(int [],int,int);
clrscr();
int n,i,a[100],e=-3,res;
cout<<"How Many Elements:";
cin>>n;
cout<<"\nEnter Elements of Array in Accending order\n";

for(i=0;i<n;++i)
{
cin>>a[i];
}


cout<<"\nEnter element to search:";
cin>>e;
res=search(a,n,e);
if(res!=0)
cout<<"\nElement is Founded at "<<res+1<<"st position";
else
cout<<"\nElement is not found....!!!";
getch();
}


int search(int a[],int n,int e)
{
int f,l,m;
f=0;
l=n-1;
while(f<=l)
{
m=(f+l)/2;
if(e==a[m])
return(m);
else
if(e>a[m])
f=m+1;
else
l=m-1;
}
return 0;
}

  • C++ Program to Count no. of alphabates,digits and spaces present in a file STORY.TXT 


#include<fstream.h>
#include<conio.h>


void main()
{
clrscr();
ifstream  fin("STORY.TXT");
char ch;
int i,a=0,s=0,d=0;


while(fin)
{
fin.get(ch);
i=ch;
if(i>63&&i<91||i>96&&i<123)
a++;
else
if(ch==' ')
s++;
else
if(i>47&&i<58)
d++;
}
cout<<"No. OF Alphabates:"<<a;
cout<<"\nNo. Of Digits:"<<d;
cout<<"\nNo. Of Spaces:"<<s;
getch();
}

  • C++ Program to Count no. of words in a string 

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char a[20];
int i,count=0;
cout<<"Enter a string:";
gets(a);
for(i=0;a[i]!='\0';++i)
{
if(a[i]==' ')
count++;
}
count++;
cout<<"\nThere are "<<count<<" words in the given string";
getch();
}

  • C++ Program to Check whether a year is Leap year or not





#include<iostream.h>
#include<conio.h>

void main()

{
clrscr();

int year;

cout<<"Enter Year(ex:1900):";

cin>>year;

if(year%100==0)

{

if(year%400==0)

cout<<"\nLeap Year";

}

else

if(year%4==0)

cout<<"\nLeap Year";

else

cout<<"\nNot a Leap Year";

getch();

}

C++ Program to check whether a String is Palindrome or not





#include<iostream.h>
#include<conio.h>

void main()

{
clrscr();

int i,j,len,flag=1;

char a[20];

cout<<"Enter a string:";

cin>>a;

for(len=0;a[len]!='\0';++len);

for(i=0,j=len-1;i<len/2;++i,--j)

{

if(a[j]!=a[i])

flag=0;

}

if(flag==1)

cout<<"\nThe string is Palindrome";

else

cout<<"\nThe string is not Palindrome";

getch();

}

  • C++ Program to reverse a string





#include<iostream.h>
#include<conio.h>
#include<stdio.h>

void main()

{
clrscr();

char a[20],a1[20];

int i,j;

cout<<"Enter any String:"<<"\n";

gets(a);    cout<<"Reverse of the string is: ";

for(i=0;a[i]!='\0';++i);

for(j=i-1;j>=0;--j)

cout<<a[j];

getch();

}

  • C++ Program to print given series:1 2 4 8 16 32 64 128






#include<iostream.h>
#include<conio.h>



void main()

{

clrscr();

int i;


for(i=1;i<=128;i*=2)

cout<<i<<" ";

getch();

}

Saturday, 7 July 2012


IGNOU Practicle Question






C code to print or display lower triangular matrix


#include<stdio.h>
int main(){
  int a[3][3],i,j;
  float determinant=0;
  printf("Enter the 9 elements of matrix: ");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&a[i][j]);
  printf("\nThe matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           printf("%d\t",a[i][j]);
  }
   printf("\nSetting zero in upper triangular matrix\n");
   for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           if(i<=j)
             printf("%d\t",a[i][j]);
           else
             printf("%d\t",0);
  }
   return 0;
}
Enter the 9 elements of matrix: 1
2
3
4
5
6
7
8
9
The matrix is
1       2       3
4       5       6
7       8       9
Setting zero in upper triangular matrix
1       2       3
0       5       6
0       0       9




C code to print or display upper triangular matrix


#include<stdio.h>
int main(){
  int a[3][3],i,j;
  float determinant=0;
  printf("Enter the 9 elements of matrix: ");
  for(i=0;i<3;i++)
      for(j=0;j<3;j++)
           scanf("%d",&a[i][j]);
  printf("\nThe matrix is\n");
  for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           printf("%d\t",a[i][j]);
  }
   printf("\nSetting zero in upper triangular matrix\n");
   for(i=0;i<3;i++){
      printf("\n");
      for(j=0;j<3;j++)
           if(i>=j)
             printf("%d\t",a[i][j]);
           else
             printf("%d\t",0);
  }
   return 0;
}
Sample output:
Enter the 9 elements of matrix: 1
2
3
4
5
6
7
8
9
The matrix is
1       2       3
4       5       6
7       8       9
Setting zero in upper triangular matrix
1       0       0
4       5       0
7       8       9