Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 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;

}
mr.montreal is offline  
Old 11-18-2004, 02:15 PM   #2 (permalink)
Too hot in the hot tub!
 
pixelbend's Avatar
 
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..
pixelbend is offline  
Old 11-18-2004, 02:21 PM   #3 (permalink)
Poo-tee-weet?
 
JStrider's Avatar
 
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
JStrider is offline  
Old 11-18-2004, 02:24 PM   #4 (permalink)
Too hot in the hot tub!
 
pixelbend's Avatar
 
oops, strider posted while I was editing same thing though
__________________
But I don't want ANY Spam!
pixelbend is offline  
Old 11-18-2004, 02:32 PM   #5 (permalink)
Poo-tee-weet?
 
JStrider's Avatar
 
Location: The Woodlands, TX
using a pair of nested loops would be better then a ton of if statements...
__________________
-=JStrider=-

~Clatto Verata Nicto
JStrider is offline  
Old 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
welshbyte is offline  
Old 11-19-2004, 11:40 AM   #7 (permalink)
 
KnifeMissile's Avatar
 
Location: Waterloo, Ontario
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?
KnifeMissile is offline  
Old 11-19-2004, 01:17 PM   #8 (permalink)
Insane
 
Location: Wales, UK, Europe, Earth, Milky Way, Universe
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.
__________________
There are only two industries that refer to their customers as "users". - Edward Tufte
welshbyte is offline  
Old 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
mr.montreal is offline  
Old 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;
}
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].
__________________
There are only two industries that refer to their customers as "users". - Edward Tufte
welshbyte is offline  
Old 11-23-2004, 06:21 PM   #11 (permalink)
Upright
 
Location: montreal,canada
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 is offline  
Old 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;
}
mr.montreal is offline  
Old 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.
oblar is offline  
 

Tags
array


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 10:57 PM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, 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 74 75 76