Monday, 29 February 2016

Average of Numbers in c++

Source Code:
#include<iostream>
using namespace std;

int main()
{
    int n;
    double average(0);
    cout<<"Enter the number of values : ";
    cin >> n;
    for(int i = 0; i < n; ++i)
    {
        int value;
        cin >> value;
        average += value;
    }
    average /= n;
    cout<<"Average is "<<average;
    return 0;
}


OUTPUT


Enter the number of values : 5 12 35 20 45 95
Average is 41.4

Array of Objects using c++

Source code:

#include <iostream>
using namespace std;

class Demo
{
    int x;

    public:
        void setX(int i)
        {
            x = i;
        }
        int getX()
        {
            return x;
        }
};

int main()
{
    Demo obj[4];
    int i;

    for(i=0; i < 4; i++)
        obj[i].setX(i);

    for(i=0; i < 4; i++)
        cout<<"obj["<<i<<"].getX() : "<< obj[i].getX()<<endl;

    return 0;
}


OUTPUT


obj[0].getX() : 0
obj[1].getX() : 1
obj[2].getX() : 2
obj[3].getX() : 3

Array Sort using c++

Source code:

#include <algorithm> // for std::swap, use <utility> instead if C++11
#include <iostream>
#define SIZE 10
using namespace std;

int main()
{

    cout<<"array values :"<<" 132, 520, 210, 510, 140 ,125,52,96,55,85"<<"\n";
    //input array values
    int array[SIZE] = { 132, 520, 210, 510, 140 ,125,52,96,55,85 };

    cout<<"sorted values : ";

    // Step through each element of the array
    for (int startIndex = 0; startIndex < SIZE; ++startIndex)
    {
        // smallestIndex is the index of the smallest element we've encountered so far.
        int smallestIndex = startIndex;

        // Look for smallest element remaining in the array (starting at startIndex+1)
        for (int nowIndex = startIndex + 1; nowIndex < SIZE; ++nowIndex)
        {
            // If the current element is smaller than our previously found smallest
            if (array[nowIndex] < array[smallestIndex])
            // This is the new smallest number for this iteration
            {
                smallestIndex = nowIndex;
            }
        }
       // Swap our start element with our smallest element
        swap(array[startIndex], array[smallestIndex]);
    }

    // Now print our sorted array as proof it works
    for (int index = 0; index < SIZE; ++index)
    {
        cout << array[index] << ' ';
    }

    cout<<"\n";
    return 0;

}

OUTPUT

array values : 132, 520, 210, 510, 140 ,125,52,96,55,85
sorted values : 52 55 85 96 125 132 140 210 510 520

Array Initialization using c++

Source code:
int main()
{
    //If the size is omitted, the compiler uses the number of values
    int days[12] = {31,28,31,30,31,30,31,31,30,31,30,31};

    // No initialization.
    float p1[1000];

    //To initialize an array to all zeros, initialize only the first value.
    // All 1000 values initialized to zero.
    float p2[1000] = {0.0};

    // Initial values of pressure(variable) undefined.
    float pressure[10];

    // Remaining characters zero.
    char greeting[100] = "Hello";

    // Array size is 6 (final zero on strings).
    char goodbye[] = "Adios";

    return 0;

}

Array Element Swapping using c++

Source code:
#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
    //variable declaration
    int arr[100],i,temp,size;

    cout<<"enter array size : ";
    cin>>size;
    cout<<"enter elements in to array : \n";
    for(i=0;i<size;++i)
    {
        cin>>arr[i];
    }
    cout<<"\narray before swaping elements : \n";
    for(i=0;i<size;++i)
    {
        cout<<arr[i]<<" ";
    }
    //element swapping logic
    for(i=1;i<size;i+=2)
    {
        temp=arr[i];
        arr[i]=arr[i-1];
        arr[i-1]=temp;
    }
    cout<<"\narray after swapping elements : \n";
    for(i=0;i<size;++i)
    {
         cout<<arr[i]<<" ";
    }

}

OUTPUT

 enter array size : 4
 enter elements in to array : 1 2 3 4
 array before swaping elements : 1 2 3 4
 array after swapping elements : 2 1 4 3

Armstrong Number using c++

An Armstrong number of three digits is an integer such that the sum of the cubes of its digits is equal to the number itself. For example, 371 is an Armstrong number since 3**3 + 7**3 + 1**3 = 371.

Source code:

#include <iostream>
using namespace std;
int main()
{
int n, n1, rem, num=0;

cout<<"Enter a positive  integer: ";
cin>>n;
n1 = n;

while(n1!=0)
{
rem=n1%10;
num+=rem*rem*rem;
n1/=10;
}

if(num==n)
{
cout << n << " is an Armstrong number.";
}
else
{
cout << n << " is not an Armstrong number.";
}

return 0;
}