8 Replies - 1547 Views - Last Post: 12 December 2012 - 11:10 AM Rate Topic: -----

#1 HeroConcept  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-December 12

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
 
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 This A Good Question/Topic? 0
  • +

Replies To: Is there anyway to shorten this?(Idea's)

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9487
  • View blog
  • Posts: 35,777
  • Joined: 12-June 08

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?
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4399
  • View blog
  • Posts: 12,255
  • Joined: 18-April 07

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

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.

:)
Was This Post Helpful? 0
  • +
  • -

#4 magius96  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 71
  • View blog
  • Posts: 787
  • Joined: 15-April 09

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;
Was This Post Helpful? 2
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3649
  • View blog
  • Posts: 11,417
  • Joined: 05-May 12

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:
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.
Was This Post Helpful? 1
  • +
  • -

#6 magius96  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 71
  • View blog
  • Posts: 787
  • Joined: 15-April 09

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.
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,805
  • Joined: 16-October 07

Re: Is there anyway to shorten this?(Idea's)

Posted 12 December 2012 - 09:46 AM

Hmm...

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


Was This Post Helpful? 0
  • +
  • -

#8 magius96  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 71
  • View blog
  • Posts: 787
  • Joined: 15-April 09

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.
Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,805
  • Joined: 16-October 07

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. :P
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1