simple question please help!!!!!!!!

This is a discussion on simple question please help!!!!!!!! within the C++ Programming forums, part of the General Programming Boards category; What would be the value in sum of the following statements when executed? int sum, k, i, j; int x ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    3

    Question simple question please help!!!!!!!!

    What would be the value in sum of the following statements when executed?

    int sum, k, i, j;
    int x [4] [4] = {{1 , 2 , 3 , 4} , {5 , 6 , 7 , 8} , {9 , 8 , 7 , 3} , {2 , 1 ,7 , 1}};

    sum = x [0] [0]
    for ( int k=1 ; k<=3 ; ++k)
    sum += x [k] [k];

    I thank you for your help in advance.
    Last edited by masood123; 07-01-2008 at 09:30 PM. Reason: add values contained in x

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    impossible to know without knowing the values contained in x.

  3. #3
    Registered User
    Join Date
    Jul 2008
    Posts
    3
    the valves are:

    int sum, k, i, j;
    int x [4] [4] = {{1 , 2 , 3 , 4} , {5 , 6 , 7 , 8} , {9 , 8 , 7 , 3} , {2 , 1 ,7 , 1}};

  4. #4
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    839
    think about it...

    since the indices of the first and second dimensions are always the same, you're adding up the elements of the diagonal of the matrix.

    x[0][0]+x[1][1]+x[2][2]...

    i'm just afraid that's all the help i'm going to give you. this problem is pretty trivial and you need to "get" this concept. try writing out the values differently so that the array more closely resembles a square matrix like so:
    Code:
    int x [4] [4] = 
    {
    {1 , 2 , 3 , 4} , 
    {5 , 6 , 7 , 8} ,
    {9 , 8 , 7 , 3} , 
    {2 , 1 , 7 , 1}
    };
    solving your problem should be very easy now .

    good luck.
    Last edited by m37h0d; 07-01-2008 at 09:47 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 08:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 11:15 AM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 05:58 PM

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