Monday, May 28, 2012

A program To find out smallest number in an array

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

int main()
{
    int n, i, min;

    printf ("\nEnter Total Numbers Of Numbers In The Array: ");
    scanf ("%d",&n);

    int array[n];

    for(i=0; i<n; i++)
    {
        printf ("\nEnter %d Number: ",i+1);
        scanf ("%d",&array[i]);
    }

    min = array[0];

    for(i=0; i<n; i++)
    {
        if(array[i] < min)

        min = array[i];
    }

    printf("\nThe Smallest Number in the array is %d \n",min);    
}

Wednesday, May 23, 2012

Encapsulation

//encapsulation in cpp

#include<iostream>
using namespace std;

class Adder
{
    public:
        //constructor
        Adder(int i=0)
        {
            total = i;
        }
       
        //interface to outside world

        void addNum(int number)
        {
            total += number;
        }
       
        //interface to outside world
       
int getTotal()
        {
            return total;
        }
   
    private:
        //hidden data from outside world
        int total;
};

int main()
{
    Adder a;
   
    a.addNum(10);
    a.addNum(20);
    a.addNum(30);
   
    cout << "Total " << a.getTotal() <<endl;
}

Friday, May 18, 2012

A program to find largest number in an given array

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

int main()
{
    int n, i, max;

    printf ("Enter Total Numbers Of Numbers In The Array: ");
    scanf ("%d",&n);

    int array[n];
   
    for( i=0; i<n; i++)
    {
        printf ("\nEnter %d Number: ",i+1);
        scanf ("%d",&array[i]);
    }

    max = array[0];

    for(i=0;i<n;i++)
    {
        if(array[i] > max)

        max = array[i];
    }
    printf ("\nThe Largest Number in the array is %d ",max);    
}

Monday, May 14, 2012

'c++ struct' can have private_members and function also

//A Simple program to show c++ structure can have function
// using c++, GCC complier
 
#include <iostream>
using namespace std;

struct CRectangle       //here we define a user-defined-datatype using 'struct'
{
    private:
        int x, y;


    public:
        void set_values (int,int);


        int area()
        {
            return (x*y);
        }
};

void CRectangle :: set_values (int a, int b)
{
    x = a;
    y = b;
}

int main()
{
    CRectangle rect;    //creating the object (rect) of a class (CRectangle)


    rect.set_values (3,4);
    cout << "area:" << rect.area();
   
    cin.ignore();
    cin.get();
}

______________________________________________________________
also see the same program using class (only difference, we use 'class' instead of 'struct')

Wednesday, May 9, 2012

A program to print multiplication table from 1 to 10

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

int main()
{
    int i,j;

    int array[10][10];
    
    for(i=0; i<10; i++)
    {
        for(j=0; j<10; j++)
        {
            array[i][j] = (i+1)*(j+1);
        }               
    }
    
    for(i=0; i<10; i++)
    {
        for(j=0; j<10; j++)
        {   
            printf("%3d ",array[i][j]);
        }
        printf("\n");
    } 
}

Tuesday, May 8, 2012

Program to understand array of object

#include <iostream>
using namespace std;

class info
{
    char name[20];
    int roll;
   
    public:
        void
getinfo()
        {
            cout << "\n Enter name: ";
            cin >> name;
            cout << "\nEnter roll number: ";
            cin >> roll;
        }
   
        void displayinfo()
        {
            cout << "\n\n\tName: " << name;
            cout << "\n\tRollNumber:" << roll;
        }
};

int main()
{
    info obj[5];     //Here i am creating 5 object using array


    for(int i=0; i<5; i++)
    {
        obj[i].getinfo();
    }
   
    for(int i=0; i<5; i++)
    {
        obj[i].displayinfo();
    }
   
    cout << "\n\n";   
}