ReCom.org
vikraman Male
Slightly Senior Member
 
vikraman's Avatar
 
Join Date: May 2008
Posts: 570
  #1 Old 16-08-2008 Default C++

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.
vikraman is offline   Reply With Quote
thesoothsayer
Less Junior Member
 
Join Date: May 2004
Posts: 65
  #2 Old 16-08-2008 Default

Quote:
Originally Posted by vikraman View Post
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.

Last edited by thesoothsayer; 16-08-2008 at 07:55 AM.
thesoothsayer is offline   Reply With Quote
vikraman Male
Slightly Senior Member
 
vikraman's Avatar
 
Join Date: May 2008
Posts: 570
  #3 Old 16-08-2008 Default

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!
vikraman is offline   Reply With Quote
thesoothsayer
Less Junior Member
 
Join Date: May 2004
Posts: 65
  #4 Old 17-08-2008 Default

Quote:
Originally Posted by vikraman View Post
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.
thesoothsayer is offline   Reply With Quote
vikraman Male
Slightly Senior Member
 
vikraman's Avatar
 
Join Date: May 2008
Posts: 570
  #5 Old 17-08-2008 Default

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
vikraman is offline   Reply With Quote
Herlene Female
Less Junior Member
 
Herlene's Avatar
 
Join Date: Mar 2008
Posts: 99
  #6 Old 02-10-2008 Default

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?
Herlene is offline   Reply With Quote
extreme Male
Less Junior Member
 
extreme's Avatar
 
Join Date: Mar 2007
Posts: 110
  #7 Old 03-10-2008 Smile

Quote:
Originally Posted by Herlene View Post
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
__________________
The 4 rules:
1. Say what you mean. Mean what you say.
2. Don?t say to anyone unless you can say to everyone
3. Don?t say inside, what you cannot say outside.
4. Don?t say unless it is true, useful or kind.


Somewhere, somehow
extreme is offline   Reply With Quote
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


All times are GMT +8. The time now is 03:38 AM.


Powered by vBulletin® Version 3.7.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

ReCom stands for Reborn Community. It has no affiliation with other organizations that may share the same name. The views expressed in this website solely represent the authors point of view and do not necessarily reflect the views of ReCom Anchors and other ReCom users.


 

Page generated in 0.11832 seconds with 15 queries