#include <fstream> #include <iostream> #include <sstream> #include <string> usin
ID: 3705009 • Letter: #
Question
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
//function to read tempratures from file to array
void readTemp(string Month[],double temp[] );
void displayTemps(string Month[], double );
double findAvgTemp(double temp[], int days);
void convertToFahr(double temp[], int days);
int daysAboveAvg(double temp[], double avg);
double findHiTemp(double temp[], string Month[]);
int main()
{
double temperatures[92] = { 0 };
double avgF = 0;
string Month[3] = { "March","April","May" };
readTemp(temperatures);
displayTemps(Month[], temperatures);
convertToFahr(temperatures, 92);
displayTemps(Month[], temperatures);
displayTemps(Month[], temperatures);
avgF = findAvgTemp(temperatures, 92);
cout << " The Average temperature (in Fahr) for March to May is " << avgF;
cout << " The days above average temperature (in Fahr) for March to May is " << daysAboveAvg(temperatures, avgF);
cout << " The Highest temperature (in Fahr) for March is " << findHiTemp(temperatures, Month);
cout << " The Highest temperature (in Fahr) for April is " << findHiTemp(temperatures, Month);
cout << " The Highest temperature (in Fahr) for May is " << findHiTemp(temperatures, Month);
}
void readTemp(double temp[])
{
//Variables
int i = 0;
std::ifstream infile("pgm6.txt");
std::string line;
while (std::getline(infile, line))
{
std::istringstream iss(line);
iss >> temp[i++];
}
infile.close();
}
void displayTemps(string Month[], double temp[], bool celcius = true)
{
string months[3] = { "March","April","May" };
string degree = celcius ? "Celcius" : "Fahrenhite";
cout << "Temperatures for " << months[MARCH] << endl;
cout << "Joshua Radabaugh"<<endl;
cout << "CS2010, Spring 2018, 11:30"<<endl;
cout << "Day Temperature ";
cout << " of month " << degree << endl;
cout << "----------------------------------"<<endl;
int days = 0;
int totaldays = 0;
for (int count = 0; count < 3; count++)
{
if (count = 0)
{
totaldays = 0;
days = 31;
for (int i = 0; i < days; i++)
{
cout << " " << i + 1 << " " << (int)temp[totaldays + i] << endl;
}
}
if (count = 1)
{
totaldays = 31;
days = 30;
for (int i = 0; i < days; i++)
{
cout << " " << i + 1 << " " << (int)temp[totaldays + i] << endl;
}
}
if (count = 2)
{
totaldays = 61;
days = 30;
for (int i = 0; i < days; i++)
{
cout << " " << i + 1 << " " << (int)temp[totaldays + i] << endl;
}
}
}
//function to find average temperature
double findAvgTemp(double temp[], int days)
{
double sum = 0, avg = 0;
for (int i = 0; i < days; i++)
{
sum += temp[i];
}
avg = sum / days;
return avg;
}
//take the celcius and convert it into farenhieght
void convertToFahr(double temp[], int days)
{
for (int i = 0; i < days; i++)
{
temp[i] = (double)(9.0 / 5.0)*temp[i] + 32;
}
}
//find the days average
int daysAboveAvg(double temp[], double avg)
{
int count = 0;
for (int i = 0; i < 92; i++)
{
if (temp[i] > avg)
{
count++;
}
}
return count;
}
//this funtion is to find the higest tempatur of each month
double findHiTemp(double temp[], string Month[])
{
int days = 0;
int totaldays = 0;
double hi = 0;
switch (month) {
case MARCH:
totaldays = 0;
days = 31;
case APRIL:
totaldays = 31;
days = 30;
case MAY:
totaldays = 61;
days = 31;
hi = temp[totaldays];
for (int i = 1; i < days; i++)
{
if (hi < temp[totaldays + i])
{
hi = temp[totaldays + i];
}
}
return hi;
}
this is my code the last answer helped explain alot but there was also a lot that i have not learned yet so i didn't want to add that to my program and i also used it to clean up my program im still stuck getting a lto of errors in my program plus i would like to know what i am missing/doing wrong and why it is wrong or why what i am missing is important to my program.
Parts from the last one that i havent learned yet are enum,break;, and cases like case MARCH: is there any simpler way to do this?
totaldays = 0;
days = 31;
this is my program requirements
Program 6 25 pts CS2010: Rex Spring, 2018 Due: Wednesday, April 11,2018 Problem: Is the Earth warming up? Are the temperatures we have been experiencing recently normal fluctuations or evidence of a warming trend? Write a C++ program to analyze temperature data from March, April and May of 2017 for Ohio. Create a project using your last name, first initial and pgmó for the project folder and cpp file names (e.8. WaveH pgm6 and WaveH pgm6.cpp). Your output may be displayed on the screen. Input: The temperature (in Celsius) for each day of March, April and May (92 temperatures) is stored in the file pgm6.txt. Copy this file from the Canvas class web site (Files 02 Program Assignments) to your project folder. Your project should read in each temperature and store it in the next available element of an array of type double All of the temperatures must be stored in the same array. Use the data values stored in the array to perform the steps described below. Processing: Your main function should do the following: 1. Declare the array to hold the temperatures and any other variables needed. 2. Call a function to read the daily temperatures from the file pgm6.txt, storing each temperature in an element in the array. Open and close the data file inside this function. All further processing must be done using the data values stored in the array. Temperatures in the file look like this: -5.55556 -1.66667 Call a function to display the temperatures for the month of March. Include a heading with your name, class data and two columns-one showing the day of the month and one showing the temperature for that day-see sample below. Note that the temperatures are displayed with no decimal places. 3. Temperatures for March Your Name Cs2010, Spring 2018, xx:xx Temperature Day of month (Celsuis) -6 -2 4 etc. etc. Call a function to convert all of the temperatures in the array from Celsius to Fahrenheit (F-905.0 C + 32), replacing the original Celsius temperature with the Fahrenheit value for each element. Note: You do not need to create a new array for this function, just change the values in the original array.Explanation / Answer
I have edited the code some modifications is doen to remove the error.
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
//function to read tempratures from file to array
void readTemp(double temp[] );
void displayTemps(double temp[]);
double findAvgTemp(double temp[], int days);
void convertToFahr(double temp[], int days);
int daysAboveAvg(double temp[], double avg);
double findHiTemp(double temp[], int Month);
int main()
{
double temperatures[92] = { 0 };
double avgF = 0;
string Month[3] = { "March","April","May" };
readTemp(temperatures);
displayTemps(temperatures);
convertToFahr(temperatures, 92);
displayTemps(temperatures);
displayTemps(temperatures);
avgF = findAvgTemp(temperatures, 92);
cout << " The Average temperature (in Fahr) for March to May is " << avgF;
cout << " The days above average temperature (in Fahr) for March to May is " << daysAboveAvg(temperatures, avgF);
cout << " The Highest temperature (in Fahr) for March is " << findHiTemp(temperatures, 0);
cout << " The Highest temperature (in Fahr) for April is " << findHiTemp(temperatures, 1);
cout << " The Highest temperature (in Fahr) for May is " << findHiTemp(temperatures, 2);
return 0;
}
void displayTemps(double temp[])
{
bool celcius = true;
string months[3] = { "March","April","May" };
string degree = celcius ? "Celcius" : "Fahrenhite";
cout << "Temperatures for " << months[0] << endl;
cout << "Joshua Radabaugh"<<endl;
cout << "CS2010, Spring 2018, 11:30"<<endl;
cout << "Day Temperature ";
cout << " of month " << degree << endl;
cout << "----------------------------------"<<endl;
int days = 0;
int totaldays = 0;
for (int count = 0; count < 3; count++)
{
if (count = 0)
{
totaldays = 0;
days = 31;
for (int i = 0; i < days; i++)
{
cout << " " << i + 1 << " " << (int)temp[totaldays + i] << endl;
}
}
if (count = 1)
{
totaldays = 31;
days = 30;
for (int i = 0; i < days; i++)
{
cout << " " << i + 1 << " " << (int)temp[totaldays + i] << endl;
}
}
if (count = 2)
{
totaldays = 61;
days = 30;
for (int i = 0; i < days; i++)
{
cout << " " << i + 1 << " " << (int)temp[totaldays + i] << endl;
}
}
}
}
void readTemp(double temp[])
{
//Variables
int i = 0;
std::ifstream infile("pgm6.txt");
std::string line;
while (std::getline(infile, line))
{
std::istringstream iss(line);
iss >> temp[i++];
}
infile.close();
}
//function to find average temperature
double findAvgTemp(double temp[], int days)
{
double sum = 0, avg = 0;
for (int i = 0; i < days; i++)
{
sum += temp[i];
}
avg = sum / days;
return avg;
}
//take the celcius and convert it into farenhieght
void convertToFahr(double temp[], int days)
{
for (int i = 0; i < days; i++)
{
temp[i] = (double)(9.0 / 5.0)*temp[i] + 32;
}
}
//find the days average
int daysAboveAvg(double temp[], double avg)
{
int count = 0;
for (int i = 0; i < 92; i++)
{
if (temp[i] > avg)
{
count++;
}
}
return count;
}
//this funtion is to find the higest tempatur of each month
double findHiTemp(double temp[], int Month)
{
int days = 0;
int totaldays = 0;
double hi = 0;
switch (Month) {
case 0:
totaldays = 0;
days = 31;
case 1:
totaldays = 31;
days = 30;
case 2:
totaldays = 61;
days = 31;
hi = temp[totaldays];
for (int i = 1; i < days; i++)
{
if (hi < temp[totaldays + i])
{
hi = temp[totaldays + i];
}
}
return hi;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.