0
Golden Rectangle using c++
I am trying to write program to check the given is golden rectangle or not.. But all time I fail I don't know what is exactly reason.. Any one help me... This my code.. int rec(int, int); int main () { int length = 100; int breadth = 60; cout << length << breadth << rec (length, breadth); system ("pause") ; } int rec(int a, int b) { double x=a/b; double y=a+b/b; double phi = 1.618; if (x==phi) { cout << x << " this is the golden rectangle" <<endl; } else if (y==phi) { cout <<y<<"this is the golden rectangle" ; } else { cout << " this is not golden rectangle" ; } return rec(a, b) ; }
10 Answers
0
Here's an example of your code modified to return boolean, a boolean is an expression that evaluates to either true or false, we use conditional check eg. 'if' to see if a boolean evaluates to true or false. The rec function returns true or false by checking whether x or y equals to phi, and in main function we use that return value to decide what to print, whether or not it is golden rectangle.
#include <iostream>
using namespace std;
bool rec(int, int);
int main ()
{
int length = 100;
int breadth = 60;
cout << "Length: " << length << endl;
cout << "Breadth: " << breadth << endl;
if(rec(length, breadth))
{
cout << "This is golden rectangle";
}
else
{
cout << "This is not golden rectangle";
}
}
bool rec(int a, int b)
{
double x = a / b;
double y = a + b / b;
double phi = 1.618;
// return true if either x or y equals to phi
if (x == phi || y == phi) return true;
// return false otherwise
return false;
}
+ 2
It's so much easier for people to help when you post a link to your code, that way it can just be run. If you paste it here, it's not as easy to help.
+ 1
*System("pause");
No such function defined.
*rec(int a, int b) {
//Codes
return rec(a,b);
}
Looks like its an infinite recursion
+ 1
I correct ; and y==phi put still give me infinite recursion
+ 1
Remove the last line
return rec(a,b)
+ 1
I am sorry I am beginner in programming.. Could you you help me how to use bool value and return it by rec in same time rec it void..
0
Please mind code indentation, function prototype 'int rec(int, int)' is missing semicolon, and typo in 'else if(y==ohi)'. And also take note of others' suggestions.
0
I want to check regtangle depend on the rule golden ratio it equal 1.618..
a/b = a+b/b = 1.618
0
When I removed return(a, b).. Error occur rec must return a value. Should I make it void function?
0
I think you better switch the function to void, as I see it you're only calling the function, no return value was expected, nor used. Alternatively, you can switch it to return bool and use the return value in main, and you print golden rectangle in main, depending on rec return value.