vikraman
16-08-2008, 12:36 AM
Hey, I realise there are a lot of C++ and other programming languages genius here. I have some questions from my lecturer which I really need help on. Here's the questions.
1. Write a program that asks the user to type an integer N and computes the sum of the cubes from 5^3 to N^3.
My solution to this question is like so;
#include <iostream>
#include <conio>
#include <math>
int main ()
{
int num, count, x;
cout << "Please enter an integer : ";
cin >> num;
num = x;
if ( num > 5)
{
for ( count = 5; count++; count <= num)
{
x = x + 1;
x = pow (x, 3.0);
cout << " Values are " << x;
cout << "\n";
}
}
else if ( num = 5)
cout << "125";
else
{
for ( count = 5; count--; count >= num)
{
x--;
x = pow (x, 3.0);
cout << " Values are " << x;
cout << "\n";
}
}
getch ();
return 0;
}
However it doesn't work. Can anyone tell me why?
2. Write a program to find a Fibonacci number. Consider the following sequence of number :
1,1,2,3,5,8,13,21,34...
Given the first numbers of the sequence (say A1 and A2), the nth number AN, N>= 3, of this sequence is given by:
AN = AN-1 + AN-2
Thus: A3 = A2 + A1 = 1 + 1 = 2,
A4 = A3 + A2 = 2 + 1 = 3, and so on....
Such a sequence is called a Fibonacci sequence. In the preceding sequence, A2 = 1 and A1 = 1. However given any first two number, using this process, you can determine the Nth number, AN, N>= 3, of this sequence. The number determined this way is called the Nth Fibonacci number. Suppose A2 = 6 and A1 = 3. Then:
A3 = A2 + A1 = 6 + 3 = 9
A4 = A3 + A2 = 9 + 6 = 15,
Next, we write a program that determines the Nth Fibonacci number given the first two numbers.
Input = The two fibonacci numbers and the desired fibonacci number
Output = The Nth position fibonacci number.
Thanks a lot for your help.
1. Write a program that asks the user to type an integer N and computes the sum of the cubes from 5^3 to N^3.
My solution to this question is like so;
#include <iostream>
#include <conio>
#include <math>
int main ()
{
int num, count, x;
cout << "Please enter an integer : ";
cin >> num;
num = x;
if ( num > 5)
{
for ( count = 5; count++; count <= num)
{
x = x + 1;
x = pow (x, 3.0);
cout << " Values are " << x;
cout << "\n";
}
}
else if ( num = 5)
cout << "125";
else
{
for ( count = 5; count--; count >= num)
{
x--;
x = pow (x, 3.0);
cout << " Values are " << x;
cout << "\n";
}
}
getch ();
return 0;
}
However it doesn't work. Can anyone tell me why?
2. Write a program to find a Fibonacci number. Consider the following sequence of number :
1,1,2,3,5,8,13,21,34...
Given the first numbers of the sequence (say A1 and A2), the nth number AN, N>= 3, of this sequence is given by:
AN = AN-1 + AN-2
Thus: A3 = A2 + A1 = 1 + 1 = 2,
A4 = A3 + A2 = 2 + 1 = 3, and so on....
Such a sequence is called a Fibonacci sequence. In the preceding sequence, A2 = 1 and A1 = 1. However given any first two number, using this process, you can determine the Nth number, AN, N>= 3, of this sequence. The number determined this way is called the Nth Fibonacci number. Suppose A2 = 6 and A1 = 3. Then:
A3 = A2 + A1 = 6 + 3 = 9
A4 = A3 + A2 = 9 + 6 = 15,
Next, we write a program that determines the Nth Fibonacci number given the first two numbers.
Input = The two fibonacci numbers and the desired fibonacci number
Output = The Nth position fibonacci number.
Thanks a lot for your help.