WHAT'S NEW?
Loading...
Using class for finding the biggest number in an array

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

class array
{
int a[100],size;
public:
void big();
void enter();
};

void array::enter()
{
cout<<"Enter the size of the array";
cin>>size;
cout<<"\nEnter the element";
for(int i=0;i<size;++i)
{
cin>>a[i];
}
}
void array::big()
{
int big;
big=a[0];
for(int i=0;i<size;++i)
{
if(a[i]<=a[i+1])
big=a[i+1];
}
cout<<"\nBiggest in the array";
cout<<big;
}

void main()
{
clrscr();
array obj;
obj.enter();
obj.big();
}







 //code for pascal's triangle

#include<conio.h>
#include<iostream.h>
int main()
{
    clrscr();
    int n,i,j,c,k,place;
    cout<<"Enter the no of rows "<<endl;
    cin>>n;
    cout<<" \n\nPASCAL TRIANGLE"<<endl;
    cout<<"\n\n";
    place=n;
    for(i=0;i<n;i++)
    {
        c=1;
        for(k=place;k>=0;k--)
            cout<<" ";
        place--;
        for(j=0;j<=i;j++)
        {
            cout<<c<<" ";
            c=(c*(i-j)/(j+1));
        }
        cout<<"\n";
    }
getch();
return 0;
}

#include<iostream.h>

struct complex
{
    int x, y;
};

void main()
{
    complex c1,c2,sum;
    complex add (complex, complex);      //function prototype with returning value and arugment as structure
    cout<<"\n Enter the real part of the first complex number";
    cin>>c1.x;
    cout<<"\n Enter the imaginary part of the first complex number";
    cin>>c1.y;
    cout<<"\n Enter the real part of the second complex number";
    cin>>c2.x;
    cout<<"\n Enter the imaginary part of the second complex number";
    cin>>c2.y;
    sum=add(c1, c2);              //function calling
    cout<<"\n The sum of the complex number  is : ";
    if(sum.y>0||sum.y==0)
        cout<<sum.x<<"+"<<sum.y<<"i";
    else
        cout<<sum.x<<sum.y<<"i";
}

complex add(complex c1, complex c2)
{
    complex sum1;
    sum1.x=0;
    sum1.y=0;
    sum1.x=c1.x+c2.x;
    sum1.y=c1.y+c2.y;
    return(sum1);
}


this code is for deleting the number which are in the vector more than once .

#include<iostream.h>

void main()
{
    int i, j, k, num, ans=0;
    float vec[50];
   
    cout<<"\n Enter the size of the vector";
    cin>>num;
    cout<<"\n Enter the elements in the vector";
    for(i=0;i<num;++i)
    {
        cin>>vec[i];
    }
    cout<<"\n Original vector";
    for(i=0;i<num;++i)
        cout<<vec[i]<<" ";
    for(i=0;i<num-1;++i)
    {
        for(j=0;j<num;++j)
        {
            if(vec[i]==vec[j])
            {
                num=num-1;
                for(k=j;k<num;++k)
                    vec[k]=vec[k+1];
                ans=1;
                j=j-1
            }
        }
    }
    if(ans==0)
        cout<<"\n Vector is without duplicates"
    else
    {
        cout<<"\n Vector after deleting duplicates :";
        for(i=0;i<num;++i)
            cout<<vec[i]<<" ";
    }
}      







//password
#include <iostream>
#include<string.h>
#include<conio.h>
#include<stdio.h>

using namespace std;

struct password
{
    char name[100], passwords[100], userid[100];

}real, login;
int main()
{
    int i, id=1, pas=1, conf, len=0, opt;
    char confirm[100] ,a[100];

    menu:
    cout<<"\t\t\n menu"
        <<"\n\n 1. press 1 for sign in"
        <<"\n 2. press 2 for login"
        <<"\n 3. press 3 for exit";


    cout<<"\n\n enter your choice";
    cin>>opt;
    gets(a);

    switch(opt)
    {
    case 1:
        cout<<"\n enter your name :";
        gets(real.name);
        cout<<"\n enter your userid :";
        gets(real.userid);
        retype:
        cout<<"\n enter your password :";
        for(i=0;;++i)
        {
            real.passwords[i]=getch();
            cout<<"*";
            if(real.passwords[i]==13)
            break;
        }
        real.passwords[i]='\0';
        cout<<"\n retype your password to confirm it :";
        for(i=0;;++i)
        {
            confirm[i]=getch();
            cout<<"*";
            if(confirm[i]==13)
            break;
        }
        confirm[i]='\0';
        conf=strcmp(real.passwords,confirm);
            if(conf==0)
            {
                cout<<"\npassword confirmed";
            }
            else
            {
                cout<<"\nretype your password";
                goto retype;
            }
            break;

    case 2:
        log:
        cout<<"\n\n login";
        cout<<"\nenter your userid";
        gets(login.userid);
        cout<<"\nenter your password";
        for(i=0;;++i)
        {
            login.passwords[i]=getch();
            cout<<"*";
        if(login.passwords[i]==13)
        break;
        }

        login.passwords[i]='\0';

        id=strcmp(real.userid,login.userid);
        if(id==0)
        {
            pas=strcmp(real.passwords,login.passwords);
            if(pas==0)
            {
                cout<<"\t\t\n welcome";
                goto exit;
            }
            else
            {
                cout<<"your password is not correct";
                goto log;
            }
        }
        else
        {
            cout<<"your userid is incorrect";
            goto log;
        }
        break;
    case 3:
        goto exit;

    }
    goto menu;
    exit:
    return 0;
}











Bubble sorting is nothing but sorting n numbers in ascending order or descending order

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

void main()
{
      clrscr();
      int a[100], i, j, n, temp;
      cout<<"Enter the size of the array";
      cin>>n;
      cout<<"Enter the elements in the array ";
      for(i=0;i<n;++i)
            cin>>a[i];
      for(i=0;i<n;++i)
      {
            for(j=0;j<(n-1);++j)
            {
                 if(a[j]>a[j+1])
                 {
                     temp=a[j];
                     a[j]=a[j+1];
                     a[j+1]=temp;
                 } 
           } 
    }
    cout<<"\n Array after sorting in ascending order  ";
    for(i=0;i<n;++i)
          cout<<a[i]<<"  ";
}





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

struct students
{
    int rollno;
    char name[30];
    char clas[30];
    int maths;
    int cs;
    int phy;
    int eng;
    int chem;
}s[20];

int main()
{
    int i, num, rollnum, ch, mark[30], grad[30];
    char a[100];

    start:

    cout<<"\t PROGRESS ANALYSIS";
    cout<<"\n\n 1. Insert Marks";
    cout<<"\n 2. Display Marks";
    cout<<"\n 3. Exit";

    cout<<"\n\n press 1,2,3 as per your need";
    cin>>ch;

    switch(ch)
    {
        case 1 :
        {
            cout<<"\n\n How many students mark you need to enter";
            cin>>num;
            for(i=0;i<num;++i)
            {
                cout<<"\n\nEnter the details of the "<<i+1<<" student";
                cout<<"\n\nEnter the rollno. of the student";
                cin>>s[i].rollno;
                cout<<"\n\nEnter the name of the student";
                gets(a);
                gets(s[i].name);
                cout<<"\n\nEnter the class of the student";
                gets(s[i].clas);
                cout<<"\n\nEnter the mark of English of the student";
                cin>>s[i].eng;
                cout<<"\n\nEnter the mark of Maths of the student";
                cin>>s[i].maths;
                cout<<"\n\nEnter the mark of Physics of the student";
                cin>>s[i].phy;
                cout<<"\n\nEnter the mark of Computer of the student";
                cin>>s[i].cs;
                cout<<"\n\nEnter the mark of Chemistry of the student";
                cin>>s[i].chem;
                mark[i]=s[i].eng+s[i].maths+s[i].phy+s[i].cs+s[i].chem;
                grad[i]=mark[i]/5;
            }
            goto start;
            break;
        }
        case 2 :
            cout<<"\n\n Enter the rollno. of student to see his detail";
            cin>>rollnum;
            for(i=0;i<num;++i)
            {
                if(s[i].rollno==rollnum)
                {
                    cout<<"\n\n the name of the student";
                    puts(s[i].name);
                    cout<<"\n\nthe class of the student";
                    puts(s[i].clas);
                    cout<<"\n\nthe mark in English of the student"
                        <<s[i].eng;
                    cout<<"\n\nthe mark in Maths of the student"
                        <<s[i].maths;
                    cout<<"\n\nthe mark in Physics of the student"
                        <<s[i].phy;
                    cout<<"\n\nthe mark in Computer of the student"
                        <<s[i].cs;
                    cout<<"\n\nthe mark in Chemistry of the student"
                        <<s[i].chem;
                    cout<<"\n\n\n Total mark"
                        <<mark[i];
                    cout<<"\n\n Grade of the student";

                    if(grad[i]>90)
                        cout<<"A1";
                    else if(grad[i]>80 && grad[i]<=90)
                        cout<<"A2";
                    else if(grad[i]>70 && grad[i]<=80)
                        cout<<"B1";
                    else if(grad[i]>60 && grad[i]<=70)
                        cout<<"B2";
                    else if(grad[i]>50 && grad[i]<=60)
                        cout<<"C1";
                    else if(grad[i]>40 && grad[i]<=50)
                        cout<<"C2";
                    else if(grad[i]>30 && grad[i]<=40)
                        cout<<"D1";
                    else if(grad[i]<=30 )
                        cout<<"fail";

                }
                else
                    cout<<"the details of the student is not insterted";
            }
            break;
            case 3 :
                exit(1);

    }

    return 0;
}





//With this example let's see how to make and use a structure


#include<iostream.h>

struct date
{
  int day,  month,  year;
}t1, t2;  //t1=>today's date t2=>tomorrow's date

void main()
{
    int i, days[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

cout<<"Enter today's date ";
cin>>t1.day>>t1.month>>t1.year;

for(i=0;i<12;++i)
{
    if(i+1==t1.month)
    {
       if(t1.day<days[i])
       {
           t2.day=t1 .day+1;
           t2.month=t1.month;
           t2.year=t1.year;
       }
        else if(t1 .day==days[i])
       {
            t2.month=t1.month+1;
            t2.day=1;
            if(t1.month==12)
           {
                t2.year=t1.year+1;
           }
           else
                t2.year=t1.year;
       }
   }
}

cout<<"\n\n tomorrow's date: "<<t2.day<<t2.month<<t2 .year;
}






// tic-tac-toe


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


   int  continuee(void);


void main()
{
   char xox[10][10], choice;
   int  m[10][10], i, j, count, boxnum, a=0,flag1=0;

   int  print(char[10][10], int[10][10], char, int);          //function for print x \o on a box
   char check(char[10][10], char);      //function to check if anyone among the two players have won the game

   do
   {
count=0;
clrscr();
cout<<"\t\t\t TIC TAC TOE"
<<"\n\n Instruction"
<<"\n\n To enter x/o enter the box number to print x/o in that box"
<<"\n The box numbering is done as shown below"
<<endl;

for (i=0;i<3;++i)
{
for (j=0;j<3;++j)
{
m[i][j]=++count;   //giving a number to the boxes
cout<<"|"<<m[i][j]<<"|";
}
cout<<endl;
}

for (i=0;i<3;++i)
{
for(j=0;j<3;++j)
xox[i][j]=' ';
}

count=1;
cout<<"\n\n Frist decided among yourself who will play first as x"<<endl;

while (count!=10)
{
if(count%2!=0)
{
do
{

cout<<"First player enter the box number :";
cin>>boxnum;
choice='x';
flag1=print (xox, m, choice, boxnum);
}while(flag1==2);
}
++count;
a=check(xox, choice);
if(count==10)
continue;

if(a==0)
goto newgame;

if(count%2==0)
{
do
{
cout<<"Second player enter the box number";
cin>>boxnum;
choice='o';
flag1=print(xox, m, choice, boxnum);
}while(flag1==2);
}
++count;
a=check(xox, choice);
if(a==0)
goto newgame;

}
if(count==10)
{
cout<<"\n\n the game is a draw";
a=continuee();
}
newgame:
   }while(a==0);
}

int print(char xox[10][10], int m[10][10], char choice, int boxnum)
{
   int i, j;

    for (i=0;i<3;++i)
   {
       for (j=0;j<3;++j)
       {
if(m[i][j]==boxnum)
{
if(xox[i][j]=='x'||xox[i][j]=='o')
{
cout<<"\n\n the box is already in use"<<endl<<" try again"<<endl;
return 2;
}
}
}
   }

   for (i=0;i<3;++i)
   {
       for (j=0;j<3;++j)
       {
if(m[i][j]==boxnum)
{
xox[i][j]=choice;
}

       }
   }

   for (i=0;i<3;++i)
   {
       for (j=0;j<3;++j)
  cout<<"|"<<xox[i][j]<<"|";
       cout<<endl;
   }
   return 0;
}

char check(char m[10][10], char choice)
{
   int i, j, len;
   char flag='a';

   for (i=0;i<3;++i)
   {

if (m[0][i]==choice&&m[1][i]==choice&&m[2][i]==choice)
flag=choice;
if (m[i][0]==choice&&m[i][1]==choice&&m[i][2]==choice)
      flag=choice;
   }
   if (m[0][0]==choice&&m[1][1]==choice&&m[2][2]==choice)
flag=choice;
   if (m[2][0]==choice&&m[1][1]==choice&&m[0][2]==choice)
flag=choice;

if (flag=='x')
{
cout<<"\n\first player as won the game";
continuee();
return(0);
}
if (flag=='o')
{
cout<<"\n\second player as won the game";
continuee();
return(0);
}

   return(1);
}

int continuee(void)
{
char choice;
cout<<"\n\n Enter (y/n) to continue or not to continue the game : ";
cin>>choice;
if(choice=='y')
return 0;
if(choice=='n')
exit(0);
return 0;
}


//  operations with 2-D matrix


#include<iostream.h>

#include<conio.h>

#include<stdlib.h>


int main()

{

    int matx1[10][10], matx2[10][10], matx3[10][10], i, j, k, row1,row2,

        col1, col2, choice, opt1, num;

    char opt2, opt3;


    do

    {

        clrscr();


                cout<<"\t\t Operations on matrix"

                        <<"\n\n Menu"

                        <<"\n 1. Sum "

                        <<"\n 2. Difference "

                        <<"\n 3. product "

                        <<"\n 4. transvers"

                        <<"\n 5. row sum & column sum"

                        <<"\n 6. sum of elements above and below the diagonal"

                        <<"\n 7. Exit";




    int sum_row_col(int [10][10], int, int, char);

    int sum_dia(int [10][10], int );


    cout<<"\n Enter your choice";

    cin>>choice;


    cout<<"\n Enter the number of rows and column in first matrix";

    cin>>row1>>col1;

    cout<<"\n Enter the  element in the first matrix";

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

    {

        for (j=0;j<col1;++j)

            cin>>matx1[i][j];

    }


    cout<<"\n Enter the number of rows and column in second matrix";

    cin>>row2>>col2;

    cout<<"\n Enter the element in the second matrix";

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

    {

        for (j=0;j<col2;++j)

            cin>>matx2[i][j];

    }



    switch(choice)

    {

        case 1:

                if(row1==row2&&col1==col2)

                {


            cout<<"\nsum of the two matrix is :";

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

            {

                for (j=0;j<col1;++j)

                {

                    matx3[i][j]=matx1[i][j]+matx2[i][j];

                    cout<<matx3[i][j]<<"  ";

                }

                cout<<endl;

            }

            }

            else

                {

                        cout<<"the matrix is not computable for addition";

                }

            break;



        case 2:

                if(row1==row2&&col1==col2)

                {

            cout<<"\ndifference of the two matrix";

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

            {

                for (j=0;j<col1;++j)

                {

                    matx3[i][j]=matx1[i][j]-matx2[i][j];

                    cout<<matx3[i][j]<<"  ";

                }

                cout<<endl;

            }

            }

               else

                {

                        cout<<"the matrix is not computable for subtraction";

                }

            break ;



        case 3:

        if(col1==row2)

        {

            cout<<"\n product of two matrix is"<<endl;

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

            {

                for (j=0;j<col2;++j)

                {

                    matx3[i][j]=0;

                    for (k=0;k<col1;++k)

                    {

                        matx3[i][j]=matx3[i][j]+matx1[i][k]*matx2[k][j];

                    }


                    cout<<matx3[i][j]<<"  ";

                }

                    cout<<endl;

            }

        }

        else

        {

             cout<<"the martix is not compatible for multiplication"<<endl;

        }


            break ;



        case 4:

            cout<<"\n enter the matrix number whose transvers you need";

            cin>>opt1;

            if (opt1==1)

            {

                cout<<"\n transvers of the first matrix is ";

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

                {

                    for (j=0;j<col1;++j)

                    {

                        matx3[i][j]=matx1[j][i];

                        cout<<matx3[i][j]<<"  ";

                    }

                    cout<<endl;

                }

            }

            if (opt1==2)

            {

                cout<<"\n transvers of the second matrix is";

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

                {

                    for (j=0;j<col2;++j)

                    {

                        matx3[i][j]=matx2[j][i];

                        cout<<matx3[i][j]<<"  ";

                    }

                }

            }

            break;



        case 5:

            cout<<"\n enter the matrix number whose row sum or column sum you need";

            cin>>opt1;

            cout<<"\n Enter r/c to find the sum of row or column ";

            cin>>opt2;

            cout<<"\n Enter the row or column number whose sum you need";

            cin>>num;


            if (opt1==1)

            {

                if (opt2=='r')

                {

                    sum_row_col(matx1, row1, num, opt2);

                }

                if (opt2=='c')

                {

                    sum_row_col(matx1, col1, num, opt2);

                }

            }

            if (opt2==2)

            {

                if (opt2=='r')

                {

                    sum_row_col(matx2, row2, num, opt2);

                }

                if (opt2=='c')

                {

                    sum_row_col(matx2, col2, num, opt2);

                }

            }

            break;


        case 6:

            cout<<"\n Enter 1/2 to find the sum of element above and below the diagonal of matrix 1/2";

            cin>>opt1;

            if (opt1==1)

            {

                cout<<"\n For first matrix";

                sum_dia(matx1, row1);

            }

            else if(opt1==2)

            {

                cout<<"\n For second matrix";

                sum_dia(matx2, row2);

            }

            else

                cout<<"please enter 1 or 2 as per your use";

            break;



        case 7:

            exit(0);

            break;

    }

        cout<<"\n\n Enter (y/n) if you want to continue the program or not :";

        cin>>opt3;



    }while (opt3=='y');

    return 0;

}


int sum_dia(int a[10][10], int row)

{

    int i, j, asum=0, bsum=0;


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

    {

        for (j=0;j<row;++j)

        {

            if ((i+j)==(row-2))

                asum=asum+a[i][j];

            if ((i+j)==row)

                bsum=bsum+a[i][j];

         }

    }

    cout<<"\n Sum of element above the diagonal is "<<asum;

    cout<<"\n Sum of element below the diagonal is "<<bsum;

    return 0;

}


int sum_row_col(int a[10][10], int row, int num, char opt2)

{

    int i, j, rsum=0, csum=0;

    num=num-1;


    if (opt2=='r')

    {

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

        {

            rsum=rsum+a[num][i];

        }

    cout<<"\n sum of element in the "<<num+1<<" row is :"<<rsum;

    }


    if (opt2=='c')

    {

        for (j=0;j<row;++j)

        {

            csum=csum+a[j][num];

        }

    cout<<"\n sum of element in the "<<num+1<<" column is :"<<csum;

    }

    return 0;

}