Question

Write a program that demonstrates use of programmer - defined data structures. Please provide code! Thank you.

Janual line Iranin Note: This program is similar to another recently assigned program, except that it uses struct and an arraAdditional Requirements and Design Considerations: 1. The main program should display a brief description of the program. 2.Sample Output: Below is the output I received from my solution to this assignment. The some information has been obliterated

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

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

temperature.cpp

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

//struct for temp
struct Temperature
{
//variables
string month;
int high;
int low;
};

//load from file
int loadData(ifstream &infile, Temperature t[], int &rows)
{
infile.open("C:/Users/NIROSHINI/Desktop/input.txt");
int n=0;

//if no file, print error msg
if(!infile)
{
cout<<"No input file found";
return 0;
}
//read from file
while(!infile.eof())
{
infile>>t[n].month>>t[n].high>>t[n].low;
n++;
}
rows=n; //set rows
  
cout<<"\nfound "<<n<<" months of data\n";
//print data on screen
for(int i=0;i<rows;i++)
cout<<setw(9)<<t[i].month<<" "<<t[i].high<<" "<<t[i].low<<endl;

infile.close(); //close file
return n; //return row size
}

//return lowest avg temperature
Temperature findLow(Temperature t[],int rows)
{
int low=999;
int index;

//find lowest temp
for(int i=0;i<rows;i++)
{
if(low>(t[i].low+t[i].high)/2){
low=(t[i].low+t[i].high)/2;
index=i;
}
}
return t[index]; //return temp
}

//return highest avg temperature
Temperature findHigh(Temperature t[],int rows)
{
int high=0;
int index;

//find highest temp
for(int i=0;i<rows;i++)
{
if(high<(t[i].low+t[i].high)/2){
high=(t[i].low+t[i].high)/2;
index=i;
}
}
return t[index]; //return temp
}

int main()
{
ifstream infile; //file
struct Temperature t[12],t1,t2; //temp obj
int rows; //rows

cout<<"This program finds the warmest and coldest months as listed in a data file.";
cout<<"\n<loadData called>";

rows=loadData(infile,t,rows); //load from file

//if no data close the program
if(rows==0){
cout<<"No data available";
return 0;
}

cout<<"\n<averageHigh called>";
t1=findHigh(t,rows); //find the highest avg temp
cout<<"\nThe highest average temperature was "<<((t1.high+t1.low)/2)<<" in "<<t1.month<<endl;

//find the lowest avg temp
cout<<"\n<averageLow called>";
t2=findLow(t,rows);
cout<<"\nThe lowest average temperature was "<<((t2.high+t2.low)/2)<<" in "<<t2.month<<endl;

return 0;
}

output:

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

//for any clarification please do comments. if you found this solution useful, please give me thumbs up

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

    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...

  • 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 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...

  • ********** PLEASE PROVIDE IN JAVA & NEW SET OF ALGORITHM For this lab use the program...

    ********** PLEASE PROVIDE IN JAVA & NEW SET OF ALGORITHM For this lab use the program at the bottom to sort an array using selection sort. Write a selection sort to report out the sorted low values: lowest to highest. Write another to report out the sorted high values – highest to lowest. Last but not least, the program should output all the values in the array AND then output the 2 requested sorted outputs. -----> MY OLD PROGRAM BELOW...

  • Main topics: Files Program Specification: For this assignment, you need only write a single-file ...

    C program Main topics: Files Program Specification: For this assignment, you need only write a single-file C program. Your program will: Define the following C structure sample typedef struct int sarray size; the number of elements in this sanple's array floatsarray the sample's array of values sample Each sample holds a variable number of values, all taken together constitute a Sample Point Write a separate function to Read a file of delimited Sample Points into an array of pointers to...

  • 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...

  • Use basic C++ 3. A text file, superstars.txt, contains statistics on cricket players. For each player,...

    Use basic C++ 3. A text file, superstars.txt, contains statistics on cricket players. For each player, the file contains the player's first name, last name, number of matches played, total number of runs scored, number of times the player scored 100 runs in a match, and the number of wickets taken. The last row of data in the file contains the word "END" only. Some rows are shown below 30 11867 164 Chanderpaul Shivnarine 34 11912 130 Lara Brian 73...

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