Question

In: Computer Science

Write C++ program as described below. Call this programChill.cpp. In April 2018 the lowest and the...


Write C++ program as described below. Call this programChill.cpp.

In April 2018 the lowest and the highest temperatures ever recorded in Ohio are 25F and 81F, respectively. The wind chill factor is the perceived temperature when taking temperature and wind into account. The standard formula for wind chill factor (from the US Weather Service) is:

0.0817 * (3.71 * sqrt(Wind) + 5.81 – 0.25 * sqrt(Wind)) * (Temp – 91.4) + 91.4

where ‘sqrt’ is the square root, Wind is the current wind speed in limes per hour and Temp is the current temperature in Fahrenheit.

You must write a program that:

  1. Creates a file with some randomly generated temperatures.
  2. Asks the user for a wind speed .
  3. Generates an output file that contains the wind chill factor of each temperature stored in the input file.

Here are the details.

First, write a function that generates 30 integer numbers, which represent hypothetically the temperature of the 30 days of April in Ohio, and stores all the generated temperature in an output file A. The temperatures generated must range between 25F and 81F. The file Amust contain nothing but integer numbers separated by a blank or a line break. The name of the file A should be given by the user and read from the keyboard. Call this functiongentemp.cpp.

Second, write the program Chill.cpp that prompts the user for the wind speed in miles per hour, reads the temperatures stored in file A and write in a file B the wind chill factor of those temperatures. The name of the file Bcan be chosen by you.

To generate integer numbers, use the functions rand() and srand() defined in <cstdlib>.

rand() - Returns a pseudo-random integral number in the range 0 to RAND_MAX

(Note: RAND_MAX is usually equal to 32767 but it may vary between cstlib library implementation).

A typical way to generate pseudo-random numbers in a determined range using rand() is to use the modulo of the returned value by the range span and add the initial value of the range:
For example:

( value % 100 ) is in the range 0 to 99
( value % 100 + 1 ) is in the range 1 to 100
( value % 30 + 1985 ) is in the range 1985 to 2014

Before generating pseudo-random numbers, use a seed to generate the series. The seed is initialized by using the function srand().

Here is a small example that generates 20 pseudo-random integers.

#include <cstdlib>

#include <iostream>

using namespace std;

int main()

{

         int random_integer;

         srand(9);

         for(int index=0; index<20; index++)

         {

                  random_integer = (rand()%100)+1;

                  cout << random_integer << endl;

         }

         return 0;

}


Solutions

Expert Solution

The function for generating temperature and storing it in user defined file is:

void generate()
{   srand(11);
    int num;
    char fname[10];
        ofstream fout;
        cout<<"Enter the name of the file:";
        scanf("%s",fname);
        fout.open(fname);
        for(int i=1;i<=30;i++)
       {num=rand()%(81-25+1)+25;
                fout<<num<<endl;}
        fout.close();
}

This function creates random integers between 25 and 81(inclusive) and stores it in the user defined file.

Now the code for chill.cpp is


#include<iostream>
#include<fstream>
#include<stdio.h>
#include<math.h>

using namespace std;

void gentemp()
{   srand(11);
    int num;
    char fname[10];
        ofstream fout;
        cout<<"Enter the name of the file:";
        scanf("%s",fname);
        fout.open(fname);
        for(int i=1;i<=30;i++)
       {num=rand()%(81-25+1)+25;
                fout<<num<<endl;}
        fout.close();
        cout<<"\n"<<fname<<" file has been created\n";
}

int main()
{   gentemp();
    int num,temperature[30];
    char fname[10],gt[10];
    float wind, windchill; 
    cout<<"\nEnter the filename to open(for temperature retrieval):";
    scanf("%s",gt); //the file name for getting the temperature values
    ifstream file(gt);

    int count = 0;
    int x;

    while (count < 30 && file >> x)
        temperature[count++] = x; //storing the values retrieved from file to array temperature


    ofstream fout; //For storing the windchill values calculated
        cout<<"\nEnter the name of the file to store the values of windchill:";
        scanf("%s",fname);
        fout.open(fname);

        cout<<"\nEnter the value of wind(miles per hour)"; 
        cin>>wind; 

        for(int i=1;i<30;i++)
       {
        windchill=0.0817*(3.71*sqrt(wind)*5.81-0.25*sqrt(wind))*(temperature[i]-91.4)+91.4;
                fout<<windchill<<endl;}
        fout.close(); //closing the resulting file

    return 0;
}

This code calls the gentemp() function initially , And the random 30 numbers from 25 to 81 are created and written to the filename provided by the user.

Then we created two float variable wind and windchill.

wind is for user to input the wind speed in miles per hour and windchill variable is for calculating the windchill using the given equation and storing it in B.txt file.

The name of the file to store the calculated value of windchill has to be provided by the user and then the values are written into that file.

Comments are added for better understanding of the program.

The screenshots are attached for your reference:

Hope it helps!!!


Related Solutions

Write a C program with call to functions to produce the output given below. // the...
Write a C program with call to functions to produce the output given below. // the requirements are that there should be 5 files; intList.h, intList.c, hw3.h, hw3.c, and main.c. please only C and use Linked List. thank you. For the 5 different files, he wants it this way: 1) main.c This file just consists of the main() function, which only consists of the displayClassInfo() function call, and the runMenuHw3() function call. 2) intList.h This file would have the IntNode...
Problem: Write a C++ program that will implement and test the five functions described below that...
Problem: Write a C++ program that will implement and test the five functions described below that use pointers and dynamic memory allocation. The Functions: You will write the five functions described below. Then you will call them from the main function, to demonstrate their correctness. 1. minimum: takes an int array and the array's size as arguments. It should return the minimum value of the array elements. Do not use square brackets anywhere in the function, not even the parameter...
Write a C or C++ program using the fork() system call function. You will need to...
Write a C or C++ program using the fork() system call function. You will need to create 3 processes – each process will perform a simple task. Firstly, create an integer "counter" initialized to a random value between 1 and 100. Print this number to the console. This can be done by: Including the stdio.h and stdlib.h libraries Using the rand() function to generate your randomly generated number The main thread consists of the parent process. Your job is to...
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in...
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in zyBooks sections 9.4 and 9.5. Prompt the user for the location of a sequence of numbers, via an external file or data entry by the user. If you choose data entry, prompt the user for the number of values and read them into a data structure of your choice. Then use the mergesort algorithm to sort them in ascending order. Finally, prompt for a...
Write a program calls each of the three methods described below and prints out the result...
Write a program calls each of the three methods described below and prints out the result of the two methods which return value reverse- a void method that reverses the elements of the array. Print the array before you call the method and after call the method. getMin- an int method that returns the smallest value in the array Sample output array: 22,34,21,35,12,4,2,3,99,81 array in reverse: 81,99,3,2,4,12,35,21,34,22 the smallest number is 2
Write a C program using system call I/O to ****NOTE YOU MUST USE SYSTEM CALL I/O,...
Write a C program using system call I/O to ****NOTE YOU MUST USE SYSTEM CALL I/O, meaning STANDARD I/O IS NOT ALLOWED a) open an existing text file passed to your program as a command-line argument, then b) display the content of the file, c) ask the user what information he/she wants to append d) receive the info from the user via keyboard e) append the info received in d) to the end of the file f) display the updated...
How do I write a C++ program to call a frequency table from a csv file,...
How do I write a C++ program to call a frequency table from a csv file, using vector? Data given is in a csv file. Below is part of the sample data. Student ID English Math Science 100000100 80 90 90 100000110 70 60 70 100000120 80 100 90 100000130 60 60 60 100000140 90 80 80
Done in C++, Write a program to read the input file, shown below and write out...
Done in C++, Write a program to read the input file, shown below and write out the output file shown below. Use only string objects and string functions to process the data. Do not use c-string functions or stringstream (or istringstream or ostringstream) class objects for your solution. Input File Cincinnati 27, Buffalo 24 Detroit 31, Cleveland 17 Kansas City 24, Oakland 7 Carolina 35, Minnesota 10 Pittsburgh 19, NY Jets 6 Philadelphia 31, Tampa Bay 20 Green Bay 19,...
In the space provided below write a C ++ program that computes the total amount of...
In the space provided below write a C ++ program that computes the total amount of money you are depositing in your bank account. Your program does this by asking you to first enter the number and amount of each check you are depositing, and then it asks you to enter the type of cash bills being deposited and how many of each type, also the types of coins being deposited, and the number of each coin type.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT