C++ Programming Blog

 
 
 

Image

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


 void read_array(int *,int );
 void print_array(int *,int );
 int sum_of_array(int *,int);
 int largest(int *,int);
 int smallest(int *,int);


 main( )
    {
       clrscr();

       const int array_size=5;

       int array[array_size]={0};

       cout<<\"\\n Enter the contents of the array : \\n\"<<endl;
       read_array(array,array_size);

       clrscr();

       cout<<\"\\n ********************************************* \\n\"<<endl;
       cout<<\" The contents of the array are :\\n\"<<endl;
       print_array(array,array_size);
       cout<<\"\\n *********************************************\\n\"<<endl;

       getch();

       cout<<\"The Sum of the given array is = \"<<sum_of_array(array,array_size)<<endl;
       cout<<\"\\n *********************************************\\n\"<<endl;

       getch();

       cout<<\"The maximum element of the array is = \"<<largest(array,array_size)<<endl;
       cout<<\"\\n ********************************************* \\n\"<<endl;

       getch();

       cout<<\"The minimum element of the array is = \"<<smallest(array,array_size)<<endl;
       cout<<\"\\n ********************************************** \"<<endl;

       getch();
       return 0;
    }



 //------------------------  Function Definitions  -----------------------//




 //-----------------------  read_array(int *,int)  -----------------------//


 void read_array(int *array,int array_size)
    {
       for(int count=0;count<array_size;count++)
      {
         cin>>*array;
         array++;
      }
    }


 //-----------------------  print_array(int *, int)  ---------------------//


 void print_array(int *array,int array_size)
    {
       for(int count=0;count<array_size;count++)
      {
         cout<<\"\\t\\t\\t Element [\"<<count<<\"] = \"<<*array<<endl;
         array++;
      }
    }


 //----------------------  sum_of_array(int *,int)  ----------------------//


 int sum_of_array(int *array,int array_size)
    {
       int count;
       int sum=0;

       for(count=0;count<array_size;count++)
      {
         sum=sum+*array;
         array++;
      }

       return sum;
    }


 //-------------------------  largest(int *,int)  ------------------------//


 int largest(int *array,int array_size)
    {
       int max=*array;

       for(int count=0;count<array_size;count++)
      {
         if(*array>max)
        max=*array;

         array++;
      }

       return max;
    }


 //-------------------------  smallest(int *,int)  -----------------------//


 int smallest(int *array,int array_size)
    {
       int min=*array;

       for(int count=0;count<array_size;count++)
      {
         if(*array<min)
        min=*array;

         array++;
      }

       return min;
    }