Nov 21, 2010 at 6:52pm Nov 21, 2010 at 6:52pm UTC
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 Nov 21, 2010 at 7:01pm Nov 21, 2010 at 7:01pm UTC
Nov 22, 2010 at 12:29am Nov 22, 2010 at 12:29am UTC
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?
Nov 22, 2010 at 2:00am Nov 22, 2010 at 2:00am UTC
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)
Nov 22, 2010 at 7:57am Nov 22, 2010 at 7:57am UTC
hmm..
what should my answer be if i have done it correctly ?
Nov 22, 2010 at 8:55am Nov 22, 2010 at 8:55am UTC
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...
Nov 22, 2010 at 8:57am Nov 22, 2010 at 8:57am UTC
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 Nov 22, 2010 at 8:57am Nov 22, 2010 at 8:57am UTC
Nov 22, 2010 at 9:00am Nov 22, 2010 at 9:00am UTC
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;
}
Nov 22, 2010 at 9:13am Nov 22, 2010 at 9:13am UTC
what do you need from there?
if it is just some calculations, refer below
Last edited on Nov 22, 2010 at 9:16am Nov 22, 2010 at 9:16am UTC
Nov 22, 2010 at 9:15am Nov 22, 2010 at 9:15am UTC
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 Nov 22, 2010 at 9:18am Nov 22, 2010 at 9:18am UTC
Nov 22, 2010 at 9:16am Nov 22, 2010 at 9:16am UTC
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
Nov 22, 2010 at 9:19am Nov 22, 2010 at 9:19am UTC
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 Nov 22, 2010 at 9:24am Nov 22, 2010 at 9:24am UTC
Nov 22, 2010 at 9:47am Nov 22, 2010 at 9:47am UTC
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 numbe rs.[/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
Nov 22, 2010 at 10:42am Nov 22, 2010 at 10:42am UTC
@ 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 Nov 22, 2010 at 10:43am Nov 22, 2010 at 10:43am UTC
Nov 23, 2010 at 5:27am Nov 23, 2010 at 5:27am UTC
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...
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Nov 23, 2010 at 5:29am Nov 23, 2010 at 5:29am UTC
the arrangemnt of the * not working wel..is there anyway i can attach a file n post??
Last edited on Nov 23, 2010 at 5:30am Nov 23, 2010 at 5:30am UTC
Nov 23, 2010 at 5:36am Nov 23, 2010 at 5:36am UTC
i intend getting a diamond shape output....