Thread: help on test quest

  1. #1
    Registered User
    Join Date
    Jun 2006
    Location
    singapore
    Posts
    5

    help on test quest

    If we declare: int a[4][3]; then a[i][j] is equivalent to:
    1) *(a[i] + j)
    2) *(&a[0][0] + 4*i + j)
    3) Both (a) and (b)
    4) None of the above

    Can anyone enlighten me on which is the correct ans and
    why !! thanks

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I'll give you two hints...

    1. an array name is actually a pointer to the first position in the array

    2. A multidimensional array is in reality an array of arrays. And to dig in even further, a multidimensional array is in fact a single dimension array. arr[2][3] is in fact an array with 6 elements
    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.

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by Mario F.
    And to dig in even further, a multidimensional array is in fact a single dimension array. arr[2][3] is in fact an array with 6 elements
    Really? Does arr[1][1] equal ((int*)arr)[4]?

    A multidimensional array isn't usually a single dimension array. Consider argv. Each argument is a different length.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Yes. I should have rephrased that better. I meant conceptually since,

    Code:
    int arr[4][3] = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12} };
    
    //and
    
    int arr[4][3] = {1,2,3,4,5,6,7,8,9,10,11,12};
    produce the same array.

    The second definition helps making it clear which answer is the right one.
    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.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > Consider argv. Each argument is a different length.
    True, each argument is a different length, but you are a little confused about argv. argv is an array of pointers to strings.

    You can find the type of a declaration by reading parentheses (if there are any) inside out and right to left.

  6. #6
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    arr[2][3], and arr[6] produce essentially the same array. In memory there is actually no difference. C++ strictly speaking doesn't know multi-dimension arrays.

    Besides b) is wrong
    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. #7
    Registered User
    Join Date
    Jun 2006
    Location
    singapore
    Posts
    5
    hi mario. from ur explaination 1) is rt

    but for *(&a[0][0] + 4*i + j)
    i am not really sure.

    my feeling is that it is wrong .

    and the rt ans would be a[i][j] =*(&a[0][0] + 3*i + j)
    is this correct??

  8. #8
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    almost

    *(&a[0][0] + 4*i + j -1)

    Which makes it a too complicated formula.

    You are correct. a) is right. All others are wrong
    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.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > but for *(&a[0][0] + 4*i + j)
    Right away, this is dereferencing a memory address, which will make the program explode. It is wrong.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    arr[2][3], and arr[6] produce essentially the same array. In memory there is actually no difference. C++ strictly speaking doesn't know multi-dimension arrays.
    I think it is more like the computer does not know about multi-dimensional arrays, and C++ initializer syntax allows one to take advantage of that as a shorthand. C++ does know about multi-dimensional arrays.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jun 2006
    Location
    singapore
    Posts
    5
    hi guys i have written a prog that proves 2)
    is correct. the output is 1.
    IS there anything wrong with my prog??
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <stdio.h>
     int a[4][3],i,j;
    int test();
     main(void)
    {
          printf("%i",test());
    
    
           cin.get();
          return 0;
    }
    
    int test()
    {
    
    if (a[i][j]==*(&a[0][0] + 4*i + j))
    return 1;
    return 0;
    }

  12. #12
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Yes. Try initializing your variables first.

    Also, it looks like you're using an out-of-date compiler (if that actually compiles)
    Code:
    #include <iostream>
    
    int a[4][3] = { {1,2,3} , {4,5,6} , {7,8,9} ,  {10,11,12} };
    int i=1,j=1;
    
    int test();
    
    int main(void)
    {
          std::cout<<test();
          std::cin.get();
    
          return 0;
    }
    
    int test()
    {
    if (a[i][j]==*(&a[0][0] + 4*i + j))
      return 1;
    
    return 0;
    }
    "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

  13. #13
    Registered User
    Join Date
    Jun 2006
    Location
    singapore
    Posts
    5
    Quote Originally Posted by JaWiB
    Yes. Try initializing your variables first.

    Also, it looks like you're using an out-of-date compiler (if that actually compiles)
    Code:
    #include <iostream>
    
    int a[4][3] = { {1,2,3} , {4,5,6} , {7,8,9} ,  {10,11,12} };
    int i=1,j=1;
    
    int test();
    
    int main(void)
    {
          std::cout<<test();
          std::cin.get();
    
          return 0;
    }
    
    int test()
    {
    if (a[i][j]==*(&a[0][0] + 4*i + j))
      return 1;
    
    return 0;
    }

    actually i jus started picking up c++ a week ago and i am using DEV C++ to do the compiling. not sure which is gd for beginner.
    i started programing with JAVA which i feel is easier to understand

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    almost

    *(&a[0][0] + 4*i + j -1)

    Which makes it a too complicated formula.
    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.

    Right away, this is dereferencing a memory address, which will make the program explode. It is wrong.
    I think it is okay. This is from section 5.3.1 of the C++ Standard:
    "The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is "pointer to T," the type of the result is "T.""
    and a few lines later:
    "The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualifiedid. In the first case, if the type of the expression is "T," the type of the result is "pointer to T.""

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you picked up C++ a week ago, why are you writing everything in C, anyway?

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