#include <iostream>
using namespace std;
long int A(int, int);
int main()
{
int m, n;
cout << "Enter two numbers for Ackerman's Function." << endl;
cin >> m >> n;
cout << A(m, n) << endl;
}
long int A(int m, int n)
{
if(m == 0)
{
return n+1;
}
else if(m > 0 && n == 0)
{
return A(m-1,1);
}
else if(m > 0 && n > 0)
{
int temp = A(m,n-1);
return A(m-1, temp);
}
}
Here is a simple code of an Ackerman's function. I am wondering how many times does this Ackerman's function calls itself as a function of n, if m is a constant? My brain exploded trying to figure it out.
you can use a count global variable to find out
#include <iostream>
using namespace std;
long int A(int, int);
int count=0;
int main()
{
int m, n;
cout << "Enter two numbers for Ackerman's Function." << endl;
cin >> m >> n;
cout << A(m, n) << endl;
count << " Ackerman's Function runs " << count << " times.";
}
long int A(int m, int n)
{
count++;
if(m == 0)
{
return n+1;
}
else if(m > 0 && n == 0)
{
return A(m-1,1);
}
else if(m > 0 && n > 0)
{
int temp = A(m,n-1);
return A(m-1, temp);
}
}
Add an extra parameter of type int& to your A function:
long int A(int m, int n, int& count)
{
count++;
And update the prototype, and the recursive calls to match.
In your main function:
int count = 0;
cout << A(m, n, count) << endl;
cout << "A calls: " << count << endl;
This avoids globals, which are problematic.
Related
I'm just starting so I'm trying to write a program which determine if a number is positive or negative.
#include <iostream>;
int step_function(int x) {
int result = 0;
if (x > 0)
result = 1;
else if (x < 0)
result = -1;
else
result = 0;
return result;
}
using namespace std;
int main() {
int num;
cout<< "please enter number : ";
cin >> num;
int a = step_function(num);
if (a == 1)
printf("%d is positive", num);
else if (a == -1)
printf("%d is negative", num);
else
printf(" it is zero");
return 0;
}
There is a few things you should do:
First things first you should get yourself a Good Book for C++.
Second thing is read why using namespace std; is a bad idea.
Lastly here is your code fixed. You needed to remove the semicolon as well as removing the printf(). I also removed the using namespace std; which made it more readable.
#include <iostream>
int step_function(int); //Function prototype
int main() {
int num;
std::cout << "please enter number : ";
std::cin >> num;
int a = step_function(num);
if (a == 1)
std::cout << num << " is postive";
else if (a == -1)
std::cout << num << " is negative";
else std::cout <<" it is zero";
return 0;
}
int step_function(int x)
{
int result = 0;
if (x > 0) result = 1;
else if (x < 0) result = -1;
else result = 0;
return result;
}
Don't use semicolon after #include <iostream>.
I think for C++ cout is more standard whereas printf is from C.
You can also include printing of the text in the step_function. Also, it's better to use braces {} after if and else statements for clarity especially if the code becomes complex.
#include <iostream>
using namespace std;
void step_function(int x) {
if (x > 0) {
cout << x << " is positive" << endl;
}
else if (x < 0) {
cout << x << " is negative" << endl;
}
else {
cout << "it is zero" << endl;
}
}
int main() {
int num;
cout<< "please enter number : ";
cin >> num;
step_function(num);
return 0;
}
Implement the recursive version of Binary Search. Simulate and show all the
steps using code. Also show number of comparisons and basic operations in code.
Sample Input:
2,3,5,8,12,34,44,55,77,88,99,123
Search 5
Output:
1st iteration: 2,3,5,8,12,34,44,55,77,88,99,123
2nd iteration: 2,3,5,8,12
Found at 5
Total comparisons: 2
Total operations: ?
#include <iostream>
using namespace std;
int count=0;
int binarySearch(int arr[], int low, int high, int x)
{
while (high >= low)
{
count=count++;
int mid = (low + high) / 2;
count=count+3;
if (arr[mid] == x)
{
count=count+2;
return mid;
}
if (arr[mid] >x)
{
count=count+2;
return binarySearch(arr,low,mid-1,x);
}
else
{
return binarySearch(arr,mid+1,high,x);
}
}
return -1;
}
int main(void)
{
int n,i;
int high,low;
cout << "Enter n:";//array size
cin >> n;
int arr[n];
cout << "Enter the numbers ";//array size
for(i=0; i<n; i++)
{
cin>>arr[i];//array elements
}
int x;
cout << "Enter the number you want to search";
cin >> x;
int result = binarySearch(arr, 0, n-1, x);
(result == -1) ? cout << "Element is not present in array"
: cout << "Element is present at index " << result;
cout << "count " << count;
return 0;
}
If the problem is "how to print the array in the range [low, high]", you may insert this:
for (int i = low; i <= high; ++i) {
cout << (i == low? "": ",") << arr[i];
}
I would advise you to refactor your code exclude the upper margin however, as this is easear to work with. That would mean that the range is [low, high). The first call would be binarySearch(arr, 0, n, x) in this case.
I need to count down to 0. I am only printing 0 to the screen. How can I print all the count-down characters to the screen? Below is the code I am using right now.
#include <stdio.h>
#include <iostream>
using namespace std;
class Solution {
public:
int num;
int numberOfSteps (int num)
{
while (num != 0)
{
if (num % 2 == 0)
{
num = num / 2;
cout << num;
}
else
{
num = num - 1;
cout << num;
}
}
}
};
int main () {
int num;
Solution myObj;
cin >> num;
cout << myObj.num;
}
You're passing the num to std::cout. You are also not calling numberOfSteps(...) anywhere in your code.
Replacing the line with cout << myObj.numberOfSteps(num); fixes the problem, but a tidier solution would be as follows:
#include <stdio.h>
#include <iostream>
void countDown (int num) {
while (num != 0) {
if (num % 2 == 0) {
num = num / 2;
std::cout << num << std::endl;
} else {
num = num - 1;
std::cout << num << std::endl;
}
}
}
int main () {
int num;
std::cin >> num;
countDown(num);
}
Class is not necessary as there is no state and the function is void since it does not return anything.
I am revisiting this question and have created a simpler solution than my original post:
#include <iostream>
using namespace std;
int num;
int main()
{
cout << "Please enter the number you would like to count down to zero : ";
cin >> num;
while (num > 0)
{
cout << num << endl;
num--;
}
cout << "The number is now zero.";
return 0;
}
I tried to write a c++ program to check wether is a golden rectangle or not by using Fibonacci series (if Fibonacci term = length the previous Fibonacci series should = the breadth ) that is mean it's a golden rectangle else not golden rectangle ..
I get error stack overflow ..!!!
This my code:
#include "stdafx.h"
#include "iostream"
using namespace std;
int fib (int);
int _tmain(int argc, _TCHAR* argv[])
{
int length;
cout << "enter the Length " << endl;
cin>> length ;
int breadth;
cout << "enter the Breadth " << endl;
cin>> breadth ;
int x ;
cout << "enter the limit " << endl;
cin>> x ;
cout << endl ;
for (int i =1 ; i <= x ; i++ )
{
cout << "Fibonacci"<<"=" <<fib(i) <<" "<<"Counter =" << (i) << endl;
if ((breadth == (fib(i)-1)) && ( length == fib(i)))
{
cout << " rectangle";
}
else
{
cout << "This is not rectangle";
}
}
system ("pause");
return 0;
}
int fib (int n)
{
if (n == 1)
return 1;
if (n == 2)
return 2;
return fib (n - 1) + fib (n - 2);
}
Using
return fib (n - 1) + fib (n - 2);
for recursively computing the Fibonacci sequence is not the right strategy. It leads to a lot of unnecessary duplicate computations and recursive calls.
It is preferable to use an iterative approach.
int fib (int n)
{
int s1 = 0;
int s2 = 1;
for ( int iter = 1; iter <= n; ++iter )
{
int temp = s1;
s1 = s2;
s2 = temp + s1;
}
return s2;
}
If you must use a recursive approach, you need a helper function that makes the recursion less onerous.
int fib_helper(int s1, int s2, int iter, int n)
{
if ( iter == n )
{
return s2;
}
return fib_helper(s2, s1+s2, iter+1, n);
}
int fib (int n)
{
return fib_helper(0, 1, 1, n);
}
You can do everything in one go. There is no need to calculate Fibonacci numbers multiple times:
int lastFib = 1;
int currentFib = 1;
while(length >= currentFib)
{
//Check if the current pair of Fibonacci numbers matches the rectangle
if(length == currentFib && width == lastFib)
{
std::cout << "This is a golden rectangle" << std::endl;
return;
}
//Calculate the next Fibonacci number
int nextFib = lastFib + currentFib;
lastFib = currentFib;
currentFib = nextFib;
}
//We left the loop without returning, i.e., the rectangle is not golden.
std::cout << "This is not a golden rectangle" << std::endl;
I have a simple program that is calculating factorials, permutations and combinations. I feel good about my math but for whatever reason I cannot get this program to execute. Full disclosure I am new student to C++. Here is my code:
#include <iostream>
using namespace std;
int factorial(int);
int permutations(int, int);
int combinations (int ,int);
void perms_and_combs(int, int, int&, int&);
int numPerms;
int numCombs;
int main() {
int factorialVal;
cout << "enter an int!\n";
cin >> factorialVal;
cout << "The factorial of " << factorialVal << " is " << factorial(factorialVal) << endl;
int permVal1;
int permVal2;
do {
cout << "Input a two values: ";
cin >> permVal1;
cout << ", ";
cin >> permVal2;
} while ( permVal1 < 0 || permVal2 > permVal1);
cout << "test"; // This line doesn't get executed
perms_and_combs(permVal1, permVal2, numPerms, numCombs);
cout << "Number of permutations: "<<numPerms << ". Number of combinations: " << numCombs;
return 0;
}
int factorial(int n) {
int product = 1;
for (int i = 1; i <= n; i++) {
product *= i;
}
return product;
}
int permutations (int n, int k) {
int result;
int denominator = n-k;
cout << denominator;
result = (factorial(n)/factorial(denominator));
return result;
}
int combinations (int n, int k) {
int result;
result = permutations(n, k) * (1/factorial(k));
return result;
}
void perms_and_combs(int n, int k, int& numPerms, int& numCombs) {
numPerms = permutations(n, k);
numCombs = combinations(n, k);
return;
}