Sunday, December 5, 2010

Sorting the Strings

//THIS PROGRAM WILL SORT A LIST OF STRINGS IN ASCENDING ORDER

#include <iostream.h>

#include <conio.h>

#include <string.h>

void main()

{

    char names[5][31], t[31];

    int i, j;

    clrscr();

    //Accept 5 strings

    cout << "\nEnter 5 names : ";

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

        cin.getline(names[i],31);

    //Sorting begins here

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

    {

        for(j=i+1; j<5; j++)

        {

            if( strcmp(names[i],names[j]) > 0 )

            {

                strcpy(t,names[i]);

                strcpy(names[i],names[j]);

                strcpy(names[j],t);

            }

        }

    }

    //Display Sorted Strings

    cout << "\n5 names : \n";

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

        cout << names[i] << endl;

getch();

}

Product Matrix – Find the Product of Two Matrices

//TO WRITE A PROGRAM TO FIND THE PRODUCT OF TWO MATRICES

#include<iostream.h>

#include<conio.h>

void input(int t[][3]);

void product(int a[][3], int b[][3], int c[][3]);

void display(int [][3]);

void arrinit(int [][3]);

void main()

{

    int a[3][3], b[3][3], c[3][3];

    clrscr();

    cout << "Enter Values for Matrix 1 \n";

    input(a);

    cout << "Enter Values for Matrix 2 \n";

    input(b);

    product(a,b,c);

    cout << "\nProduct of two matrices are \n\n";

    display(c);

getch();

}

void input(int t[][3])

{

    int i, j;

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

    {

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

            cin >> t[i][j];

    }

}

void product(int a[][3], int b[][3], int c[][3])

{

    int i, j, k;

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

    {

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

        {

            c[i][j]=0;

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

            {

                c[i][j] += a[i][k] * b[k][j];

            }

        }

    }

}

void display(int c[][3])

{

    int i, j;

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

    {

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

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

    cout << "\n";

    }

}

Monday, November 29, 2010

Chronologically Descending Order

#include <iostream.h>

#include <conio.h>

void main()

{

    int a[5], i;

    clrscr();

    cout << "Enter values for array : ";

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

        cin >> a[i];

    cout << "\nChronological Descending Order \n\n";

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

        cout << a[i] << endl;

getch();

}

Merging Two One Dimensional Numeric Array

#include <iostream.h>

#include <conio.h>

void main()

{

    int a[5], b[5], c[10], i, j;

    clrscr();

    cout << "Enter values for array 1 : ";

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

        cin >> a[i];

    cout << "Enter values fro array 2 : ";

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

        cin >> b[i];

    for(i=0,j=0; i<5; i++, j++)

        c[j]=a[i];

    for(i=0; i<5; i++, j++)

        c[j]=b[i];

    //display merged array

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

        cout << c[i] << "\t";

getch();

}

Sunday, November 28, 2010

Equality of the Two Dimensional Arrays

Write a program to check the equality of an array with its transpose.

#include <iostream.h>

#include <conio.h>

void main()

{

    int a[3][3], b[3][3];

    int i, j, f;

    clrscr();

    //input array values

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

    {

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

            cin >> a[i][j];

    }

    //transpose occurs

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

    {

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

        {

            b[i][j]=a[j][i];

        }

    }

    //comparing the equality

    f=1;

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

    {

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

        {

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

            {

                f=0;

                break;

            }

        }

    if(j<=2)

        break;

    }

    if(f==1)

        cout << "Original and Transpose are equal!!!";

    else

        cout << "Original and Transpose are not equal...";

getch();

}

Thursday, November 25, 2010

Magic Square

Building Magic Square is not a serious thing for a beginner.....

First what is the idea behind? Let see...

We need a 3x3 matrix (2 D Array)

The number arrangement will look like the below given table.

8

1

6

3

5

7

4

9

2

From the above table it's confirmed that every order we count (horizontal, vertical, diagonal) we get 15.

Logic

Using three variables.. r, c, and i, r will represent row, c will represent column and i will start from value 1 and upto 9

First find the centre column of the top row. Here it is 3x3 that is 3/2=1.5, hence we take integers it will take only 1

That means

C=3/2=1

By default r will be 0 i.e. a[r][c]=1;

Next

r--;

c++;

when r<0 then r will assigned as 2 (3x3 matrix – 0,1,2)

when c>2 then c will assigned as 0

when multiples of 3 comes (3, 6) only r will be incremented that is

r++

above steps will be repeated till a i becomes 9

you will get astonishing magic square

detailed program

#include <iostream.h>

#include <conio.h>

void main()

{

    int a[3][3], i, r, c;

    clrscr();

    r=0; c=3/2; // 3 represent columns

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

    {

        a[r][c]=i;

        if(i%3==0)

        {

            r++;

            continue;

        }

        c++;

        r--;

        if(r<0) r=2;

        if(c>2) c=0;

    }

    cout << "\n\nMagic Square of Order 3 x 3 is \n\n";

    for(r=0; r<3; r++)

    {

        for(c=0; c<3; c++)

        {

            cout << a[r][c] << "\t";

        }

    cout << endl;

    }

getch();

}


 

Hope you will enjoy.....

Try this for 5x5, 7x7, 9x9 and nxn and also try the same with even orders (2x2, 4x4, 6x6 etc..)

Tuesday, November 23, 2010

Program to Accept an amount and print its Rupee Denomination

includes iostream.h and conio.h

void main()
{
int n, d;
clrscr();

cout << "Enter an Amount : ";
cin >> n;

d=n/1000;
cout << "1000 = " << d << endl;

n=n%1000;
d=n/500;
cout << "500 = " << d << endl;

n=n%500;
d=n/100;
cout << "100 = " << d << endl;

n=n%100;
d=n/50;
cout << "50 = " << d << endl;

n=n%50;
d=n/20;
cout << "20 = " << d << endl;

n=n%20;
d=n/10;
cout << "10 = " << d << endl;

n=n%10;
d=n/5;
cout << "5 = " << d << endl;

n=n%5;
d=n/2;
cout << "2 = " << d << endl;

n=n%2;
d=n/1;
cout << "1 = " << d << endl;

getch();
}

Labels: