Thread: what's wrong with this array code?

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    25

    what's wrong with this array code?

    I have a 10x10 array of numbers from 1 to 100. I'm trying to get the diagonal sum from top right to bottom left corner and then printing it. But I keep getting an error that I need a semicolon. I'll bold the part where it says I have the error.


    Code:
    #include<stdio.h>#include<stdlib.h>
    
    
    int main(void)
    {
        
        int i,j;
        int word[10][10];
        int num = 1;
        int diag_sum = 0, diag_sum_right = 0;
        
        for(i=0;i<10;i++)
        {
                         
                         for(j=0;j<10;j++)
                         {
                                          
                                          word[i][j] = num;
                                          num++;
                                          
                                          }
                                          
    }
    
    
    
    
     for(i=0;i<10;i++)
     {
                      
                      for(j=0;j<10;j++)
                      {
                                       
                                       printf("%d \t", word[i][j]);
                                       
                                       }
                                       
                                       printf("\n");
                                       
    }
    
    
    
    
                                       for(i=0;i<10;i++)
                                       {
                                                        
                                                        diag_sum += word[i][i];
                                                        
                                                        }
                                                        
                                       for(i=9;j=0;i>0;j<10;i--;j++)
                                       {
                                       
                                       diag_sum_right += word[i][j];
                                       }
                                       
                                                        
                                       printf("The sum of diagonal from left to bottom right is %d \n", diag_sum);
                                       
                                       printf("The sum of diagonal from right to bottom left is %d \n", diag_sum_right);
    
    
    
    
    system("pause");
    return 0;
    
    
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The line
    Code:
         for(i=9;j=0;i>0;j<10;i--;j++)
    is the culprit. There are too many semi-colons between the brackets.
    Last edited by grumpy; 04-24-2014 at 01:22 AM. Reason: Fixed typo
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    25
    what can I do about it?

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Code:
    for (i = 9, j = 0; i > 0 && j < 10; i--, j++)

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by DRK View Post
    Code:
    for (i = 9, j = 0; i > 0 && j < 10; i--, j++)
    That will make it compile. The OP has not described what the code is supposed to do though. There is always a difference between "compiles" and "works as intended".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's wrong with my array code?
    By Cancino Mel in forum C Programming
    Replies: 1
    Last Post: 04-02-2014, 11:24 PM
  2. Where I'm wrong in this C code
    By rcbandit in forum C Programming
    Replies: 1
    Last Post: 04-06-2011, 11:58 AM
  3. What's wrong with this code?
    By stillwell in forum C++ Programming
    Replies: 30
    Last Post: 10-07-2004, 09:09 PM
  4. Replies: 3
    Last Post: 01-19-2003, 04:08 PM
  5. what is wrong with this code
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 11-21-2001, 03:46 PM