Program is in C++. Told that I can no longer use break to end the loop. Here is
ID: 3677487 • Letter: P
Question
Program is in C++.
Told that I can no longer use break to end the loop.
Here is what I have so far:
#include <iostream>
#include <cmath>
using namespace std;
double getrec(double x[], double y[])
{
int count = 0;
while(count < 100) // while loop until count hits 99
{
cout << "Enter a pair of positive x, then y coordinates, separated by spaces, or '0 0' to stop: ";
cin >> x[count]>> y[count];
if(x[count] == 0 && y[count] == 0) break; // breaks loop if (x,y) = (0,0)
count++;
}
return count;
}
double polar(double x, double y)
{
const double toDegrees = 180.0/3.14159265359;
double polar = atan(y/x) * toDegrees;
return polar;
}
//OUTPUT
int main()
{
const int SIZE = 100;
double x[SIZE];
double y[SIZE];
double count = getrec(x,y);
cout<< "Coordinates [inputs ---> polar]:" << endl;
cout<< "================================" << endl;
for(int i =0; i < count; i++)
{
double radius = sqrt((pow(x[i]+2.0,2))+(pow(y[i]+6.0,2)));
double angle = polar(x[i]+2, y[i]+6);
cout<< "(" << x[i]<<", "<<y[i]<<") "<<"---> "<<"("<<radius<<", "<<angle<<")"<< endl;
}
return 0;
}
Now here is the question:
Change your main program to be dealing with files instead of console interaction:
Set up a file called "inputs.dat" in the same directory as your program formatted with one pair of rectangular coordinates per line. Give the x coordinate first, then whitespace (a single space or multiple spaces or a tab will all work the same), and then the y coordinate. Use one of the sets of test data from last week where you know what results to expect.
Remove the interactive input phase from your main program. Instead, change your program to read from any file called "inputs.dat" in the program's working directory and formatted as above and store the data in paraellel arrays. You must handle any file size up to 1000 lines. You may not assume any particular size of file, nor may you determine the file size in advance; you must read until the end of the file.
Additionally, we only want to be working in the first quadrant, so we should validate our input. Check every input you read in to make sure it's in the first quadrant and save only those that are. For those that are not, print to the console an error message reporting the bad input, e.g. "Read point (-20, 7), which is not in Quadrant I...ignoring."
When reading in data, the user doesn't really have any indication what's going on, and with long files, it might take a while. So, after reading every 10th data point, print a status message like "Successfully read 10 lines of input so far," "Successfully read 20 lines of input so far," etc.
You may assume your input file is properly formatted, but you may not assume it exists. If it does not, tell the user, and make sure the rest of your program doesn't do anything weird. (Do not, under any circumstances, prematurely terminate the program.)
We read in from a file, so why not send results to a file. Produce an output file called "outputs.dat" in the program's working directory formatted similarly to the input file, with r first on each line.
The output file doesn't show anything in the console, but give the user a message saying where to find the output.
Have a program setting that determines whether or not to give the console output you programmed last week. It should be something that must able to be turned on or off via setting a constant at compiletime.
Sample Interaction
Here is one example of what your program might print to the console:
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
double polar(double x, double y)
{
const double toDegrees = 180.0/3.14159265359;
double polar = atan(y/x) * toDegrees;
return polar;
}
//OUTPUT
int main()
{
ifstream inFile;
inFile.open("inputs.dat");
ofstream outFile;
outFile.open("outputs.dat");
int count = 0;
double x,y;
while (inFile>>x>>y)
{
count++;
if(count!=0 && count%10==0)
cout<<"Successfully read "<<count<<" lines of input so far."<<endl;
if(x<0 || y< 0){
cout<<"Read point ("<<x<<" , "<<y<<"), which is not in Quadrant I...ignoring."<<endl;
continue;
}
double radius = sqrt((pow(x+2.0,2))+(pow(y+6.0,2)));
double angle = polar(x+2, y+6);
outFile<< "(" << x<<", "<<y<<") "<<"---> "<<"("<<radius<<", "<<angle<<")"<< endl;
}
cout<<"Successfully read "<<count<<" lines of input so far."<<endl;
inFile.close();
outFile.close();
return 0;
}
/*
Input: inputs.dat
3 4
4 -4
5 5
1 2
3 2
1 6
2 1
6 7
1 -2
-3 2
5 5
-2 -2
Output:
Read point (4 , -4), which is not in Quadrant I...ignoring.
Read point (1 , -2), which is not in Quadrant I...ignoring.
Successfully read 10 lines of input so far.
Read point (-3 , 2), which is not in Quadrant I...ignoring.
Read point (-2 , -2), which is not in Quadrant I...ignoring.
Successfully read 12 lines of input so far.
outputs.dat
(3, 4) ---> (11.1803, 63.4349)
(5, 5) ---> (13.0384, 57.5288)
(1, 2) ---> (8.544, 69.444)
(3, 2) ---> (9.43398, 57.9946)
(1, 6) ---> (12.3693, 75.9638)
(2, 1) ---> (8.06226, 60.2551)
(6, 7) ---> (15.2643, 58.3925)
(5, 5) ---> (13.0384, 57.5288)
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.