Write a C++ program to "draw" ellipses by printing characters, to the following specifications:
Prompt the user for values for a and b, then draw an ellipse using the formula
(x2/a2)+(y2/b2)=1. Actually, since double values in C++ are rarely exact, use (x2/a2)+(y2/b2)-1.0<0.0001, but use a named constant in place of 0.0001.
Since there are 25 rows in a standard PuTTy screen, output 24 rows for the ellipse. For each row number row 0 to 23, let y be 12-row.
Since there are 80 columns in a standard PuTTy screen, output 80 characters in each line. Since each column is equivalent to about half a row in terms of space, for each column number col 0 to 79, let x be (40-col)/2.
Use pow from <cmath> to calculate the squares, ensuring that the two parameters are of type double.
After the ellipse, output information about it and prompt to repeat or not.
If it is a circle, output the word Circle, the radius of the circle (either a or b), the area of the circle and the number of characters printed in the "drawing", as in the first example.
If it is not a circle, output the word Ellipse, both a and b, the area of the ellips and the number of characters printed, as in the second example.
The area of an ellipse is Π * a * b. Use M_PI from <cmath> for the value of Π.
All floating point numbers should be output in fixed format, always showing the point and with a precision of 2 places after the point.
An example executable named project1 is available in the /share directory on the server.
As with project 1, upload a text file containing a cat of your program file, a compiler instruction and a sample run of your program to this dropbox.
You must include documentation at the top of the program with your name, the date, the course section, and a short description of the program. Every variable must have a comment explaining briefly what it is for.
Example
@
@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@
@
Circle, radius=10.00 Area=314.16 (629 characters). Go again? (y/n)
Example
@
@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@
@
Ellipse, a=10.00 b=6.00 Area=188.50 (369 characters). Go again? (y/n)
So far here is what I have:
< #include<iostream>
#include<cmath>
using namespace std;
const double PI = M_PI;
const double ZERO = 0.0001;
int a, b; //variables for a and b
int characters; //variable for the number of characters used to create the shape
int runProgram();
int main(){ //main method
char Continue; //variable for repeating the program after it has been run
do{ runProgram();
cout << "Would you like to run again? Enter Y to continue or N to quit."<<endl;
cin >>Continue;
}
while((Continue!='n')||(Continue!='N'))
}
int runProgram(){
cout << "Enter a number for 'a' : "; //asks for input a
cin >> a >> endl;//inputs number from keyboard to variable a
cout << "Enter a number for 'b' : "; //asks for input b
cin >> b >> endl;//inputs number from keyboard to variable b
drawEllipse(); //method to draw ellipse
circletest(); //method to output information about the ellipse
characters(); //method to output the number of characters that ellipse use
}
int drawEllipse();{ //method to draw an ellipse
int y=0,x=0;row=0;
for(y=0; y<12-row ; row++ ){
for(x=0; x<80; x++){
if((x*x)/(a*a)+(y*y)/(b*B)-1.0<ZERO) cout << " " <<;
else { cout << "@";
}
}
}
}
int circletest();{ //tests to see if variable a=b
if (a==B){
cout << "Circle :: " << "radius = " << a << " :: area = " << PI*a*a << " :: " <<;
} else {
cout << "Ellipse :: " << "a = " << a << "b = " << b << " :: area = " << PI*a*a << " :: " <<;
}
}
int characters();{
cout << "Characters = " << characters << endl;
}>
I'm having a hard time following all of those brackets. Could you post it again with indentation and [ code ] tags ?
Your last 3 functions look wrong int drawEllipse();{ //remove that semicolon ---> ;
They are also supposed to return an integer. If you don't want to return anything just mark the return type as void
#include<iostream>
#include<cmath>
usingnamespace std;
constdouble PI = M_PI;
constdouble ZERO = 0.0001;
int a, b; //variables for a and b
int characters; //variable for the number of characters used to create the shape
int runProgram();
int main(){ //main method
char Continue; //variable for repeating the program after it has been run
do{ runProgram();
cout << "Would you like to run again? Enter Y to continue or N to quit."<<endl;
cin >>Continue;
}
while((Continue!='n')||(Continue!='N'))
}
int runProgram(){
cout << "Enter a number for 'a' : "; //asks for input a
cin >> a >> endl;//inputs number from keyboard to variable a
cout << "Enter a number for 'b' : "; //asks for input b
cin >> b >> endl;//inputs number from keyboard to variable b
drawEllipse(); //method to draw ellipse
circletest(); //method to output information about the ellipse
characters(); //method to output the number of characters that ellipse use
}
int drawEllipse();{ //method to draw an ellipse
int y=0,x=0;row=0;
for(y=0; y<12-row ; row++ ){
for(x=0; x<80; x++){
if((x*x)/(a*a)+(y*y)/(b*B)-1.0<ZERO) cout << " " <<;
else { cout << "@";
}
}
}
}
int circletest();{ //tests to see if variable a=b
if (a==B){
cout << "Circle :: " << "radius = " << a << " :: area = " << PI*a*a << " :: " <<;
} else {
cout << "Ellipse :: " << "a = " << a << "b = " << b << " :: area = " << PI*a*a << " :: " <<;
}
}
int characters();{
cout << "Characters = " << characters << endl;
}
Thanks for cleaning it up. I mean that a function like:
1 2 3 4 5 6 7 8 9 10 11
int circletest() //it is expecting an 'int' type to be returned
{ //tests to see if variable a=b
if (a==B){
cout << "Circle :: " << "radius = " << a << " :: area = " << PI*a*a << " :: " <<;
} else {
cout << "Ellipse :: " << "a = " << a << "b = " << b << " :: area = " << PI*a*a << " :: " <<;
}
return 0; //the return statement that completes the function
//the return statement is if u need the result in the change of a variable
//or for error checking/seeing if the function succeeded
}
your other option is to just write that function prototype like this
since returning 0 all the time with no purpose is the same as not returning anything...
1 2 3
void circletest() //this function is not expecting any return type
{ //...code
}
Also, all of your functions should have prototypes ie:
Before 'main()' you should have:
int circletest(); //tells the compiler that there is going to be a function that takes
0 parameters and is expecting an integer returned
Then after 'main()' like u did, write the implementation of that function. I think that's where the extra semicolon ; confusion was at.
Another thing to fix would be this:
1 2 3 4 5
int characters(){
cout << "Characters = " << characters << endl; //'characters' is undefined here
//it goes out of scope from the one u declared in main()
//since u want to use this variable in this function u'll have to pass it into the function
}
this means changing the declaration and implementation lines to something like this:
1 2 3 4 5 6 7 8
//notice how u can and should change the name of variables in a function
//compared to what u used in main() or other functions
int characters(int numcharacters); //declaration
int characters(int numcharacters){
//...implementation
return 0;
}
OK, you have started a new thread with exactly the same question as before - Don't do this. The problem is that people might write a whole of stuff into one thread, only to discover others have written lots into the other one, which is rather annoying.
+1 IdeasMan. It's very annoying to find out you've wasted your time helping someone who already has his answer in a duplicate thread. That effort could have been spent helping someone else...
TheIdeasMan and cnoeval...with all due respect, i did not mean to do it.
I appriciate all your help, and I check both of my threads, since this one was not replied to for a day, out of desperation i posted another one. I really need help for this program that's why.
It won't happen again, and thank you for all your effort and help.
sorry