Question

Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing an array of structs to a function, and returning a struct as the value of a function. A function in the program should read several rows of data from a text file. (The data file should accompany this assignment.) Each row of the file contains a month name, a high temperature, and a low temperature. The main program should call another function which finds and returns a struct which contains the month with the highest temperature, and another function to do the same with the lowest temperature. The main program should output the highest and lowest temperatures for the year, along with the month names that correspond to those temperatures.

  • The program should define a struct that holds a month name, a low temperature (an int) and a high temperature (an int).
  • The program should define an array of such structs. The number of elements in that array should be established using a named constant of type int. This value should be sufficient to store all of the data records from the provided data file.

Your program MUST use the following functions:

  • Function loadData: The function reads and stores data in the parallel array and 2D array from a text file (temps.txt).
    int loadData( ifstream &infile, Temperature [], int &rows )
    (Note that the last parameter returns the number of rows of data actually read from the file.)
  • Function findLow: This function finds the month that has the lowest temperature and returns a copy of the struct containing that information.
    Temperature findLow( Temperature [], int rows )
  • Function findHigh: This function finds the month that has the highest temperature and returns a copy of the struct containing that information.
    Temperature findHigh( Temperature [], int rows ):
  1. The main program should display a brief description of the program.
  2. The main program should report the number of months of data read from the file.
  3. The main program should not call the High and Low functions if the number of rows is less than 1.
  4. Do not use any global variables. (All variables must be declared inside functions.)
  5. You must use all the given function prototypes and not change them. You can add more functions if you like.
  6. The first thing each function should do is announce that it has been called, after which it should display no other output.
  7. Use a while loop to read all lines of text from the input file.
  8. LoadData( ) should include code to prevent the arrays from being exceeded.
  9. The loadData function should return 0 if no error occurred or a nonzero number if an error occurred.
  10. The program should return an error if the file could not be opened.
  11. Keep the input file in the same directory as your project.

This program finds the warmest and coldest months as listed in a data file. KloadData called) found 12 months of data Kaverag

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

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// tempMonths.txt

January 47 36
February 51 37
March 57 39
April 62 43
May 69 48
June 73 52
July 81 56
August 81 57
September 75 52
October 64 46
November 52 41
December 45 35

________________________

#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;

struct Temperature
{
   string monName;
   int high;
   int low;
};

void loadData( ifstream &infile, Temperature [], int &rows );
Temperature findLow( Temperature [], int rows );
Temperature findHigh( Temperature [], int rows );
int main() {
   string monName;
   int high,low,rows=0;
  
//defines an input stream for the data file  
ifstream dataIn;
  
//Opening the input file
dataIn.open("tempsMonths.txt");
//checking whether the file name is valid or not
if(dataIn.fail())
{
   cout<<"** File Not Found **";
return 1;
}
else
{
   cout<<"This program finds the warmest and coolest months as listed in a data file."<<endl;
   Temperature temps[100];
loadData(dataIn,temps,rows);
cout<<"found "<<rows<<" months of data"<<endl;
Temperature taHigh=findHigh(temps,rows);
cout<<"The lowest average temperaure was "<<taHigh.low<<" in "<<taHigh.monName<<endl;
Temperature taLow=findLow(temps,rows);
cout<<"The lowest average temperaure was "<<taLow.low<<" in "<<taLow.monName<<endl;
  
}

  
  
//Closing the intput file
dataIn.close();

  
return 0;
}
void loadData( ifstream &infile, Temperature temps[], int &rows )
{
   int i=0;
   cout<<"(loadData called)"<<endl;
   while(infile>>temps[i].monName>>temps[i].high>>temps[i].low)
   {
       i++;
       rows++;
   }
   infile.close();
  
}
Temperature findLow(Temperature temps[],int rows)
{
   cout<<"(averageLow called)"<<endl;
   int min=temps[0].low;
   int indx=0;
   for(int i=0;i<rows;i++)
   {
       if(min>temps[i].low)
       {
           min=temps[i].low;
           indx=i;
       }
   }
   return temps[indx];
}
Temperature findHigh( Temperature temps[], int rows )
{
    cout<<"(averageHigh called)"<<endl;
   int max=temps[0].high;
   int indx=0;
   for(int i=0;i<rows;i++)
   {
       if(max<temps[i].low)
       {
           max=temps[i].low;
           indx=i;
       }
   }
   return temps[indx];  
}

_________________________

Output:
C:AProgram Files (x86)\Dev-Cpp\MinGW64\bin\ReadFileMonthTempsFindAverageHighLOwTemps... This program finds the warmest and co

_______________Could you plz rate me well.Thank You

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that demonstrates use of programmer-defined data structures (structs), an array of structs, passing...
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
  • Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank...

    Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank you. Here are the temps given: January 47 36 February 51 37 March 57 39 April 62 43 May 69 48 June 73 52 July 81 56 August 83 57 September 81 52 October 64 46 November 52 41 December 45 35 Janual line Iranin Note: This program is similar to another recently assigned program, except that it uses struct and an array of...

  • Write a program that uses a two-dimensional array to store the highest and lowest temperatures for...

    Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. You should also have a parallel array to store the names of the 12 months. The program should read the Month's names, and the temperatures from a given input file called temperature.txt. The program should output the highest and lowest temperatures for the year, and the months that correspond to those temperatures. The program must use the following functions:...

  • Write a program that dynamically allocates an array in the freestore large enough to hold a...

    Write a program that dynamically allocates an array in the freestore large enough to hold a user defined number of test scores as doubles. Once all the scores are entered, the array should be passed to a function that finds the highest score. Another function the array should be passed to a function that finds the lowest score. Another function should be called that calculates the average score. The program should show the highest, lowest and average scores. Must use...

  • Write a program that dynamically allocates an array in the freestore large enough to hold a...

    Write a program that dynamically allocates an array in the freestore large enough to hold a user defined number of test scores as doubles. Once all the scores are entered, the array should be passed to a function that finds the highest score. Another function the array should be passed to a function that finds the lowest score. Another function should be called that calculates the average score. The program should show the highest, lowest and average scores. Must use...

  • Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and...

    Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program must consist of the following functions 1. Function getData: This function reads and stores data in the two- dimensional array. 2. Function averageHigh: This function calculates and returns the aver- age high temperature for the year. 3. Function averageLow:...

  • I need this asap. C++ please A A Ap Aa Consolas 14 AaBbCcDd AaBbCcDd AaBbCc Editing...

    I need this asap. C++ please A A Ap Aa Consolas 14 AaBbCcDd AaBbCcDd AaBbCc Editing ste 1 No Spac.. Heading 1 В I 1 Normal x A U A v ab х. Dictate ipboard Font Paragraph Styles Voice Write a program that uses a two-dimensional array to store the highest and lowest temperatures for each month of the year. The program should output the average high, average low, and the highest and lowest temperatures for the year. Your program...

  • The name of the C++ file must be search.cpp Write a program that will read data...

    The name of the C++ file must be search.cpp Write a program that will read data from a file. The program will allow the user to specify the filename. Use a loop that will check if the file is opened correctly, otherwise display an error message and allow the user to re-enter a filename until successful. Read the values from the file and store into an integer array. The program should then prompt the user for an integer which will...

  • Write a program in C++ language that finds the largest number in an array of positive...

    Write a program in C++ language that finds the largest number in an array of positive numbers using recursion. Your program should have a function called array_max that should pass in an int array, and an int for the size of the array, respectively, and should return an int value. As an example, given an array of size 10 with contents: {10, 9, 6, 1, 2, 4, 16, 8, 7, 5} The output should be: The largest number in the...

  • Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that...

    Using basic c++ write 2 separate codes for this assignment. Program #1 Write a program that calculates the average of a group of test scores, where the lowest score in the group is dropped. It should use the following functions. • void getScore() should ask the user for a test score, store it in the reference parameter variable, and validate it. This function should be called by the main once for each of the five scores to be entered. •...

  • 8. (15 marks) Write a complete C program, which uses an array of structures and two...

    8. (15 marks) Write a complete C program, which uses an array of structures and two user defined functions. The program deals with a library database. The program will ask the user to input required information about each book and store it in a structure in a user defined function called get info. The second user defined function display_info will display database information on the screen. The output should look as follows: Book Title Book ld 123456 C Programming 10...

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