Question

Question 11/. C++ PLEASE Environment Canada has a program that processes daily temperatures. It includes the...

Question 11/. C++ PLEASE

Environment Canada has a program that processes daily temperatures. It includes the variable declarations

given below.

const int MAXDAYS = 100;

double temperatures[MAXDAYS], minTemperature;

int actualDays, longestColdSnapDuration, longestColdSnapStart;

Part I

Part of the program involves finding the lowest temperature. The code is as follows:

minTemperature = findMinimumTemperature (temperatures, actualDays);

cout << "The lowest temperature is " << minTemperature << endl;

Unfortunately the code for function findMinimumTemperature has been lost. Save the day by

writing this function. You should be able to work out everything you need to know.

Part II

The program also deals with cold snaps (one or more consecutive days all having a temperature below -10

degrees). The relevant code is given below:

findLongestColdSnap (temperatures, actualDays,

longestColdSnapDuration,

longestColdSnapStart);

if (longestColdSnapDuration == 0) {

cout << "There were no cold snaps." << endl;

} else {

cout << "The longest cold snap started on day " <<

longestColdSnapStart

<< " and lasted for " << longestColdSnapDuration << "

days." << endl;

}

Function findLongestColdSnap is missing and you must write it as well. If there a number of

equally long cold snaps, any one of them may be chosen as the longest cold snap. As the output is

intended for humans, the first day for which there is temperature data is day one (and not day zero). You

should be able to work out everything else you need to know from the declarations and code supplied.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the required c++ code:

#include <iostream>

using namespace std;

const int MAXDAYS = 100;//required declaration

//function to calculate minimum temperature

double findMinimumTemperature(double *temperatures, int actualDays){

   double minTemp = temperatures[0];//set min temp as first day temp

   for(int i = 1; i < actualDays; ++i){

       minTemp = min(minTemp, temperatures[i]);//if any day has temp smaller that min temp, set min temp to that

   

   return minTemp;

}

//function to find longest cold snap

void findLongestColdSnap(double *temperatures, int actualDays, int &longestColdSnapDuration, int &longestColdSnapStart){

   int count = 0;//current streak

   int start = -1;//current streak start day

   int curStart, curMaxDays = 0;//current maximum streak and start day

   for(int i = 0; i < actualDays; ++i){

       if(temperatures[i] < -10){

           count++; //increment current streak

           if(start == -1)start = i;//set current streak start day if not already set

           //compare with current maximums streak and update

           if(count > curMaxDays){

               curMaxDays = count;

               curStart = start;

           

       

       else{

           count = 0;

           start = -1;

       

   

   //update final max streak and start day

   longestColdSnapDuration = curMaxDays;

   longestColdSnapStart = curStart + 1;

}

int main()

{

   //required variable declarations

   double temperatures[MAXDAYS], minTemperature;

   int actualDays, longestColdSnapDuration, longestColdSnapStart;

   //input actualDays and temperatures on each day

   cout << "Enter the total number of days: ";

   cin >> actualDays;

   cout << "Enter temperature on each day: ";

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

   

       cin >> temperatures[i];

   

   cout << endl;

   //below is the code provided in question

   minTemperature = findMinimumTemperature(temperatures, actualDays);

   cout << "The lowest temperature is " << minTemperature << endl;

   findLongestColdSnap(temperatures, actualDays, longestColdSnapDuration, longestColdSnapStart);

   if (longestColdSnapDuration == 0)

   

       cout << "There were no cold snaps." << endl;

   

   else

   

       cout << "The longest cold snap started on day " << longestColdSnapStart << " and lasted for " << longestColdSnapDuration << " days." << endl;

   

}


Sample IO:
Terminal File Edit View Search Terminal Help |[unseen@pc]08:44 AM_$ ~/prog/CPPclude <iostream> g++ main.cpp [unseen@pc]08:44

Add a comment
Know the answer?
Add Answer to:
Question 11/. C++ PLEASE Environment Canada has a program that processes daily temperatures. It includes the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT