Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Scientists are involved in a study to record the total number of bad winter days

ID: 3820798 • Letter: S

Question

Scientists are involved in a study to record the total number of bad winter days in a month. They have obtained the file which consists of the temperature data of a certain month. Write a program in C++ that will read this file and be able to count the total number of days in that given month for which temperature was below zero degree Celsius. You can assume that the maximum number of weeks that can be recorded for a month is at most four and the maximum number of days is at most twenty eight.

The input file structure is as follows:
line 1: an integer value depicting the total number of weeks
line 2: an integer value depicting the number of days for each week that was recorded
line 3 and onwards: integer values (positive and negative) depicting the recorded temperature in degree Celsius for that day

Example Input File:
1 //recorded only for 1 week
3 // recorded only the first 3 days of each week
5 -6 8 //temperature reading for the first 3 days

Your program output for the above example input file should return the value 1 (total number of temperature readings below zero degree Celsius).

Attached the input file to be used for your program.

3
7
2 6 7 -8 -4 -3 7
-5 -2 -6 -8 2 0 3
5 5 4 3 -1 -4 -5

Explanation / Answer

Please see f the source code and the output:

#include <iostream>

#include <fstream>

using namespace std;

int main(int argc, const char * argv[]) {

// local variable declarations

  

int number_of_days,number_of_weeks,temperature;

int count = 0;

  

// open input.txt file in read mode

ifstream fp;

fp.open("/Users/shreya/Desktop/smita/Amey_K/Practice examples/badWinterDays/badWinterDays/input.txt");

  

// read the number

fp >> number_of_weeks ; // first number is the total number of weeks

fp >> number_of_days ; // Next number is the total number of days

  

// loop infinite until reached end of input.txt file

while(1)

{

if(fp.eof()) // if it is end of the file input.txt

break; // exit the loop

fp >> temperature ; // read the next integer and store the result in temperature

if(temperature < 0)

count++;

}

count--;

// Display the count of bad winter days

cout << " Total number of temperature readings below zero degree Celsius :" << count << endl;

return 0;

}


output:

Total number of temperature readings below zero degree Celsius :10

Program ended with exit code: 0

Note: Could not attach the screenshot due to technical issue.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote