# Function Problems

• 09-27-2005
TheGamE
Function Problems
Hello all,

Quite new at this programming stuff, and am having a few problems with functions...i have four questions:
I need functions:
1. that accepts an integer and returns the sum of squares of all numbers from 1 to the integer
2. that accepts an integer and prints the Fibonacci sequence of numbers i.e. 1, 1, 3, 5...
3. that accepts an integer and return true, if it is prime, false otherwise.
4. that accepts 2 integers and returns:
0 if they are equal
-1 if the first number is less than second
1 if first number is greater than second

Just the functions would like to do the programs myself.
Thank You,

TheGamE
• 09-27-2005
Speedy5
Sounds like homework.

But honestly though, if you're having problems, post what you have right now. You should at least be able to make function prototypes for the questions. Then look in your textbook to read about for/while loops since you'll be needing those for the first three. The last one is simply an if statement.
• 09-27-2005
Salem
> Just the functions would like to do the programs myself.
Well by the time you've written the functions, the "program" isn't much more than call the functions.

• 09-27-2005
Rashakil Fol
Scroll down to the bottom for my real answer; I just felt the urge to put jokes in between.

Quote:

Originally Posted by TheGamE
I need functions:

Okay!
Quote:

Originally Posted by TheGamE
1. that accepts an integer and returns the sum of squares of all numbers from 1 to the integer

{(0,0), (1,1), (2,∞), (3,∞), (4,∞), ...}
Quote:

Originally Posted by TheGamE
2. that accepts an integer and prints the Fibonacci sequence of numbers i.e. 1, 1, 3, 5...

Code:

`void foo() { std::cout << "1, 1, 3, 5, ...\n"; }`
I was a bit confused here because 1, 1, 3, 5 is not the Fibonacci sequence, but let's not let that get in the way.
Quote:

Originally Posted by TheGamE
3. that accepts an integer and return true, if it is prime, false otherwise.

{(-1, false), (-2, false), (-3, false), (-4, false), ...}
Quote:

Originally Posted by TheGamE
4. that accepts 2 integers and returns:
0 if they are equal
-1 if the first number is less than second
1 if first number is greater than second

{ ((x,y), 0): x,y ∈ Z, x = y } ∪ { ((x,y), -1): x,y ∈ Z, x < y } ∪ { ((x,y), 1): x,y ∈ Z, x > y }

Okay, your real answer (I'm just summarizing what the others have said):

What parts of C++ do you have trouble with? Do you understand conditionals? How about loops? Namely, you should be able to write these yourself, once you have learned a little, in which case the real problem is that you have some learning to do. This forum is here to cause people to learn, and learning is certainly more useful than answers. How can we help you?
• 09-27-2005
dwks
Quote:

3. that accepts an integer and return true, if it is prime, false otherwise.
Try using the modulus operator (%). It gives you the remainder. So x % 2 will be 1 if x is odd, zero if x is even.

• 09-27-2005
Daved
Prime is different than even/odd.
• 09-27-2005
dwks
Why so it is. Sorry, misread it somehow.
• 09-27-2005
wakish
yeah, try to post your codes and we can then help u better...

btw my advice would be to look for a good tutorial on C++ and try coding from that..
Never ever try to write codes without any knowlegde about something, that WON'T help!
and DN'T just blindly copy codes, that won't help either.
The MOST important thing is to understand the key concept! And then u can start building on these concepts! ;)
• 09-27-2005
Dae
Put all known primes up to n number, say 100, or 1000, in an array. Then use a for loop to compare if the integer input is equal to it, if it is equal to one in the list it returns true.

Code:

```int primeNumbers[100] =  {2,3,5,7,9,11,13,17,19,23,29,31,37,41,43, 47,53,59,61,67,71,73,79,83,89,97,101,103, 107,109,113,127,131,137,139,149,151,157, 163,167,173,179,181,191,193,197,199,211, 223,227,229,233,239,241,251,257,263,269, 271,277,281,283,293,307,311,313,317,331, 337,347,349,353,359,367,373,379,383,389, 398,401,409,419,421,431,431,433,439,443, 449,457,461,463,467,479,487,491,499,503, 521,523,541}```
• 09-28-2005
TheGamE
Rofl, not homework...I borrowed this C++ book from a library doing the exercises in it...apprently they haven't put in the answers. Plus I managed to do most of them.
Thanks for the help till now :p

1. function that accepts integer and return sum of squares of all numbers 1 to integer.

Code:

```int sumOfSquares(int n) {     int i = 1;     int sum = 0;     while (i <= n)     {           sum = sum + i^2;           i++;       }       return sum; }```
The program runs fine, but the answer is not correct :P

2. function that accepts integer and prints fibonacci sequence of numbers.

(No clue on how to do this)

3. function that accept an integer and return true if prime false otherwise.

Code:

```int primeNumber(int n) {     if (n % 1 == 0) && if (n % n == 0)           return true;     else           return false; }```
Giving loads of errors, don't know where to begin correcting :P

Thanks,

TheGamE

Note: I am not a fag to get my work done from other people, I was interested in some programming got a few books and this question are from the exercises.
• 09-28-2005
Salem
> sum = sum + i^2;
^ is exclusive-or (a bitwise operator), not raise-to-the-power-of like it is in some other languages.

Try
sum = sum + i * i;

> if (n % 1 == 0) && if (n % n == 0)
The syntax would be
if ( foo && bar )
Besides, calculating whether a number is prime or not is a lot more complicated than that. Use the web to find out more.
• 09-28-2005
Decrypt
Quote:

Originally Posted by TheGamE
2. function that accepts integer and prints fibonacci sequence of numbers.

(No clue on how to do this)

To put it simply, the fibonacci sequence is a sequence of numbers with the following properties:
1) The first two numbers are 1 and 1.
2) Each successive number is the sum of the two before it.

So, the fibonacci numbers are 1,1,2,3,5,8,13,...

Knowing that, the program to generate them is fairly straightforward. (But if you can't get it, ask ;) )

Decrypt