Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [c++] array (https://thetfp.com/tfp/tilted-technology/76401-c-array.html)

mr.montreal 11-18-2004 01:12 PM

[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;

}

pixelbend 11-18-2004 02:15 PM

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...

JStrider 11-18-2004 02:21 PM

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...

pixelbend 11-18-2004 02:24 PM

oops, strider posted while I was editing :) same thing though

JStrider 11-18-2004 02:32 PM

using a pair of nested loops would be better then a ton of if statements...

welshbyte 11-18-2004 04:17 PM

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.

KnifeMissile 11-19-2004 11:40 AM

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?

welshbyte 11-19-2004 01:17 PM

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.

mr.montreal 11-23-2004 04:36 PM

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

welshbyte 11-23-2004 05:37 PM

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].

mr.montreal 11-23-2004 06:21 PM

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 11-27-2004 09:34 AM

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;
}


oblar 11-27-2004 10:05 PM

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.


All times are GMT -8. The time now is 01:35 AM.

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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360