Thread: [c++] array
View Single Post
Old 11-23-2004, 05:37 PM   #10 (permalink)
welshbyte
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  
 

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