Thread: [c++] array
View Single Post
Old 11-18-2004, 01:12 PM   #1 (permalink)
mr.montreal
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  
 

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