Question

Description Write C++ code that correctly input and output information. (Moving data to and from the...

Description Write C++ code that correctly input and output information. (Moving data to and from the screen and to and from text files. Also inputting predefined functions, etc., from included libraries.)

Problem Description

Consider a data file that has geometrical angle values in degrees. Angle values may be on one line, several lines, or any other white space separated format. Fig. 1 below shows one possible format, but other formats are possible.

12.9 100.8 270.5
300.6 120.8

There are no whitespaces in the file after the last digit. Thus, last character in the file is an EOF character. File is a text (ASCII) file, strictly created in notepad software on windows or textwrangler on Mac.

Write a C++ program, which using loops and cmath library does followings:

1. Reads one angle at a time from the input file.

2. Converts the angle to radians. (Other conversions or corrections may be needed).

3. Computes the followings for the angle read (assume that angle is A): sin(A), cos(A), tan(A), cot(A), sec(A), and cosec(A)

4. Program writes to console and output file a table such as below (without vertical lines):

Angle(degrees) Angle(radians) sin cos tan cot sec cosec
12.9000 0.2251 0.2232 0.9748 0.2290 4.3672 1.0259 4.4802

Input and output files name CANNOT be hard coded. The program must ask for both, full path to input and output file names. Instructors have freedom to use tabular output that is in either of the three formats: left aligned, right aligned, or center aligned. The program can be done just in main function, or student can use user-defined function. The use of user-defined function is not part of SLO. The trigonometric values can be positive or negative based on the value of the angle, as per rules given in the table below:

Quadrant # Clockwise angle from origin and horizontal line Signs of various trigonometric functions
1 0 to 90 All functions are positive
2 >90 but <=180 sin and cosec are positive, but all other functions are negative
3 >180 but <=270 tan and cot are positive, but all others are negative
4 >270 but<=360 cos and sec are positive, but all others are negative.

Thus, programmer has to, programmatically handle the angles larger than 90 and assign proper positive and negative signs to results.

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


// C++ program to read angles from file in degrees and convert them to radians and calculate and output the result of trignometric functions to output file
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
using namespace std;

# define PI 3.14159

int main() {

   string inputfile, outputfile;
   // input of input and output file path
   cout<<"Enter the full path to input file: ";
   getline(cin,inputfile);
   cout<<"Enter the full path to output file: ";
   getline(cin,outputfile);
   // open the input file
   ifstream fin(inputfile.c_str());
   // if file can be opened
   if(fin.is_open())
   {
       // create an output file
       ofstream fout(outputfile.c_str());
       double angle_degree, angle_radians;
       // output the header
       cout<<left<<setw(20)<<"Angle(degrees)"<<left<<setw(20)<<"Angle(radians)"<<left<<setw(10)<<"sin"<<left<<setw(10)<<"cos"
               <<left<<setw(10)<<"tan"<<left<<setw(10)<<"cot"<<left<<setw(10)<<"sec"<<left<<setw(10)<<"cosec"<<endl;
       fout<<left<<setw(20)<<"Angle(degrees)"<<left<<setw(20)<<"Angle(radians)"<<left<<setw(10)<<"sin"<<left<<setw(10)<<"cos"
                       <<left<<setw(10)<<"tan"<<left<<setw(10)<<"cot"<<left<<setw(10)<<"sec"<<left<<setw(10)<<"cosec"<<endl;

       cout<<fixed<<setprecision(4);
       fout<<fixed<<setprecision(4);
       // read till the end of file
       while(!fin.eof())
       {
           fin>>angle_degree; // read the angle
           // convert to radians
           angle_radians = (PI*angle_degree)/180;
           cout<<left<<setw(20)<<angle_degree<<left<<setw(20)<<angle_radians;
           fout<<left<<setw(20)<<angle_degree<<left<<setw(20)<<angle_radians;
           // output the result to file and console
          
       cout<<left<<setw(10)<<sin(angle_radians)<<left<<setw(10)<<cos(angle_radians)<<left<<setw(10)<<tan(angle_radians)
                       <<left<<setw(10)<<(1/tan(angle_radians))<<left<<setw(10)<<(1/cos(angle_radians))<<left<<setw(10)<<(1/sin(angle_radians))<<endl;
               fout<<left<<setw(10)<<sin(angle_radians)<<left<<setw(10)<<cos(angle_radians)<<left<<setw(10)<<tan(angle_radians)
                   <<left<<setw(10)<<(1/tan(angle_radians))<<left<<setw(10)<<(1/cos(angle_radians))<<left<<setw(10)<<(1/sin(angle_radians))<<endl;
          
}

       // close the files
       fin.close();
       fout.close();
   }else
       cout<<"Unable to open file : "<<inputfile<<endl;


   return 0;
}
//end of program

Output:

Input file:

Console:

Output file:

Add a comment
Know the answer?
Add Answer to:
Description Write C++ code that correctly input and output information. (Moving data to and from the...
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
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