Thread: something wrong with the idea of creating a struct - how to fix it?

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yup. That's more like what they are asking you to do. Hope that clear up a bit and allows yout to start seeing the possible advantages of using structs and classes as a means to simplify the description of data and its manipulation.

    For completeness you should observe the question requirements, which include also calculating the min and max values and store them in the struct.

    Meanwhile:

    - Your median calculation contains unnecessary code. You don't need to cycle through the vector. Just to sort and calculate. Note your for loop body is not making any use of i, neither it is accumulating the result of the calculation, which clearly indicates you don't need the loop. Just the calculation.

    - If you already went through algorithms, you should know you can simplify some calculations by taking advantage of these. For instance, double sum = accumulate(results.v.begin(), results.v.end(), 0); will return the sum of all values in the vector. Check the STL Algorithms.

    EDIT:
    One small side note. It's perhaps best if you replace the ternary operator in your median calculation with the if statement. Ternary operators are nice, but when they force you to a lengthy line, loose readability.
    Last edited by Mario F.; 01-21-2010 at 05:57 AM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  2. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  3. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM