oop by robert lafore, exercise 10, chap 7... help?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shockxd
    New Member
    • Apr 2015
    • 2

    #1

    oop by robert lafore, exercise 10, chap 7... help?

    It's the actual problem.

    A matrix is a two-dimensional array. Create a class matrix that provides the same safety feature as the array class in Exercise 7; that is, it checks to be sure no array index is out of bounds. Make the member data in the matrix class a 10-by-10 array. A constructor should allow the programmer to specify the actual dimensions of the matrix (provided they’re less than 10 by 10). The member functions that access data in the matrix will now need two index numbers: one for each dimension of the array. Here’s what a fragment of a main() program that operates on such a class might look like:

    Code:
    matrix m1(3, 4); // define a matrix object
    int temp = 12345; // define an int value
    m1.putel(7, 4, temp); // insert value of temp into matrix at 7,4
    temp = m1.getel(7, 4); // obtain value from matrix at 7,4
    Now, help me with the idea. How can i initialize a member data (10 by 10 array) and then reinitialize the array to the given dimensions of matrix?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Read this:



    THen post again with any further questions.

    Comment

    • shockxd
      New Member
      • Apr 2015
      • 2

      #3
      Hi, Thank you for your time.

      That article is good but it will take some time for me to digest. I will do it your way, "read and understand the article and solve the problem" if you say "It's the only way to do this problem. This problem needs those topics (in the article) to be covered by me".

      Robert in his chapter 7 teaches basics about arrays like.
      - Array Fundamentals (declaration, initialization, passing to functions etc.)
      - Array as Class Member Data
      - Array of Objects

      That's it.

      How can he give a problem like that which requires more understanding of arrays than he already gave. So, i think maybe problem belongs to wrong chapter??? or Is there a simpler way to re-initialize/re-assign the dimensions of a 10 by 10 array???

      Please Comment on this.

      I will go for the article and hopefully in 3/4 days i will be able to do it, just i wanna be sure.
      Thank you again.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        A 10 by 10 array is 100 elements contiguous in memory.

        You can initialize these elements using a loop that cycles from 0 99.

        Or you can get fancy and say, no, these 100 elements are really 10 arrays of 10 elements each. Now you use two loops where the outer loop cycles 0 to 9 and the inner loop cycles from 0 to 9. These are exactly the same elements in the above example. That is a 10 x 10.

        Or you can say, no, these 100 elements are really an array of 5 elements where each element is an array of 2 elements where each of those element is an array of 10 elements. Now you need three loops. The outer loop cycles from 0 to 4, the middle loop cycles 0 to 1 and the inner loop cycles from 0 to 9. That is a 5 x 2 x 10. Still the same 100 elements from the first example.

        It's not these arrays are structured differently rather it's your VIEW that is different. Re-read the last example in that article.

        Note that when you allocate an array all you allocate is an area of memory. There are no rows or columns or anything else. You map that memory to be what you want. Run a loop from 0 to 99 on that memory and you will see your 10 x10 matrix initialized perfectly.

        Things must work this way to preserve "pointer arithmetic" so that the compiler can calculate the addresses you want to use. You will find that when the compiler can't do that you will get compile errors until you preserve pointer arithmetic.

        Comment

        Working...