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')