Thread: Function Problems

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    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

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    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.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.

    Start with the first one and post your attempt at it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    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?
    Last edited by Rashakil Fol; 09-27-2005 at 11:32 AM.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

    [edit]There's a thread in the FAQ board that answers this.[/edit]
    Last edited by dwks; 09-27-2005 at 04:10 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Prime is different than even/odd.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Why so it is. Sorry, misread it somehow.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    ~Team work is the best!~ wakish's Avatar
    Join Date
    Sep 2005
    Posts
    85
    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!

  9. #9
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    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}
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  10. #10
    Registered User
    Join Date
    Sep 2005
    Posts
    2
    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

    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.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Moderately Rabid Decrypt's Avatar
    Join Date
    Feb 2005
    Location
    Milwaukee, WI, USA
    Posts
    300
    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
    There is a difference between tedious and difficult.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Problems with str.replace function
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2001, 03:35 AM