recursive calculator

closed account (STR9GNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int tes(int n)
{
	
	if (n == 1)
	{
		counter++;
		return 3;
	}
	else if (n == 2)
	{
		counter++;
		return 2;
	}
	else
	{
		counter++;	
		return tes(n - 2) + tes(n - 1);
	}	 
}


am i doing it correctly to count the amount of times my recursive function is being called upon?
Last edited on
sure, but
1
2
3
4
5
6
int tes(int n){
   counter++;
   if (n == 1) return 3;
   else if (n == 2) return 2;
   else return tes(n - 2) + tes(n - 1);	 
}
would work too..
closed account (STR9GNh0)
woah, i 've tried that after i have post this in.

okays. so for the above if i were to input n = 6 my recursive calls will be 15?
That's because you are recalculating too many values. (ie: 6 need 5 and 4, but 5 also calls 4 ).
You could improve a lot if you store the values that you already calculate (dynamic programming)
closed account (STR9GNh0)
hmm..

what should my answer be if i have done it correctly ?
i want to write a program asking two integer numbers from the user and then compute the sun of all the integer numbers in between these two numbers.

i have been having problem with it...
closed account (STR9GNh0)
whiisper2uall:

1
2
3
4
5
6
7
8
9
10
11
12
13

int n;
int n2;
int sum;

cout << "number 1: ";
cin >> n;
cout << "number 2: ";
cin >> n2;

sum = n + n2;

cout << sum;
Last edited on
closed account (STR9GNh0)
sad.. still no solution~
aww no solution to dis...

please modify this

#include <iostream>
using namespace std;

int main()
{
int count;
int num1;
int num2;
int sum;

sum = 0;
count<num1;


cout << "enter first integer :";
cin >> num1;

cout << "enter second integer : ";
cin >> num2;


while ( count< num1,count <= num2)
{
cout << "count << endl";
sum = sum + count;
count= count +1;

}

cout << "Sum of first & second number = " << sum;

return 0;
}
closed account (STR9GNh0)
what do you need from there?

if it is just some calculations, refer below
Last edited on
closed account (STR9GNh0)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main()
{
int count;
int num1;
int num2;
int sum;

sum = 0;
//count<num1;  <-- count never initialize, what is with the < ?? dno't quite understand the reason behind this


cout << "enter first integer :";
cin >> num1;

cout << "enter second integer : ";
cin >> num2;


//while ( count< num1,count <= num2)  <- are u trying to achieve calculations over calculations?
//{
	cout << "count << endl";
	sum = num1 + num2;
//}

cout << "Sum of first & second number = " << sum;

return 0;
}
Last edited on
Ok...but i run the program but its not showing the sum of integer between two numbers...please edit the control statement..i am sure thats where my error is
here is the question :

the program will ask two integer numbers from the user and then compute the sun of all the integer numbers in between these two numbers.

Sample output


Please insert integer 1: 8
Please insert integer 2: 1
The numbers to be added: 8 7 6 5 4 3 2 1
Total value: 36
Last edited on
@ whiisper2uall,

open your own thread instead of supersede nanochan1.

to your problem:

1
2
3
4
for(int i = num1; i < num2; ++i)
{
  sum += i;
}
if excluding num1/num2 write:
1
2
3
4
for(int i = num1 + 1; i < (num2 - 1); ++i)
{
  sum += i;
}


@ nanochan1

your original post line 7: Do you intentionally return 3? What's purpose of tes
here is the question :

the program will ask two integer numbers from the user and then [b]compute the sum of all the integer numbers in between these two numbers.[/b]

Sample output


Please insert integer 1: 8
Please insert integer 2: 1
The numbers to be added: 8 7 6 5 4 3 2 1
Total value: 36
@ whiisper2uall,

open create your own thread topic instead of supersede nanochan1.

to your problem:

1
2
3
4
5
6
if(num1 > num2) // now it doesn't matter which is the least value
  std::swap(num1, num2);
for(int i = num1; i <= num2; ++i) // '<=' instead of '<' (my fault)
{
  sum += i;
}

compute the sum of all the integer numbers in between these two numbers.
Last edited on
nanochan1 wrote:
what should my answer be if i have done it correctly ?
eh? you're doing it correctly. If you take the dynamic approach, first time will be linear and after that just 1.

Counting the recursive calls for fibonacci
1
2
3
4
unsigned int counter(int n){
	if( n==1 or n==2 ) return 1;
	return 1+counter(n-1)+counter(n-2);
}
Thanks i got what i wanted..though you were helpful..

Please run this program


#include <iostream>
using namespace std;
int main()
{
int i, j;
int row = 2;
int column = 9;
i = 1;
while ( i <= row )
{
j = 1;
while ( j <= column )
{
cout << "* ";
j++;
}
cout << "\n";
i++;
}
return 0;
}


Please how can edit the first program,so it will give this below...

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
the arrangemnt of the * not working wel..is there anyway i can attach a file n post??

Last edited on
i intend getting a diamond shape output....
Topic archived. No new replies allowed.