Thread: simple question please help!!!!!!!!

  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
    838
    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
    838
    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, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM