int validation(int pick, int I, int II, int III, int IV, int V){
if (pick == I)
return 0;
else if (pick != 1)
return 1;
else if ((pick == I) || (pick == II))
return 0;
else if ((pick != I) || (pick != II))
return 1;
else if ((pick == I) || (pick == II) || (pick == III))
return 0;
else if ((pick != I) || (pick != II) || (pick != III))
return 1;
else if ((pick == I) || (pick == II) || (pick == III) || (pick == IV))
return 0;
else if ((pick != I) || (pick != II) || (pick != III) || (pick != IV))
return 1;
else if ((pick == I) || (pick == II) || (pick == III) || (pick == IV) || (pick == V))
return 0;
else if ((pick != I) || (pick != II) || (pick != III) || (pick != IV) || (pick != V))
return 1;
}
Is there anyway to shorten this?(Idea's)
Page 1 of 18 Replies - 547 Views - Last Post: 12 December 2012 - 11:10 AM
#1
Is there anyway to shorten this?(Idea's)
Posted 11 December 2012 - 10:37 AM
Hi, this is my first post. I am a first year computer programming student and working on a assigment, yes I know "We won't do your homework for you" which is not what I am getting at.I Have already completed the code and it works fine for what it has to, but is there anyway to make this shortened I feel like its a bit bulky, for the assignment I dont care.. just wondering. Thanks
Replies To: Is there anyway to shorten this?(Idea's)
#2
Re: Is there anyway to shorten this?(Idea's)
Posted 11 December 2012 - 10:54 AM
Have you been properly introduced to the select/case or switch statements?
#3
Re: Is there anyway to shorten this?(Idea's)
Posted 11 December 2012 - 10:55 AM
I just wanted to point out a small pattern here....
Isn't saying if (pick == I) going to be true in this other if statement if ((pick == I) || (pick == II))? This means you don't need the stand alone if (pick == I).
Carry this idea through the rest of the if statements. You will quickly find out that (because they are OR'ed together) that you really only need a statement like ....
Because if pick is equal to I, II, III, IV, or V it will trigger this and return the 0. You don't need to test for them individually. Now if they were ANDed together it might be a different story.
Isn't saying if (pick == I) going to be true in this other if statement if ((pick == I) || (pick == II))? This means you don't need the stand alone if (pick == I).
Carry this idea through the rest of the if statements. You will quickly find out that (because they are OR'ed together) that you really only need a statement like ....
if ((pick == I) || (pick == II) || (pick == III) || (pick == IV) || (pick == V))
return 0;
Because if pick is equal to I, II, III, IV, or V it will trigger this and return the 0. You don't need to test for them individually. Now if they were ANDed together it might be a different story.
#4
Re: Is there anyway to shorten this?(Idea's)
Posted 11 December 2012 - 12:06 PM
If I've read everything right, here's the smallest you can write that:
return ((pick == I) || (pick == II) || (pick == III) || (pick == IV) || (pick == V)) ? 0 : 1;
return ((pick == I) || (pick == II) || (pick == III) || (pick == IV) || (pick == V)) ? 0 : 1;
#5
Re: Is there anyway to shorten this?(Idea's)
Posted 11 December 2012 - 09:11 PM
Huh? Martyr2 and magius96, I appreciate your attempt to simplify the logic of the OP, but in my mind it looks like you've missed a major case.
Consider the situation where the call is:
Both of your code simplifications would have x == 0, but if you follow the logic of the OP, x == 1 due to lines 2-5 of the original post.
Consider the situation where the call is:
int x = validation(2, 1, 2, 3, 4, 5);
Both of your code simplifications would have x == 0, but if you follow the logic of the OP, x == 1 due to lines 2-5 of the original post.
#6
Re: Is there anyway to shorten this?(Idea's)
Posted 12 December 2012 - 08:34 AM
if (pick == I) return 0; else if (pick != 1) return 1; else if ((pick == I) || (pick == II)) return 0; else if ((pick != I) || (pick != II)) return 1; else if ((pick == I) || (pick == II) || (pick == III)) return 0; else if ((pick != I) || (pick != II) || (pick != III)) return 1; else if ((pick == I) || (pick == II) || (pick == III) || (pick == IV)) return 0; else if ((pick != I) || (pick != II) || (pick != III) || (pick != IV)) return 1; else if ((pick == I) || (pick == II) || (pick == III) || (pick == IV) || (pick == V)) return 0; else if ((pick != I) || (pick != II) || (pick != III) || (pick != IV) || (pick != V)) return 1; }
First, let me say that I assumed the second conditional was a typo. When compared to the rest of the code provided, I made the judgement that the second conditional was supposed to be testing against I not 1. If that was the case, then control flow will NEVER get past the second conditional. If that's not the case, control flow will NEVER get past the 4th conditional.
Let me expain:
if (pick == I) return 0; else if (pick != I) return 1;
In order to pass both of these conditionals, pick must be equal to, and not equal to I.
else if ((pick == I) || (pick == II)) return 0; else if ((pick != I) || (pick != II)) return 1;
Even if we don't change the 1 to an I, then the next two conditionals do the same thing. In order to pass these two lines pick must be both equal to and not equal to I or II.
I believe that Martyr2 and I were both working on the assumption that 1 was supposed to be an I in the second conditional, and using that logic to make a corrected version of the logic by looking only at that last two conditionals since they comprise all the other conditionals within them.
I say all this to illustrate to both you, and the OP that the code provided by the OP does in fact not work as it never tests all the conditions. The repsonses provided by both Martyr2 and I allot for the typo correction and repair the damaged logic all at the same time to provide a cleaner more accurate result.
#7
Re: Is there anyway to shorten this?(Idea's)
Posted 12 December 2012 - 09:46 AM
Hmm...
Assuming the code (pick != 1) was supposed to be (pick != I), then the shortest form is:
int validation(int pick, int I, int II, int III, int IV, int V){
if (pick == I) { return 0; }
// note, from this point forward (pick != I) must be true
if (pick != 1) { return 1; } // not sure how 1 fits into this?
// this if ((pick == I) || (pick == II)) return 0;
// is really
if (pick == II) { return 0; }
// this if ((pick != I) || (pick != II)) return 1;
// is really
return 1;
// because if (pick == II) didn't make us leave, then (pick != II) must be true
}
Assuming the code (pick != 1) was supposed to be (pick != I), then the shortest form is:
int validation(int pick, int I, int II, int III, int IV, int V){
return (pick == I) ? 0 : 1;
}
#8
Re: Is there anyway to shorten this?(Idea's)
Posted 12 December 2012 - 10:02 AM
baavgai, your solution works if he intended to only check the first variable. In our solutions however we assume that he intended to check all variables. I guess only the OP will really know what they were going for.
#9
Re: Is there anyway to shorten this?(Idea's)
Posted 12 December 2012 - 11:10 AM
I'm afraid with a request to shorten "this" I can only reasonably work from the logic of what "this" is.
Such a question was actually more interesting than the one assumed.
Such a question was actually more interesting than the one assumed.
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote









|