Monday, April 30, 2012

CS201 2 introduction to programming assignment solution fall 2012


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

int highest(int[],int);
int lowest(int[],int);
float average(int[],int);

main()
{
      int nmbr,*ptr, high, low;
      float avg;
      cout<<"Enter the number of consecutive days to read their temperature : ";
      cin>>nmbr;
      ptr= new int[nmbr];
      for (int i =0; i<nmbr; i++ )
      {
          cout<<"\nEnter temperature for day "<<i+1<<" : ";
          cin>>ptr[i];
      }
      avg = average(ptr, nmbr);
      high =highest(ptr, nmbr);
      low = lowest(ptr, nmbr);
     
      cout<<"\n\nThe average temperature is \t"<<avg<<endl;
      cout<<"The highest temperature is \t"<<high<<endl;
      cout<<"The lowest  temperature is  \t"<<low<<endl;
getch();    
}

float average (int arr[], int size)
{
      float total=0;
      float avg;
      for (int i=0;i<size;i++)
      total = total + arr[i];
      avg = total/size;
      return avg;
}

int highest (int arr [], int size)
{
    int big = arr[0];
    for(int i=0; i<size; i++)
    if (big < arr[i])
    big = arr[i];
    return big;
}

int lowest (int arr [], int size)
{
    int small = arr[0];
    for(int i=0; i<size; i++)
    if (small > arr[i])
    small = arr[i];
    return small;
}

No comments:

Post a Comment