Tuesday, 5 January 2016

Calculate the average smallest and the largest number in Array | Array Exercise | Array Logic Building Exercise

Calculate the average smallest and the largest number in Array | Array Exercise | Array Logic Building Exercise 

This is the logic building program on array exercise. The program reads a group of numbers from the user and places them in an array of type float. Once the numbers are stored in the array, the program calculate the average of the input numbers, find the smallest and the largest number from the group of input numbers and print the result for all calculations.
Source Code
#include<iostream>
using namespace std;
float division(float[],int);
float minMax(float[] ,int );
int main()
{
      int size;              //Array size
      cout << "How many numbers do you want to input? ";
      cin >> size;       
      float dataArray[size];     //Declaring dataArray       
   for(int index=0;index<size;index++)   //Loop which inputs arrays data
                               
      {
              cout << "Number " << index+1 << " is: ";
              cin>>dataArray[index];
      }
      //Now calling division function to find the sum...
      cout<<"Average of dataArray elements is "<<division(dataArray,size)<< endl;
      minMax(dataArray,size);
      system("pause");
      return 0;
}
float division(float dataArray[],int size)
{
    float sum;
    float average=0;
    for(int index=0;index<size;index++)
        sum +=dataArray[index];
     average=sum/size;
     return average;
}
float minMax(float dataArray[],int size)
{
 int max;
 int min;
 max=dataArray[0];
 min=dataArray[0];

 for (int index=0 ; index<size; index++)
 {
  if (dataArray[index] > max)
  {
   max = dataArray[index];
  }
  if (dataArray[index] < min)
  {
   min= dataArray[index];
  }
 }
 cout<<"\nBiggest number is : "<<max;
 cout<<"\nSmallest number is: "<<min<<endl;
}
Output of the Program


 Array Logic Building Exercise

No comments:

Post a Comment