PDA

View Full Version : C++


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.

thesoothsayer
16-08-2008, 07:44 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?


Only going to talk about question 1 and on general errors.

Let's see.

1. You didn't declare the namespace that you are using.

2. You use undefined variables (variables with undefined values).

3. You use "=", an assignment operator, instead of "==" to compare. Therefore, the compiler will probably optimise statement 2 of the "if-else" statement
(else if ( num = 5)) to be always true and remove all the other parts of the if-else statement.

4. For loop initialization - the location where you put the initialisations are wrong. for(initial value; comparison; new value).

5. You use different types from the library functions. pow takes in double type and returns double type. While this can be solved with a cast (or perhaps the compiler does it for you already), it's probably better to write your own function if you want portability.
Edit: Ah sorry. In C++, pow is overloaded for:
float pow(float, float);
float pow (float, int);
double pow(double, int);
long double pow(long double, long double);
long double pow(long double, int);

But it still doesn't take integers as the first parameter. The one I stated originally is for C, which takes 2 doubles as parameters and returns a double.

6. Your algorithm is wrong. I suggest you take a pencil and simulate your function by hand or draw out the flow chart before coding.

7. Why do you need the getch() at the end? And getch() is not a standard C/C++ function either. Use getchar() if you need to for C portability across platforms.

8. You swapped the positions of the assignment. Variable to be assigned a value is always on the left. The value to be assigned is on the right.

9. conio is not a C/C++ standard header.

Good luck.

vikraman
16-08-2008, 06:55 PM
hmm i use Borland C++ 5.02. It's a very old version compiler and requires conio and getch at the end. i don't have to declare a namespace in this compiler.

Thanks for the tips. I'll try to fix it. Question 2 however.. i don't even know where to start!

thesoothsayer
17-08-2008, 04:32 AM
hmm i use Borland C++ 5.02. It's a very old version compiler and requires conio and getch at the end. i don't have to declare a namespace in this compiler.

Yes, guessed as much. How about using some newer, free compilers. Don't think there are any that's 100% compatible with the standard but are probably a whole lot closer.

vikraman
17-08-2008, 04:36 PM
well my university (UiTM) uses this officially. My lecturers teach on this as well and the university computers (where we do our practical exams and such) use it as well.. so i'm stuck with it.. anyway its just for Pre-U course.. next semester (january) i dont have C++ anymore so i'll forget about it then :P

Herlene
02-10-2008, 11:19 PM
Hi, I'm also using borland C++ 5.02.
I want to use the swap function but after compiling,I got the prompt stating "call to undefined function swap". How do I define it?

extreme
03-10-2008, 01:14 AM
Hi, I'm also using borland C++ 5.02.
I want to use the swap function but after compiling,I got the prompt stating "call to undefined function swap". How do I define it?


have u defined your function??? do you know what is a function call, function definition and function prototype???
an example of function definition is
void swap(int a[],){

int hold,pass,i;

for(pass=1;pass<SIZE;pass++){
for(i=0;i<SIZE-1;i++){
if(a[i]>a[i+1]){
hold=a[i];
a[i]=a[i+1];
a[i+1]=hold;
}
}
}
}

to call this function use this
swap(a);

the function prototype is
void swap(int );

This code function swap is to swap integer and arrange it from small to big number.It is called bubble sort in C.
another note is size is defined using macro and is inside the main program itself.In my knowledge there is no predefined function inside C to swap anything so we have to define it ourself.
I write this program in the C language format. It should be similar in C++.

:)):))
may this help you