Question

The program will need to accept two input arguments: the path of the input file and...

The program will need to accept two input arguments: the path of the input file and the path of the output file. See etc/cpp/example.ifstream.cpp for the basis of how to do this. Once the input and output file paths have been received, the program will need to open the input and output files, read and process each input file line and create a corresponding output file line, close the input and output files, and then create and output some summary lines to the console. Note that the summary lines can be output to the console before or after the input and output files are closed, but it would be best to output them to the console after the files have been closed. Each input line will either be a comment line or shape specification line. Your program will need to ignore the comments lines and process the shape specification lines. There will be three types of shapes: a circle, a triangle, and a square. Each shape specification lines will be in this form, with the different values separated by spaces: The shape identifier will be an integer, the shape type will be a string, and the shape measure will be a double. So, for example, you might encounter lines like this: # this line is a comment and can be ignored…it starts with the pound/hash character 1 circle 3.51 2 triangle 4.62 3 square 5.73 For comments lines, lines that start with the pound/hash character, you can ignore them. For the other lines, you should calculate the shape’s area and create an output line in this form:

<shape identifier> <shape type> < shape area> For circle shape lines, the shape measure represents the radius of a circle (r) and thus the area of the circle is A = PI * r * r (where PI can be a defined constant double of 3.14). For triangle shape lines, the shape measure represents the length of the side of an equilateral triangle (a) and thus the area is A = sqrt(3) / 4 * a * a. For square shape lines, the shape measure represents the length of one side of the square (a) and thus the area is A = a * a. So, for the input file lines above, the output file lines would look like this (with the area values formatted with a precision of three decimal places): 1 circle 38.685 2 triangle 9.242 3 square 32.833

And, the summary lines that should be sent to the console, if the example above represents all of the shape specifications, would look like this:<your name> - CS 1336 – Assignment 25 Number of circles: 1 Number of triangles: 1 Number of squares: 1 Average circle area: 38.685 Average triangle area: 9.242 Average square area: 32.833 Average shape area: 26.920 In case it is not obvious, the average circle area should be calculated by summing all of the individual circle areas and then dividing by the number of circles. And, the same pattern should be used for the average triangle area and the average square area. Finally, the average shape area should be calculated by adding all of the areas together and dividing by the total number of shapes.

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#include<iostream>

#include<fstream>

#include<math.h>

#include<iomanip>

#define PI 3.14

using namespace std;

int main(){

              //getting and storing input / output file names

              string input,output;

              cout<<"Enter input file name: ";

              cin>>input;

              cout<<"Enter output file name: ";

              cin>>output;

              //opening input file

              ifstream inFile(input.c_str());

              //exiting if file not found

              if(!inFile){

                           cout<<input<<" not found"<<endl;

                           return 0;

              }

              //opening output file, exiting if can't open

              ofstream outFile(output.c_str());

              if(!outFile){

                           cout<<output<<" cant be opened to write"<<endl;

                           return 0;

              }

             

              //initializing required variables

              double circleArea=0,triangleArea=0,sqArea=0,measure;

              int numCircles=0,numTriangles=0,numSq=0, identifier;

              string line,type;

             

              //setting a fixed precision of 3 digits after decimal point

              outFile<<setprecision(3)<<fixed;

              cout<<setprecision(3)<<fixed;

             

              //looping until end of file

             

              while(true){

                           //discarding the line if it starts with #

                           if(inFile.peek()=='#'){

                                         getline(inFile,line);

                           }else if(!inFile.good()){

                                         //end of file, breaking loop

                                         break;

                           }

                           else if(inFile>>identifier){

                                         //read the identifier, reading type and measure

                                         inFile>>type;

                                         inFile>>measure;

                                         double area=0;

                                         //finding type, calculating its area, appending to total area

                                         if(type=="circle"){

                                                       area=PI*measure*measure;

                                                       circleArea+=area;

                                                       numCircles++;

                                         }else if(type=="triangle"){

                                                       area=(sqrt(3.0)/4.0)*(measure*measure);

                                                       triangleArea+=area;

                                                       numTriangles++;

                                         }else if(type=="square"){

                                                       area=measure*measure;

                                                       sqArea+=area;

                                                       numSq++;

                                         }

                                         //writing to output file

                                         outFile<<identifier<<" "<<type<<" "<<area<<endl;

                                         inFile.ignore(); //ignoring newline character

                           }

              }

              //closing files

              inFile.close();

              outFile.close();

             

              //finding averages

              double avgCircleArea=(double)circleArea/numCircles;

              double avgTriangleArea=(double)triangleArea/numTriangles;

              double avgSqArea=(double)sqArea/numSq;

              double avgShapeArea=(double) (circleArea+triangleArea+sqArea)/(numCircles+numTriangles+numSq);

             

              //displaying stats

              cout<<"<your name> - CS 1336 - Assignment 25"<<endl;

              cout<<"Number of circles: "<<numCircles<<endl;

              cout<<"Number of triangles: "<<numTriangles<<endl;

              cout<<"Number of squares: "<<numSq<<endl;

             

              cout<<"Average circle area: "<<avgCircleArea<<endl;

              cout<<"Average triangle area: "<<avgTriangleArea<<endl;

              cout<<"Average square area: "<<avgSqArea<<endl;

              cout<<"Average shape area: "<<avgShapeArea<<endl;

              return 0;

}

/*OUTPUT*/

Enter input file name: shapes.txt

Enter output file name: output.txt

<your name> - CS 1336 - Assignment 25

Number of circles: 1

Number of triangles: 1

Number of squares: 1

Average circle area: 38.685

Average triangle area: 9.242

Average square area: 32.833

Average shape area: 26.920

/*output.txt*/

1 circle 38.685

2 triangle 9.242

3 square 32.833

Add a comment
Know the answer?
Add Answer to:
The program will need to accept two input arguments: the path of the input file and...
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
  • I am confused on how to read valid lines from the input file. I am also...

    I am confused on how to read valid lines from the input file. I am also confused on how to count the averages from these valid input lines to keep running the total from these valid lines. If you could show an example of an input and output file that would be amazing as well. Purpose        Learn how to use Java input/output. Due Date       Per the Course at a Glance. Can be resubmitted. Submissions           In this order: printed copy of...

  • Please use C++ and Please do all file separate separately. And also I need output too....

    Please use C++ and Please do all file separate separately. And also I need output too. thanks. Please do it complete work. ShapeNodePoly.cpp Avaliable from: Wednesday, July 27, 2016, 10:20 AM Requested files: Point.h, Point.cpp, Shape.h, Shape.cpp, Polygon.h, Polygon.cpp, Ellipse.h, Ellipse.cpp, ShapeNodePoly.cpp, ShapeNodePoly_test.cpp (Download) Type of work: Individual work In this assignment, you will create a class that can behave as any of the shapes listed below. You will use object inheritance to enable all shapes to be managed using...

  • C++ There are to be two console inputs to the program, as explained below. For each input, there is to be a default val...

    C++ There are to be two console inputs to the program, as explained below. For each input, there is to be a default value, so that the user can simply press ENTER to accept any default. (That means that string will be the best choice of data type for the console input for each option.) The two console inputs are the names of the input and output files. The default filenames are to be fileContainingEmails.txt for the input file, and...

  • DATA PROGRAMMING: PYTHON Be sure to use sys.argv NOT input. Write a program and create a...

    DATA PROGRAMMING: PYTHON Be sure to use sys.argv NOT input. Write a program and create a function called inch2cm that takes one number in inches as a parameter, converts it to the centimeters and prints the result. The program output is shown below. Input: 14 Output: 14 inches = 35.56 centimeter Write a program and create the following functions: shapes(): takes the shape name and a number as parameters, and calls the proper function to calculate the area. areaCircle(): take...

  • Q2) Interface Create a program that calculates the perimeter and the area of any given 2D...

    Q2) Interface Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamically assign the appropriate calculation for the given shape. The type of shapes are the following: • Quadrilateral 0 Square . Perimeter: 4xL • Area:LXL O Rectangle • Perimeter: 2(L+W) • Area:LxW Circle Circumference: I x Diameter (TT = 3.14) Area: (TT xD')/4 Triangle (assume right triangle) o Perimeter: a+b+c O Area: 0.5 x base x height (hint: the base...

  • FOR JAVA Write a program that takes two command line arguments: an input file and an...

    FOR JAVA Write a program that takes two command line arguments: an input file and an output file. The program should read the input file and replace the last letter of each word with a * character and write the result to the output file. The program should maintain the input file's line separators. The program should catch all possible checked exceptions and display an informative message. Notes: This program can be written in a single main method Remember that...

  • ( IN JAVA): Write a program that reads each line in a file, and writes them...

    ( IN JAVA): Write a program that reads each line in a file, and writes them out in reverse order into another file. This program should ask the user for the name of an input file, and the name of the output file. If the input file contains: Mary had a little lamb Its fleece was white as snow And everywhere that Mary went The lamb was sure to go Then the output file should end up containing: The lamb...

  • Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle,...

    Write a Java program that prompts user to select one of the five shapes(Rectangle, Square, Triangle, Circle, and Parallelogram), then calculate area of the shape,must have input validation and uses more of java library like math class, wrapper class, string methods, use formatted output with the printf method. Finally, create a pseudo-code statement and flowchart

  • C++ (1) Write a program to prompt the user for an input and output file name....

    C++ (1) Write a program to prompt the user for an input and output file name. 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. For example, (user input shown in caps in first line, and in second case, trying to write to a folder which you may not have write authority in) Enter input filename: DOESNOTEXIST.T Error opening input file: DOESNOTEXIST.T...

  • 1. Write a program that determines the area and perimeter of a square. Your program should...

    1. Write a program that determines the area and perimeter of a square. Your program should accept the appropriate measurement of the square as input from the user and display the result to the screen. Area of Square = L (squared) Perimeter of Square = 4 * L 2.  Write a program that determines the area and circumference of a circle. Your program should accept the appropriate measurement of the circle as input from the user and display the result to...

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