Thread: [c++] array
View Single Post
Old 11-27-2004, 09:34 AM   #12 (permalink)
mr.montreal
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  
 

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