Question

In this project you will create a console C++ program that will have the user to...

In this project you will create a console C++ program that will have the user to enter Celsius temperature readings for anywhere between 1 and 365 days and store them in a dynamically allocated array, then display a report showing both the Celsius and Fahrenheit temperatures for each day entered. This program will require the use of pointers and dynamic memory allocation.

Getting and Storing User Input: For this you will ask the user how many days’ worth of temperature readings they wish to enter. This value must be numeric and between 1 and 365. Do not forget to check for non-numeric input.

Array allocation: Once you have determined how many temperature readings to store, allocate the appropriate amount of memory to a pointer using the new[] operator. This should happen in a try {} block. If the memory allocation fails at run-time, the new[] operator throws a bad_alloc exception. Include a catch{} block that displays the error message “Error allocating memory” to the user if this happens. Remember that all memory allocated with the new[] operator should be freed when you are done with it. The final statement of your try block should delete[] your pointer.

Getting valid temperature readings: The temperatures the user enters should fall between the range -90.0°C and +60.0°C. Do not accept an input value outside of that range.

Processing: Converting Celsius to Fahrenheit To generate the required output, you will need to convert each Celsius temperature to Fahrenheit. Given this is a fairly generic task that could be re-used in another program, you should create a function to do this. The actual calculation is straight forward; the function body may only need to be one line of code, so the function should be ‘inline’. Keep in mind that a processing function should only do processing, not input or output. Pass the Celsius temperature in as a parameter and send the Fahrenheit temperature back using a ‘return’ statement.

Output: This Week’s Temperature Report Use an Output Function: Write a function that will display the temperatures entered in Celsius and Fahrenheit formatted as above. You must pass this function a pointer to the array storing the Celsius temperatures and the number of items in that array and you are required to use pointer arithmetic to move from one index of the array to the next. You may not use the array subscript operator in this function. Formatting Hints: Look up the ASCII code value for the degrees symbol. Each ‘column’ of the report is right aligned ten characters wide.

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

#include<iostream>

#include<new>

using namespace std;

double convertTemp(double celTemp);

void displayTemp(double *tmp,int size);

int main()

{

double * temp = NULL,cel;

int numberOfDays;

x:

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

cin >> numberOfDays;

while (!cin)

{

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << "Enter numeric value: ";

cin >> numberOfDays;

}

if (numberOfDays < 1 || numberOfDays>365)

goto x;

try

{

//allocate dynamic memory

temp = new double[numberOfDays];

cout << "Enter temperature in Celsius: ";

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

{

cin >> cel;

while (!(cel >= -90 && cel <= 60))

{

cout << "Enter valid temperature: " << endl;

cin >> cel;

}

*(temp + i)=cel;

}

//output the temperature

displayTemp(temp,numberOfDays);

}

catch (std::bad_alloc& ba)

{

std::cerr << "Error allocating memory.... " << ba.what() << '\n';

throw;

}

//delete the entire array

delete[] temp;

//to hold the output screen

system("pause");

return 0;

}

double convertTemp(double celTemp)

{

return ((9 * celTemp) / 5) + 32;

}

void displayTemp(double *tmp,int size)

{

char degree =248 ;

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

{

cout << *(tmp+i) << degree<<"F ";

int temp = convertTemp(*(tmp+i));

cout << temp << degree << "C " << endl;;

}

}

//output

//Need any help regarding this solution just leave a comment ................. Thanks

Add a comment
Know the answer?
Add Answer to:
In this project you will create a console C++ program that will have the user to...
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 in C that will convert all 12 months' average temperature from Celsius to...

    Write a program in C that will convert all 12 months' average temperature from Celsius to Fahrenheit. You need to create an array named aye jump and prompt the user to input average temperature in Celsius for all 12 months. After that, you need to develop a user temperatures from Celsius to Fahrenheit and pass the array as reference and convert all temperatures from Celsius to Fahrenheit. Next, compute the average temperature of the year and assign it to a...

  • Can you help me to create this program, please? Prompt the user for the user for...

    Can you help me to create this program, please? Prompt the user for the user for a number of degrees in Fahrenheit as an int. That number should be passed to a function named ffocREFO. This function will do the necessary calculation to convert the passed temperature to degrees Celsius as a double. The function MUST have a void return type and have the following prototype: void ffccREF(double& celsius, int Faren); In your main, display both the entered temperature and...

  • Write a C++ program that prompts a user to enter a temperature in Fahrenheit as a...

    Write a C++ program that prompts a user to enter a temperature in Fahrenheit as a real number. After the temperature is entered display the temperature in Celsius. Your program will then prompt the user if they want to continue. If the character n or N is entered the program will stop; otherwise, the program will continue. After your program stops accepting temperatures display the average of all the temperatures entered in both Fahrenheit and Celsius. Be sure to use...

  • Create a program in Python that will allow the user to enter a temperature in Fahrenheit...

    Create a program in Python that will allow the user to enter a temperature in Fahrenheit which will then be converted to degrees Celsius. The program will keep asking the user for a Fahrenheit temperature until the user enters Q to quit. After each conversion the program needs to print out the degrees Celsius. The input prompts for this problem need to look like the following: Degrees Fahrenheit: Continue: For these input prompts watch the capitalization and spacing. There are...

  • Temperature Converter Create a temperature conversion program that will convert the following to Fahrenheit: Celsius Kelvin...

    Temperature Converter Create a temperature conversion program that will convert the following to Fahrenheit: Celsius Kelvin Newton Your program should take a character which represents the temperature to convert from and a value to be converted to using the following specification: C - Celsius K - Kelvin N - Newton In addition your program should take in the following character to exit the program: X - eXit the program The numeric input for your program should be of type double....

  • using C language Create an array of doubles with 5 elements. In the array prompt the...

    using C language Create an array of doubles with 5 elements. In the array prompt the user to enter 5 temperature values (in degree Fahrenheit). Do this within main. Create a user defined function called convert2Cels. This function will not return any values to main with a return statement. It should have parameters that include the temperature array and the number of elements. The function should convert the values for Fahrenheit to Celsius and save them back into the same...

  • Write a program that would ask the user to enter an input file name, and an...

    Write a program that would ask the user to enter an input file name, and an output file name. Then the program reads the content of the input file, and read the data in each line as a number (double). These numbers represent the temperatures degrees in Fahrenheit for each day. The program should convert the temperature degrees to Celsius and then writes the numbers to the output file, with the number of day added to the beginning of the...

  • You can vary it as long as the fahr and cels temperatures line up vertically and everything is clearly labeled. This program involves inputting a set of Fahrenheit temperatures and performing a set...

    You can vary it as long as the fahr and cels temperatures line up vertically and everything is clearly labeled. This program involves inputting a set of Fahrenheit temperatures and performing a set of operations on them: The number of temperatures to input is determined by the user at the beginning of the program. You must ask them to enter the number of temperatures that they will be typing in. This number must be between 1 and 30 (inclusive.) If...

  • Write a program in C++ that gives the temperature of earth at a depth. It must...

    Write a program in C++ that gives the temperature of earth at a depth. It must be in C++ and follow the information below: Problem description Write a program to take a depth in kilometers inside the earth as input data; calculate and display the temperature at this depth in both degrees Celsius and degree Fahrenheit. The formulas are: Celsius temperature at depth in km: celsius = 10 x depth(km) + 20 Convert celsius to fahrenheit: fahrenheit = 1.8 x...

  • Can someone help me out? c# For this Project, you will create a function called getTemperatures()....

    Can someone help me out? c# For this Project, you will create a function called getTemperatures(). The getTemperatures() function will have one parameter called temperatures[].Within the getTemperatures() function you will populate the temperatures array with the following values: 10,15,20,25,30,32,40,45,50,55,60,65,70. Your main program will traverse through the array and display the Fahrenheit and Celsius equivalents. Expected output 10 degrees Fahrenheit is -12 degrees Celsius 10 degrees Celsius is 50 degrees Fahrenheit 15 degrees Fahrenheit is -9 degrees Celsius 15 degrees Celsius...

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