help on test quest

This is a discussion on help on test quest within the C++ Programming forums, part of the General Programming Boards category; Essentially what happened is that you didn't initialize your array (neither your i and j flags). When one doesn't initialize ...

  1. #16
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Essentially what happened is that you didn't initialize your array (neither your i and j flags).

    When one doesn't initialize a variable and it was declared on the global scope, it gets initialized for us. On the case of ints, they get the value 0. So, your array was populated with all 0's. Which means all elements are equal. Your test could not possibly fail.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  2. #17
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    Here's a test program that I wrote. It compiles on the MinGW port of GCC 3.4.2 and on MSVC8, the program compiled returns acceptable output. My question would then be whether the pointer arithmetic is indeed correct, given that a is a two dimensional array. If it is, then the answer is still that (a)/1 is correct, because (b)/2 is wrong.
    Code:
    #include <iostream>
    
    int main()
    {
    	int a[4][3] = { {0, 1, 2}, {3, 4, 5}, {6, 7, 8}, {9, 10, 11} };
    
    	for (std::size_t i = 0; i < 4; ++i)
    	{
    		for (std::size_t j = 0; j < 3; ++j)
    		{
    			std::cout << a[i][j] << " ";
    		}
    		std::cout << "\n";
    	}
    	std::cout << "\n";
    	for (std::size_t i = 0; i < 4; ++i)
    	{
    		for (std::size_t j = 0; j < 3; ++j)
    		{
    			std::cout << *(&a[0][0] + (3 * i) + j) << " ";
    		}
    		std::cout << "\n";
    	}
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #18
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I think that is correct. According to this page: "Two dimensional arrays are stored in row major order, that is, row by row. Each row contains COLS elements, which is why you see COLS in the formula. In fact, you don't see ROWS"
    Code:
       addr( & arr[ row ][ col ] ) = addr( arr ) + [ sizeof( int ) * COLS * row ]
                                                 + [ sizeof( int ) * col ]
    It seems odd, because if I wanted to store a 2d table in a single dimensional array, I'd usually use x + y*width for the index, but for 2d arrays, it seems you have to use y + x*height.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Mario F.
    1. an array name is actually a pointer to the first position in the array
    Not exactly. (As in, no, an array is not a pointer.)
    http://c-faq.com/aryptr/constptr.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #20
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    I said array name
    An array is not a pointer. An array name is a pointer to the first element in the array.

    Code:
    int arr[5] = {1,2,3,4,5};
    
    std::cout << *(arr + 1) << std::endl;
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  6. #21
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    or more explicitly:

    Code:
    int arr[3] = {1,2,3};
    
    int *p = arr;
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    An array is not a pointer. An array name is a pointer to the first element in the array.
    An array name represents the array. An array decays to a pointer to its first element.
    Consider:
    Code:
    int arr[5] = {1,2,3,4,5};
    
    std::cout << sizeof(arr) << std::endl;
    If indeed an "array name is a pointer to the first element in the array", then you would expect sizeof(arr) to be the same as sizeof(int*). However, it would be the same as (sizeof(int) * 5) since arr has not decayed to a pointer to its first element.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #23
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Mario F.
    I said array name
    [...] An array name is a pointer to the first element in the array.
    Look!The name of an int is really a double!
    Code:
    #include <iostream>
    
    int main()
    {
       int i = 5;
       double d = i;
       std::cout << "d = " << d << '\n';
       return 0;
    }
    Or, no, such oversimplifications are simply false.

    Under certain conditions an array is converted to a pointer to the first element (as already mentioned).
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #24
    Registered User
    Join Date
    Apr 2006
    Posts
    25
    Quote Originally Posted by laserlight
    hmm... suppose i = j = 0.
    *(&a[0][0] + 4*0 + 0 - 1) => *(&a[0][0] - 1)

    This looks like one is trying to dereference a pointer that points to one before the first element.
    ...............

    In this case a[0][0] is an lvalue, so &a[0][0] is valid. (&a[0][0] + 4*i + j) is just pointer arithmetic, and results in a pointer. Hence using unary operator* on it should be valid, assuming that the pointer is valid.
    So, int a[4][3], the last element is a[3][2].
    When i =3, j = 2, *(&a[0][0] + 4*3 + 2) => *(&a[0][0]+14) = exceeds the array.
    I think that it's correct if we modify it as following : a[i][j] ==*(&a[0][0] + 3i + j), and it works with all i in [0,3] and j in [0,2].
    Am I correct ?

    Edit:
    I think a[0][0] is the pointer point to the first collumn of this array. That means it is the pointer of the array of pointers to each row of the array. This following declares an 4x3 array :
    Code:
    int **p = new int[4]; //create an one-dimemtion array of pointer type int, which is pointed to by a pointer p.
    for (int i=0;i<4;i++)
       p[i] = new int[3]; //create 3 element for each row.
    So &a[0][0] is referent to a. My feeling is that the compile would create an anonymous array variable that has the same address as a[0][0]. Thus, *(&a[0][0]) is dereferencing to the value of a.

    I do'nt know if I am right or wrong, but I just want to learn more by participating any discussion.
    Last edited by trongsi; 06-03-2006 at 07:39 PM.

  10. #25
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,436
    Quote Originally Posted by Dave_Sinkula
    Under certain conditions an array is converted to a pointer to the first element (as already mentioned).
    Yes. I had to go back to Stroustrup's (page 92). I had misread this information when I first went through it. I apologize
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    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.

  11. #26
    Registered User
    Join Date
    Jun 2006
    Location
    singapore
    Posts
    5
    this is one of my module that i have to take in my studies. Its the holiday and i am trying to finish it asap.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,593
    So, int a[4][3], the last element is a[3][2].
    When i =3, j = 2, *(&a[0][0] + 4*3 + 2) => *(&a[0][0]+14) = exceeds the array.
    I think that it's correct if we modify it as following : a[i][j] ==*(&a[0][0] + 3i + j), and it works with all i in [0,3] and j in [0,2].
    Am I correct ?
    Yes. Notice that is what I used in my test program.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

Similar Threads

  1. Integer Emulation
    By Elysia in forum C++ Programming
    Replies: 31
    Last Post: 03-18-2008, 01:03 PM
  2. undefined reference
    By 3saul in forum Linux Programming
    Replies: 12
    Last Post: 08-23-2006, 05:28 PM
  3. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  4. MSVC Template Constructor/Assignment Errors
    By LuckY in forum Windows Programming
    Replies: 3
    Last Post: 07-22-2005, 02:57 PM
  5. Space, Police, and King's quest.
    By sean in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 07-02-2002, 12:33 PM

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