Showing posts with label array. Show all posts
Showing posts with label array. Show all posts

15.9.15

array programming in c++

/* Fill up an array,print the array,count number of values,count the number of odd,count the number of even,sum  of odd ,sum of even,average,find largest value,find smallest */

#include<iostream>
using namespace std;
int fill_up(int array[],int size);
int main()
{
    int array[10],size;
    cout<<"Enter the size of an array:"<<endl;
    cin>>size;
    fill_up(array,size);
}
int fill_up(int array[],int size)
{
    int count=0,even=0,odd=0,even_sum=0,odd_sum=0,largest,smallest;
    float avg=0.0,avg_sum=0.0;
    cout<<"Enter the element:"<<endl;
    for(int i=0;i<size;i++)
    {
        cin>>array[i];
        if(array[i]%2==0)
        {
           even++;
           even_sum+=array[i];
        }
        else
        {
            odd++;
            odd_sum+=array[i];
        }
        count++;
        avg_sum+=array[i];
    }
    avg=avg_sum/size;
    largest=array[0];
    smallest=array[0];
    for(int i=0;i<size;i++)
    {
        if(array[i]<smallest)
        {
            smallest=array[i];
        }
    }
    for(int i=0;i<size;i++)
    {
        if(array[i]>largest)
        {
            largest=array[i];
        }
    }
    cout<<"The value of the array is:"<<endl;
    for(int i=0;i<size;i++)
    {
        cout<<array[i]<<"\t";
    }
    cout<<endl;
    cout<<"The number of value into the array:"<<count<<endl;
    cout<<"The number of even Value is:"<<even<<endl;
    cout<<"The number of Odd value is:"<<odd<<endl;
    cout<<"The sum of the even number is:"<<even_sum<<endl;
    cout<<"The sum of the Odd number is:"<<odd_sum<<endl;
    cout<<"The Avarage is:"<<avg<<endl;
    cout<<"The Largest value is:"<<largest<<endl;
    cout<<"The Lowest value is:"<<smallest<<endl;
    return 0;
}

27.8.15

Define a function template for finding the minimum value contained in an array

Define a function template for finding the minimum value contained in an array. Write main( ) function to find the minimum value of integer array and minimum value of floating point numbers in an array.

#include<iostream.h>

#include<conio.h> template<class T>

T minimum(T a[],int size)

{

T min=a[0];

for(int i=0;i<size;i++)

{

if(a[i]<min)

min=a[i];

}

return min;

}

int main()

{

int a[10],size,i,min1; float b[10],min2; clrscr();

cout<<"enter the size value:\n"; cin>>size;
cout<<"enter the integer aray elements\n"; for(i=0;i<size;i++)

cin>>a[i];

cout<<"enter the floating array elements\n"; for(i=0;i<size;i++)

cin>>b[i];

min1=minimum(a,size);

min2=minimum(b,size);


cout<<"The minimum integer elements is:\n"; cout<<min1;
cout<<"\nThe minimum floating elements is :\n";

cout<<min2;

getch();

return 0;

}

************************************OUTPUT******************************

enter the size value: 5

enter the integer aray elements 20 30 10 38 28



 enter the floating array elements 20.4 19.7 14.8 1.7 2.6


The minimum integer elements is: 10
The minimum floating elements is : 1.7