Thread: Repeated Addition

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    Repeated Addition

    Hello everyone,

    I'm trying to write pseudocode for a function that uses repeated addition instead of multiplication. In other words, inputting 2 and 5 should return 10, using only addition.

    So far, I've thought of using one of the inputted numbers as a counter, but I don't know how this would be useful without the multiplication operator. I have some code, but it doesn't address how I can get the sum. I really need help getting direction with this program. If it helps, I think we are expected to use a recursive function.

    Code:
    #include <stdio.h>
    
    float sum (float number1, float number2)
    {
        float sum;            /* sum of the two input numbers */
        
        sum = ??;
        
        return (sum);
    }
    Thank you all in advance!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Recursion is not necessary for this.
    Use a for loop.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rocksteady View Post
    Hello everyone,

    I'm trying to write pseudocode for a function that uses repeated addition instead of multiplication. In other words, inputting 2 and 5 should return 10, using only addition.

    So far, I've thought of using one of the inputted numbers as a counter, but I don't know how this would be useful without the multiplication operator. I have some code, but it doesn't address how I can get the sum. I really need help getting direction with this program. If it helps, I think we are expected to use a recursive function.
    Good ideas, as far as the first sentence goes. I suppose you could use a recursive function for this. You could also use a circular saw to cut your fingernails. (In other words: way more tool than you actually need, and I suggest you only look that way if the assignment explicitly asks for it.)

    Code:
    #include <stdio.h>
    
    float sum (float number1, float number2)
    {
        float sum;            /* sum of the two input numbers */
        
        sum = ??;
        
        return (sum);
    }
    So, why sum? You don't want to return the sum (2+5=7, but you're supposed to return 10). Think back, way back, to the first time you ever saw multiplication. What is multiplication, and how does it relate to adding? When you answer that, you'll be much farther.

    (PS: floats? Are you sure we aren't talking (unsigned) integers?)

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Edited: hindsight is 20/20
    Last edited by rocksteady; 12-10-2007 at 03:33 PM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A for loop repeats a number of specified times.
    How would you multiplication in the real world with only using +?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Here is my revised code:

    Code:
     int sum (int x, int y)
    
    {
    	int sum;	/* sum of the two input numbers */
    
    	for (sum=0; x<y; sum+y)
    		x--;
    
    	return (sum);
    }
    How does it look?
    Last edited by rocksteady; 12-10-2007 at 04:07 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And what does that do? Not a darn thing but an infinite loop.
    What does the code look to you?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    I meant for it to add an input value to a variable initialized to 0, multiple times. Should I change the condition so that x>=0?

  9. #9
    Registered User
    Join Date
    Jan 2007
    Location
    Euless, TX
    Posts
    144
    if x=2 and y=5, how would you code that in a for loop to make the sum = 10?

    Start with this: outside the for loop, sum = 0

    for x times you are going to add y to sum, right?

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by rocksteady View Post
    Code:
        for (sum=0; x<y; sum+y)
    this part has no effect.

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Don't name variables the same as function names, it's silly.

    Keep track of the total, it might look something like:
    Code:
    #include <math.h>
    
    float sumf(float a, float b)
    {
        float total = 0.0f;
        int cycles =    (int) ceil(b),
                        i = 0;
        
        for(i = 0; i < cycles; i++)
            total += a;
        
        return total;
    }
    Of course if you steal that, you'd be a theif.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Are we going to give out the answer again?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes because I cbf, I'm not giving it away -- you can find things like that everywhere on the net. Unless he understands it, stealing will do him more harm than good.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by zacs7 View Post
    Unless he understands it, stealing will do him more harm than good.
    So it will... So it will...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Of course if you steal that, you'd be a theif.
    On the plus side, at least it isn't recursive (that's apparently a requirement).
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-01-2008, 07:47 AM
  2. PC Issue: Repeated Long Beeps
    By alphaoide in forum Tech Board
    Replies: 8
    Last Post: 07-19-2005, 08:46 AM
  3. need some help with binary addition.
    By InvariantLoop in forum C++ Programming
    Replies: 21
    Last Post: 01-27-2005, 06:52 AM
  4. Check for repeated digits!
    By CrackerJack in forum C Programming
    Replies: 5
    Last Post: 11-08-2003, 11:37 PM
  5. Problems with an addition operator
    By jamjar in forum C++ Programming
    Replies: 12
    Last Post: 03-28-2003, 01:47 PM