help w/function

This is a discussion on help w/function within the C++ Programming forums, part of the General Programming Boards category; for (t = 0; t < 10; ++t) { for (i = 0; i < 10; ++i) since you take ...

  1. #16
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    for (t = 0; t < 10; ++t)
    {
    for (i = 0; i < 10; ++i)


    since you take in the value for size, you might as well use it here for 10.
    Blue

  2. #17
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Problem now, it keeps looping.

    realize that it is going to say something ~100 times with way you have it set up.

    pull the "sorry...." outside of the loop like this

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

  3. #18
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    or even better

    Code:
    int set=0;
    
    for (t = 0; t < 10; ++t) 
    { 
       for (i = 0; i < 10; ++i) 
       { 
          if (array[t] + array[i] == 100) 
          {
             cout << "The numbers equal 100.\n";
             set++;
           } 
        }
        if (set==0)
          cout << "Sorry, I cannot find 2 numbers whose sum is 100.\n"; 
        else
           cout << "Found " << set << " sets of numbers whose sum is 100\n."
    
    }
    Once again this doesn't take into account one number equaling 50. Notice how one number equaling 50 will make it appear that 100 was reached using two different numbers.

    I will leave you to the rest....
    Blue

  4. #19
    Registered User
    Join Date
    Feb 2002
    Posts
    10

    Thanks Betazep

    I'm getting closer...you've been a great help. I appreciate it.

  5. #20
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    A final hint on the problem of calculating the value of the same number if it equals 50...

    if/then hint: if number 1 is equal to number 2 then it is the same array position... or rather if number 1 is not equal to number 2 then perform desired operation
    Blue

  6. #21
    Registered User
    Join Date
    Feb 2002
    Posts
    10

    got it

    thanks betazep, i figured that one out. my only issue now is, I only want it to print one message or the other, not both. everything else checks out - got rid of double hits (two hits on same set of #'s) etc. Think I have the last problem figured out, going to try it now (fingers crossed!).

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21