why only the succeeding value adds up???

This is a discussion on why only the succeeding value adds up??? within the C Programming forums, part of the General Programming Boards category; yeah, but why does that mean more power?...

  1. #16
    Registered User
    Join Date
    Sep 2008
    Location
    St. Petersburg, FL
    Posts
    35
    yeah, but why does that mean more power?

  2. #17
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    you could replace the whole switch with a small array (no more conditionals):
    Code:
      int total[]={0/*unused*/,25,55,45,...};
      printf(...)
      scanf("%u",&order);
      sum+=total[order%the_appropriate_value];

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by root4 View Post
    you could replace the whole switch with a small array (no more conditionals):
    Code:
      int total[]={0/*unused*/,25,55,45,...};
      printf(...)
      scanf("%u",&order);
      sum+=total[order%the_appropriate_value];
    Although it probably is a good idea to range-check the values.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You CAN of course use if/else instead of switch/case - but consider what the compiler does if you have a value 8 for this:
    Code:
    if(order==1){
           total=25.00;
           }
    else if (order==2){
    	total=55.00;
    	}
    else if(order==3){
    	total=45.00;
    	}
    else if (order==4){
    	total=35.00;
    	}
    else if(order==5){
    	total=50.00;
    	}
    else if (order==6){
    	total=70.00;
    	}
    else if(order==7){
    	total=80.00;
    	}
    else if(order==8){
    	total=15.00;
    	}
    Unless it figures out that it's a "switch in disguise", it will compare down the chain until it finds the one that matches. In a switch/case block, the compiler will (most of the time) generate a jump-table, which means that no matter how many variants it is, there will be a O(1) type, rather than O(n) that the if/else version does.

    Note that reasonably modern/clever compilers, such as gcc 3.4 can not figure out that the above is a "switch/case in disguise". I just tried out the above code as a if/else, and as a switch/case, and for the average case, the code is simply shorter and better for the switch/case statement.

    But if it's part of your assignment to use if-else, then do so.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ctime adds \n
    By Elkvis in forum C Programming
    Replies: 2
    Last Post: 05-19-2009, 08:54 AM
  2. Google maps adds keyhole integration
    By kermi3 in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 04-05-2005, 11:55 AM
  3. How to Remove computer adds
    By nipun in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 09-28-2004, 01:07 PM
  4. Function that adds "!" to end of String
    By M204Programmer in forum C++ Programming
    Replies: 4
    Last Post: 03-24-2003, 03:07 PM
  5. banner adds
    By iain in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 09-16-2001, 12:35 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21