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";

    }

}