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
  • **This is for the C Language. Trigonometric Calculator Specification You are required to write a program...

    **This is for the C Language. Trigonometric Calculator Specification You are required to write a program that calculates and displays values of the trigonometric functions sine, cosine, and tangent for user-supplied angular values. The basic program must comply with the following specification. 1. When the program is run, it is to display a short welcome message. TRIG: the trigonometric calculator 2. The program shall display a message prompting the user to enter input from the keyboard. Please input request (h-help,...

  • this is a C++ program! You will write a program that performs the following tasks for...

    this is a C++ program! You will write a program that performs the following tasks for a simple mathematical calculator: (1) Open a user-specified file for input. Prompt for the name of the file, read it into a string variable, echo print it to the terminal and then open it. If the file is not opened, enter into a loop that prints out an error message, resets the input file stream variable (see the hints section), obtains a new file...

  • This assignment tests your ability to write C programs that handle keyboard input, formatted console output,...

    This assignment tests your ability to write C programs that handle keyboard input, formatted console output, and input/output with text files. The program also requires that you use ctype functions to deal with the logic. In this program, you will input from the user two characters, which should both be letters where the second letter > the first letter. You will then input a file character-by-character and output those letters that fall between the two characters (inclusively) to an output...

  • Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The progra...

    Please write this in C. Write this code in Visual Studio and upload your Source.cpp file for checking (1) Write a program to prompt the user for an output file name and 2 input file names. The program should check for errors in opening the files, and print the name of any file which has an error, and exit if an error occurs opening any of the 3 For example, (user input shown in caps in first line) Enter first...

  • Write a C++ program to get an input string from the user, and output the string...

    Write a C++ program to get an input string from the user, and output the string in uppercase letters. Use a dynamic character array to store the string. ?(this can be from any input file, there is no specific file.)

  • The program should be in C programming The law of sines is an equation relating the...

    The program should be in C programming The law of sines is an equation relating the lengths of the sides of an arbitrary triangle to the sines of its angles. Such that if we have the following triangle: sin a sin ß sin y A B C B Write a program that takes the values of angle a, angle B and length of A from a user. These input values are passed to a function find which calculates the angle...

  • Consider the following C++ program. It prints a small table containing sin and cos values from...

    Consider the following C++ program. It prints a small table containing sin and cos values from 0 to 360 degrees. #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() { int step = 20; cout << setw(10) << "degrees" << setw(10) << "cos" << setw(10) << "sin" << endl; for (int degrees = 0; degrees <= 360; degrees += step) { cout << setw(10) << degrees << setw(10) << setprecision(5) << fixed << cos(degrees) << setw(10) << setprecision(5)...

  • write a c++ code to read multiple integers from input.dat until the end of file. Edit...

    write a c++ code to read multiple integers from input.dat until the end of file. Edit input.dat and put several integers in it. Compile and execute the code then check output.dat to verify all the integers are in there. Update the code to check to see if input.dat exists before reading from it. If it doesn’t exist print an error message (and don’t read!). Compile and execute your code. Delete input.dat and output.dat and execute – did you see your...

  • //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which...

    //I NEED THE PROGRAM IN C LANGUAGE!// QUESTION: I need you to write a program which manipulates text from an input file using the string library. Your program will accept command line arguments for the input and output file names as well as a list of blacklisted words. There are two major features in this programming: 1. Given an input file with text and a list of words, find and replace every use of these blacklisted words with the string...

  • Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_Student...

    Student ID: 123 Write a C+ program with the following specifications: a. Define a C++ function (name it function_StudentlD where StudentID is your actual student ID number) that has one integer input (N) and one double input (x) and returns a double output S, where N S = n 0 and X2 is given by 0 xeVn n 0,1 Хл —{2. nx 2 n 2 2 m2 x2 3 (Note: in the actual quiz, do not expect a always, practice...

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