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:

Sunday, November 14, 2010

Structure of a C++ Program

How to write a C++ Program?
The following are the normal format to write a CPP program.

1) Documentation: it contains the details or meta data (Data about data) of a program like developer, to whom the program is writing, what the program is etc. etc...
example:
/*
Program to find the sum of 2 integer variables
programmed by Satheesh Kammath R
this program is writing for my students
*/

2) Include section: This includes header files for our program. Header files are the file which contains pre defined variables and functions used for our program. Normally this file will be stored in INCLUDE directory(folder) with the extension .h
example:
#include "" (" cannot be used). # symbol is known as pre processor directive. because this instruct the compiler to include the specified header file into our program before the main process starts.

we can also create our own header files for our later use.

3) Global Variable/Function Section: this contains variable declared for global use(for entire program) means the variable or functions declared here can be accessed from any part of the program.
example:
const float pi = 3.14;
char title[]="Satheesh Kammath R"
void display(void); //declaration only - also called function prototype

4) Main section: this is the entry point of every C++ program. this function should be named as main followed by ().
and then followed by the program code which is to be written inside the main function.
example:
int main()
{
int n;
cout << "Enter a number ";
cin >> n;
cout << "Your number is " << n << endl;
}

5) function definition section
the body or process of the function is writing here with declaration
example:
void display(void)
{
cout << "Welcome to the world of C";
cout << "Hello World";
}

In the above given sections
section 1, 3, 5 are optional, means it is not compulsory to write these section to run cpp program.
that strongly impose that other sections, 2 4 are important to execute a cpp program.

hope make it sense...

if any doubts or clarifications please comment or email to skrtvm@gmail.com

Labels:

Sunday, November 7, 2010

Developer of C++


Bjarne Stroustrup (pronounced as Byan Stroustrup) is the master developer of C++.

Welcome to Love C++ Blog

Welcome readers,

Through this blog I am aiming to solve the problems facing by a beginner student in C/C++ computer language by sharing knowledge I got hitherto in C++.

With regards

Satheesh Kammath R
Faculty in Computer Science
Saraswathi Vidyalaya
Mobile: 9495037421

Labels: