title: Calculating number of page faults for 2-d array
body: <p>I am trying to study for an exam..and I found this example but can't understand how they got the answer. Can anyone explain it please?</p>
  
  <p>Question:</p>
  
  <blockquote>
    <p>Consider the two-dimensional array A:
    int A[][] = new int[100][100];
    where A[0][0] is at location 200 in a paged memory system with pages of size 200. A small process that manipulates the matrix resides in the page 0 (location 0 to 199). Thus every instruction fetch will be from page 0. 
    For two page frames, how many page faults are generated by the following array-initialization loops, using LRU replacement and assuming that the first page frame contains the process and the other is initially empty?</p>
  </blockquote>
  
  <p>A:</p>
  
  <pre><code>for (int j=0;j&lt;100;j++)
     for (int i=0; i&lt;100; i++)
       A[i][j] = 0;
  </code></pre>
  
  <p>B:</p>
  
  <pre><code>for(int i=0; i&lt;100; i++)
    for (int j=0; j&lt;100; j++)
        A[i][j] = 0;
  </code></pre>
  
  <p>The correct answer given is :
  a: 100 x 50 = 5000
  b: 50</p>
  
  <p>I somewhat understand the first part. There are total of 50 pages. (10000/200=50) and every time j changes, a page fault occurs..so a total of 100 page faults..but why is that multiplied by 50? and why is the second one 50? </p>
  
  <p>Thanks!! </p>