Thursday , 28 November 2024

CS201 Assignments # 2 Solution Spring 2012

Problem Statement: Temperature Calculation system

Write a program that displays average, highest and lowest temperatures.

Detailed Description:

• The program will take temperature readings as input from user for specified number of consecutive days.
• Program will store these temperature values into an array.
• Then it will pass this array to three different functions.
• One function will return highest temperature, one will return lowest temperature and third one will return average temperature.
• All these functions will be called inside main() function where these temperature results will be displayed to the user.

Sample Output:

Enter the number of consecutive days to read their temperature : 5
Enter temperature for day 1: 50
Enter temperature for day 2: 80
Enter temperature for day 3: 30
Enter temperature for day 4: 92
Enter temperature for day 5: 47
The average temperature is 59.80
The highest temperature is 92.00
The lowest temperature is 30.00


Solution:-

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

int getMaxTemp(int[],int); // function to calcualte Maximum Temprature
int getMinTemp(int[],int); // function to calculate Minimum Temprature
float getAvgTemp(int[],int); // function to calculate Average Temperature

main()
{
int NDays;
cout<<“Enter the number of consecutive days to read their temperature : “;
cin>>NDays;
if(NDays<=0){
cout<<“Please Enter number of days greater than 0!”<<endl;
system(“pause”);
return 0;
}

int Cday[NDays]; // array to store temprature
for(int i=0;i<NDays;i++){
cout<<endl<<“Enter temperature for day “<<i+1<<“: “;
cin>>Cday[i];
}
cout<<endl<<“The average temperature is “<<getAvgTemp(Cday,NDays);
cout<<endl<<“The highest temperature is “<<getMaxTemp(Cday,NDays);
cout<<endl<<“The lowest temperature is “<<getMinTemp(Cday,NDays);
cout<<“\n\n”;
system(“PAUSE”);
return EXIT_SUCCESS;
}
// function defination of calculating maximum temprature
int getMaxTemp(int data[],int length){
int max=data[0];
for(int i=0;i<length;i++)
if(max<data[i])
max=data[i];
return max;
}

// function defination of calculating minimum temprature

int getMinTemp(int data[],int length){
int min=data[0];
for(int i=0;i<length;i++)
if(min>data[i])
min=data[i];
return min;
}

// function defination of calculating average temprature
float getAvgTemp(int data[],int length){
float total=0;
float avg = 0;
for(int i=0;i<length;i++)
total+=data[i];
avg = total/length;
return avg;
}

Download Solution file from here

 

CS201_Assignments_Solution_2_Spring_2012

Check Also

CS201 Assignment no 02 complete idea solution has been uploaded

CS201 Colpmete Idea Solution Step 1 Copy and paste below given code on Dev C++ …

Leave a Reply

Your email address will not be published. Required fields are marked *

*