Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [c++] array (https://thetfp.com/tfp/tilted-technology/76401-c-array.html)

mr.montreal 11-18-2004 01:12 PM

[c++] array
 
hi all, the question is:

write a program that inputs 6 integers,and then checks to see if any combination of exactly three of the first 5 integers adds up to the last integer.

test cases:

1)input -----> 1 -2 4 -1 15 17 output -------> Yes. -2+4+15=17
2) 3 12 11 -4 16 7 No.


It doesnt work though, any help would be appreciated.
this is what i have so far:




#include <iostream>
using namespace std;



int main()


{

const int size=6; //defines the size N for 6 elements
int a[6]; //declares the array's elements as integers
int i = 0;

cout << "Enter six numbers: ";
for (i=0;i<size;i++)
cin >> a[i];

for (i=0; i < 5;i++)
if ((a[i] + a[i+1] + a[i+2]) == a[5])

{

cout << "Yes." << a[i] << "+" << a[i+1] << "+" << a[i+2] << "=" << a[5];
}

else
cout << "No.";

return 0;

}

pixelbend 11-18-2004 02:15 PM

how does it not work? does it break on compile? where does it break?

Looks to me like your numbers are going into the array correctly but your comarison is off.
You need to compare 012 then 023 then 034 then 123 then 134, etc.
Probably need a nested for loop for that.

for (int i = 0; i <= 2; i++){
for (int k = 1; k <= 3; k++){
...etc...

JStrider 11-18-2004 02:21 PM

it looks like your only checking the first 3 numbers... you need to make if statements that check all the other numbers in the array also...

so right now your checking
if ((a[i] + a[i+1] + a[i+2]) == a[5])
which is checking if the first +second + third == sixth

next you would prolly want to check
if ((a[i] + a[i+2] + a[i+3]) == a[5])
checking if the first + third + fourth == sixth

doin it the way im mentioning ya prolly dont really need to put em in the loop...
just do if ((a[0] + a[1] + a[2]) == a[5])

seems like there should be a better way instead of just tons of if statements...

pixelbend 11-18-2004 02:24 PM

oops, strider posted while I was editing :) same thing though

JStrider 11-18-2004 02:32 PM

using a pair of nested loops would be better then a ton of if statements...

welshbyte 11-18-2004 04:17 PM

I just had a go at this.. didn't take long. Not sure if there's a "better" way of doing this but (not gonna paste my code) i used 3 for loops and selected the initialisation and test statement of each looping variable (i, j, k) very carefully.

Tip: Try writing out all the combinations of array indexes that will need to be checked. You'll find there's a systematic way of doing it that avoids duplications.

KnifeMissile 11-19-2004 11:40 AM

Quote:

Originally Posted by mr.montreal
hi all, the question is:

write a program that inputs 6 integers,and then checks to see if any combination of exactly three of the first 5 integers adds up to the last integer.

test cases:

1)input -----> 1 -2 4 -1 15 17 output -------> Yes. -2+4+15=17
2) 3 12 11 -4 16 7 No.

Any combination? If you really mean that then it sounds like everybody has misinterpreted your question.

For instance, 3 5 7 8 -2 6 -> yes, 3+5-2=6.

If this is, indeed, the problem you're trying to solve then your printed algorithm is totally naive and no one has come close to solving it.

I'll wait to see if this is your actual problem but this isn't hard to solve. My intuition is that you'll need to solve it recursively. Are you familiar with recursion?

welshbyte 11-19-2004 01:17 PM

Quote:

Originally Posted by KnifeMissile
If this is, indeed, the problem you're trying to solve then your printed algorithm is totally naive and no one has come close to solving it.

I did solve it in that way: _any_ combination. Although i didnt use recursion, just 3 nested for loops and a bit of logic. It took 16 lines of code (including }'s on single lines) inside the main method.

mr.montreal 11-23-2004 04:36 PM

the question was on a midterm, after submitting my answer i came home and posted that original post.

the prof later posted the question on his site. here is the exact question:

Q1. Write a program that inputs 6 integers, and then checks to see if any combination of exactly three of the first 5 integers adds up to the last integer.

Test Case 1 (TC1): Input 1 –2 4 –1 15 17 Output Yes. – 2 + 4 + 15 = 17
TC2: Input 3 12 11 –4 16 7 Output No.
TC3: Input 0 12 23 –1 11 11 Output Yes. 0 + 12 + – 1 = 11

welshbyte 11-23-2004 05:37 PM

Considering you've already done the midterm i think its justified to post my version of it. I'd be interested to know if its close to the actual answer.
Code:

#include <iostream>

using namespace std;

int main() {
    const int size = 6;
    int nums[size];

    cout << "Enter 6 numbers:\n";
    for (int i = 0; i < size; i++) {
        cout << (i + 1) << ":> ";
        cin >> nums[i];
    }
   
    for (int i = 0; i < size - 3; i++)
        for (int j = i + 1; j < size - 2; j++)
            for (int k = j + 1; k < size - 1; k++)
                if ((nums[i] + nums[j] + nums[k]) == nums[size-1]) {
                    cout << "Yes: ";
                    cout << nums[i] << " + ";
                    cout << nums[j] << " + ";
                    cout << nums[k] << " = ";
                    cout << nums[5] << "\n";
                }
    return 0;
}

As you can see its pretty close to the functionality given in the spec but if there are no matches, it won't say "No." it'll just end. Also, it'll list all matching combinations that add up to nums[5].

mr.montreal 11-23-2004 06:21 PM

running your code.....

Enter 6 numbers:
1:> -1
2:> -2
3:> 4
4:> 6
5:> 7
6:> 8
Yes: -2 + 4 + 6 = 8

thats correct. thanks

mr.montreal 11-27-2004 09:34 AM

here is the solution posted by the prof.
A little more sophisticated than I thought.

Code:


#include <iostream>
using namespace std;

int main() {
        int a[6], b[10];
        int s=0, o, p, q;
        bool match=false;
        cout<<"Enter six integers between 1 and 1000"<<endl;
        for (int i=0; i<=5; i++)
                cin>>a[i];
        for (int j=0; j<5; j++)
                for (int k=j+1; k<5; k++)
                        for (int l=k+1; l<5; l++) {
                                b[s] = a[j]+a[k]+a[l];
                                if (b[s] == a[5]) {
                                        o=j; p=k; q=l;
                                        match = true;
                                }
                                        s++;
                        }
        if (match)
cout<<"MATCH found: "<<a[o]<<" + "<<a[p]<<" + "<<a[q]<<" = "<<a[5]<<endl;
        else cout<<"NOT found. No combination adds up to "<<a[5]<<endl;
        return 0;
}


oblar 11-27-2004 10:05 PM

i wouldn't call that too sophisticated, but that is mainly because of two reasons.

1. He checks combinations that he had already checked.
2. Even after a match is found he continues to check for other matches, but he only stores the last one found. I would either have it exit on that match or at least save all possible matches.


All times are GMT -8. The time now is 02:27 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73