11-18-2004, 01:12 PM | #1 (permalink) |
Upright
Location: montreal,canada
|
[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; } |
11-18-2004, 02:15 PM | #2 (permalink) |
Too hot in the hot tub!
|
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...
__________________
But I don't want ANY Spam! Last edited by pixelbend; 11-18-2004 at 02:23 PM.. |
11-18-2004, 02:21 PM | #3 (permalink) |
Poo-tee-weet?
Location: The Woodlands, TX
|
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...
__________________
-=JStrider=- ~Clatto Verata Nicto |
11-18-2004, 04:17 PM | #6 (permalink) |
Insane
Location: Wales, UK, Europe, Earth, Milky Way, Universe
|
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.
__________________
There are only two industries that refer to their customers as "users". - Edward Tufte |
11-19-2004, 11:40 AM | #7 (permalink) | |
Location: Waterloo, Ontario
|
Quote:
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? |
|
11-19-2004, 01:17 PM | #8 (permalink) | |
Insane
Location: Wales, UK, Europe, Earth, Milky Way, Universe
|
Quote:
__________________
There are only two industries that refer to their customers as "users". - Edward Tufte |
|
11-23-2004, 04:36 PM | #9 (permalink) |
Upright
Location: montreal,canada
|
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 |
11-23-2004, 05:37 PM | #10 (permalink) |
Insane
Location: Wales, UK, Europe, Earth, Milky Way, Universe
|
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; }
__________________
There are only two industries that refer to their customers as "users". - Edward Tufte |
11-27-2004, 09:34 AM | #12 (permalink) |
Upright
Location: montreal,canada
|
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; } |
11-27-2004, 10:05 PM | #13 (permalink) |
άber-Rookie
Location: No longer, D.C
|
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. |
Tags |
array |
|
|