Program is in C++ Key Concepts: loops, methods, parameter passing, arrays, paral
ID: 3677294 • Letter: P
Question
Program is in C++
Key Concepts: loops, methods, parameter passing, arrays, parallel arrays, good programming practices
Tasks
This project consists of 4 tasks:
Task 1
Write a method that takes as input parameters a pair of Cartesian coordinates and which sets as output parameters a pair of polar coordinates corresponding to the same point as the input Cartesian coordinates. Use degrees for output angles.
Task 2
Write a main program that:
prompts a user to input several pairs of positive Cartesian coordinates, one pair per line and separated by spaces. The user may enter up to 50 pairs, and the user will say he/she is done by inputting the coordinate pair (0, 0). Store the pairs in a pair of parallel arrays as you read them in.
shifts all coordinates to the right by 4 units and up by 7 units.
converts all shifted coordinate pairs to polar coordinates by calling the method you wrote.
prints lines showing both the input coordinates and output coordinates as ordered pairs in the style of this example: (4, 1) ---> (11.3137, 45)
Task 3
After successfully compiling and testing your program, change it to accept up to 100 coordinate pairs instead of up to 50. Also change the coordinate shift to be up 6 and to the right by 2.
Task 4
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 rfirst 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:
Read point (-4, 3), which is not in Quadrant I...ignoring.
Successfully read 10 lines of input so far.
Read point (-1, -3), which is not in Quadrant I...ignoring.
Successfully read 20 lines of input so far.
Data has been output to file outputs.dat,
where each line contains r, then theta, with a space between
Explanation / Answer
Task 1:
#include <iostream>
#include <math.h>
using namespace std;
int getrec(double x[], double y[]);
void polar(double x, double y, double& r, double& theta);
void showPolarCoord(double radius, double angle);
const int SIZE = 100;
const double toDegrees = 180.0/3.1415926;
int main()
{
double x[SIZE];
double y[SIZE];
double distance[SIZE];
double angle[SIZE];
double x_same[SIZE];
double y_same[SIZE];
int count = getrec(x,y);
for (int i=0; i < count; i++)
{
x_same[i] = x[i] + 6;
y_same[i] = y[i] + 2;
}
for(int i=0; i < count; i++)
{
polar (x_same[i], y_same[i], distance[i], angle[i]);
}
}
int getrec(double x[], double y[])
{ int count = 0;
do
{ cout << "Enter the x coordinate: ";
cin >> x[count];
cout << "Enter the y coordinate: ";
cin >> y[count];
count++;
}
while(count < SIZE && (x[count -1] != 0) && (y[count -1] =! 0));
return count;
}
void polar(double x, double y, double& r, double& theta)
{
r = sqrt((pow(x,2))+(pow(y,2)));
theta = atan(y/x) * toDegrees;
return;
}
void showPolarCoord(double radius, double angle)
{
cout << "The polar coordinates are: " << endl;
cout << " Distance from origin: " << radius << endl;
cout << " Angle (in degrees) from x-axis: " << angle << endl;
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.