Search Results

Search found 2 results on 1 pages for 'theva'.

Page 1/1 | 1 

  • Recursively determine average value

    - by theva
    I have to calculate an average value of my simulation. The simulation is ongoing and I want (for each iteration) to print the current average value. How do I do that? I tried the code below (in the loop), but I do not think that the right value is calculated... int average = 0; int newValue; // Continuously updated value. if(average == 0) { average = newValue; } average = (average + newValue)/2; I also taught about store each newValue in an array and for each iteration summarize the whole array and do the calculation. However, I don't think that's a good solution, because the loop is an infinity loop so I can't really determine the size of the array. There is also a possibility that I am thinking too much and that the code above is actually correct, but I don't think so...

    Read the article

  • Is fetching data from database a get-method thing?

    - by theva
    I have a small class that I call Viewer. This class is supposed to view the proper layout of each page or something like that... I have a method called getFirstPage, when called the user of this method will get a setting value for which page is currently set as the first page. I have some code here, I think it works but I am not really shure that I have done it the right way: class Viewer { private $db; private $user; private $firstPage; function __construct($db, $user) { $this->db = $db; if(isset($user)) { $this->user = $user; } else { $this->user = 'default'; } } function getFistPage() { $std = $db->prepare("SELECT firstPage FROM settings WHERE user = ':user'"); $std->execute(array(':user' => $user)); $result = $std->fetch(); $this->firstPage = $result['firstPage']; return $this->firstPage; } } My get method is fetching the setting from databse (so far so good?). The problem is that then I have to use this get method to set the private variable firstPage. It seems like I should have a set method to do this, but I cannot really have a set method that just fetch some setting from database, right? Because the user of this object should be able to assume that there already is a setting defined in the object... How should I do that?

    Read the article

1