Thread: help w/function

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    10

    help w/function

    I need a function to determine if there are two numbers in an array of 10, whose sum is 100. The function will be called find. Here is the rest of my program:

    int initialize(int array[], int size)
    {
    int i;
    cout << "Enter ten integers: " << endl;
    cin >> array[0];

    for (i = 0; i < 10; i++)
    cin >> array[i];

    return 0;
    }

    int main()
    {
    int array[10];
    initialize(array,10);

    if (find(array,10))

    return 0;
    }

    The main function must remain the same. I'm having difficulty trying to write a function to determine if two of 10 integers has a sum of 100.
    Thanks in advance!!

  2. #2
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    for (i = 0; i < 10; i++)
    cin >> array[i];


    you are overwriting your array at position 0 here. Initialize i to 1.

    Someone else might write the function for you. I will not. You need to deliver a little bit more. How are you going to step through the array to check the sum of two numbers and hit all the possible solutions (logically you should be able to figure that out... the coding we can help you with)?
    Blue

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    >>>I'm having difficulty trying to write a function to determine if two of 10 integers has a sum of 100.
    Code:
    a=10;
    b=90;
    
    if ((a+b)==100)
      return 1;
    else 
      return 0;
    Blue

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    The evil way:

    Code:
    register int t;
    register int i;
    
    for (t = 0; t < 10; ++t) {
        for (i = 0; i < 10; ++i) {
              if (array[t] + array[i] == 100) 
                    return (1);
        }
    }
    return (0);

  5. #5
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    What does the register prefix do to the ints? Does it set the values as an asm type register??

  6. #6
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Originally posted by rmullen3
    The evil way:

    Code:
    register int t;
    register int i;
    
    for (t = 0; t < 10; ++t) {
        for (i = 0; i < 10; ++i) {
              if (array[t] + array[i] == 100) 
                    return (1);
        }
    }
    return (0);

    what is the register keyword?
    Blue

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    10

    here's more info

    I realized my error in the first part of your reply :
    "for (i = 0; i < 10; i++)
    cin >> array[i];
    "
    Thanks.

    I figured I could set up two arrays. The first would be the input acquired in the initialize array. The second array would be 100-amount in 1st array. If there is a match in 1st and 2nd line - ie. each line have 40 - those two positions equal 100. IE:

    20 40 15 60 29
    80 60 85 40 71

    the second position in 1st array and 4th position in 2nd array match, so 2nd and 4th position in 1st array = 100.

    problem is - i'm having a heck of a time figuring this out. My code is so off right now it's not worth posting. hopefully i'll have something postable soon.....please help!!

  8. #8
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    A register variable is a type of local variable. The register keyword tells the compiler “make accesses to this variable as fast as possible.” Increasing the access speed is implementation dependent but, as the name suggests, it is often done by placing the variable in a register. There is no guarantee that the variable will be placed in a register or even that the access speed will increase. It is a hint to the compiler.

    There are restrictions to the use of register variables. You cannot take or compute the address of a register variable. A register variable can only be declared within a block (you cannot have global or static register variables). You can, however, use a register variable as a formal argument in a function (i.e., in the argument list).

    Generally, you shouldn’t try to second-guess the compiler’s optimizer, since it will probably do a better job than you can. Thus, the register keyword is best avoided.
    Blue

  9. #9
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330
    Register suggest to the compiler it should place the variable in the computer's register memory. On some compilers it's a bit of a performance enhancer for counter variables (that will be incremented/decremented a lot).

    And yes, it is not the best to use ; )

  10. #10
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    >>>I figured I could set up two arrays. The first would be the input acquired in the initialize array. The second array would be 100-amount in 1st array. If there is a match in 1st and 2nd line - ie. each line have 40 - those two positions equal 100. IE:


    Nah... just do it like rmullen gave away. (though there is a problem with it) Take the first value and compare it to values 2-10. Take the second value and compare it with 1 and 3-10 etc etc.

    rmullen's wouldn't work right if one of the values was 50 because it would loop by itself and create a value of 100...

    That is close to what you want though.
    Blue

  11. #11
    Registered User
    Join Date
    Feb 2002
    Posts
    10

    getting closer...

    Ok, this is what I have. Problem now, it keeps looping. I am getting some positive finds when I input 2 #'s that equal 100, and alot of "sorry..." How can I get it to check all, but print out only one time. THanks in advance!!

  12. #12
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    >>Register suggest to the compiler it should place the variable in the computer's register memory. On some compilers it's a bit of a performance enhancer for counter variables (that will be incremented/decremented a lot).

    And yes, it is not the best to use ; )

    ***

    I didn't type that... it is a quote. I knew nothing about it.
    Blue

  13. #13
    Registered User
    Join Date
    Feb 2002
    Posts
    10

    forgot to post code

    #include<iostream.h>

    int find (int array [], int size)

    {
    int t;
    int i;

    for (t = 0; t < 10; ++t)
    {
    for (i = 0; i < 10; ++i)
    {
    if (array[t] + array[i] == 100)
    cout << "The numbers equal 100.\n";
    else cout << "Sorry, I cannot find 2 numbers whose sum is 100.\n";
    }
    }


    return (0);
    }



    int initialize(int array[], int size)
    {

    int i;
    cout << "Enter ten integers: " << endl;

    for (i = 0; i < 10; i++)
    cin >> array[i];

    return 0;
    }

    int main()
    {
    int array[10];

    initialize(array,10);

    if (find(array,10))

    return 0;
    }

  14. #14
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    >>>Ok, this is what I have. Problem now, it keeps looping. I am getting some positive finds when I input 2 #'s that equal 100, and alot of "sorry..." How can I get it to check all, but print out only one time. THanks in advance!!


    and that is the other issue. Once it finds one value, you want it to continue until the entire array is processed. There may be more than two numbers that add up to 100. Several things to think about.


    ***edit****

    use code tags when you post code.

    [ code ]

    your code

    [ /code ]

    without the spaces between the brackets....
    Blue

  15. #15
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Thanx, that's what I thought but I wasn't sure.

Popular pages Recent additions subscribe to a feed