0

I'm trying to print 8 rows and 5 columns in my output, but my code is throwing errors saying:

warning: excess elements in array initializer. 

This is my code:

#include <stdio.h>
  
void main()
{
   int rows,columns;
   int array[8][5] =
   {
    { 11, 12, 13, 14, 15 }, { 21, 22, 23, 24, 25 },
    { 31, 32, 33, 34, 35 }, { 41, 42, 43, 44, 45 },
    { 51, 52, 53, 54, 55 }, { 61, 62, 63, 64, 65 },
    { 71, 72, 73, 74, 75 }, { 81, 82, 83, 84, 85 },
   };
   
   for(columns=0;columns<=4;columns++)
   {
       for(rows=0;rows<=7;rows++)
       {  
            printf("%d",array[rows][columns]);
       }
       printf("\n");
   }
}

Can anyone please help me fix this code snippet?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 2
    You define `array` as an array of five sub-arrays, each sub-array containing eight elements. But you initialize as eight sub-arrays, each sub-array containing five elements. – Some programmer dude Oct 01 '20 at 07:10
  • Note that you should _not_ change the code in the question to invalidate answers. You've changed the original `int array[5][8]` to `int array[8][5]`, and your code no longer generates the warning you claimed. You're printing the data in column-major order, which is fine if that's what you want, but is aconventional in C — the data is stored in row-major format. – Jonathan Leffler Oct 01 '20 at 07:49

1 Answers1

2

You have:

int array[5][8]= {{11,12,13,14,15},{21,22,23,24,25},{31,32,33,34,35},{41,42,43,44,45},{51,52,53,54,55},{61,62,63,64,65},{71,72,73,74,75},{81,82,83,84,85}};

That declares an array of five sub-arrays with eight elements in each sub-array, but you try to initialize it with eight sub-arrays (with five elements in each), and that is why you get the error message about "excess elements". If there were only five sub-arrays with five elements in each, the last three elements in each sub-array would be zeroed.

Fortran does this differently from C. See Wikipedia on Row-major vs Column-major order.

You need to either use int array[8][5] = { … }; or you need to regroup your initializers into five groups of eight, not eight groups of five.

int array[8][5] =
{
    { 11, 12, 13, 14, 15 }, { 21, 22, 23, 24, 25 },
    { 31, 32, 33, 34, 35 }, { 41, 42, 43, 44, 45 },
    { 51, 52, 53, 54, 55 }, { 61, 62, 63, 64, 65 },
    { 71, 72, 73, 74, 75 }, { 81, 82, 83, 84, 85 },
};

Or:

int array[5][8] =
{
    { 11, 12, 13, 14, 15, 21, 22, 23, },
    { 24, 25, 31, 32, 33, 34, 35, 41, },
    { 42, 43, 44, 45, 51, 52, 53, 54, },
    { 55, 61, 62, 63, 64, 65, 71, 72, },
    { 73, 74, 75, 81, 82, 83, 84, 85, },
};

I want 8 rows and 5 columns. Every set of 5 elements should be printed in 8 separate rows.

So you need int array[8][5] — 8 rows with 5 elements in each row. In a 2D array in C, the first index is the row, the second is the column. That means the outer loop runs over rows, the inner loop runs over columns.

#include <stdio.h>

int main(void)
{
    int array[8][5] =
    {
        { 11, 12, 13, 14, 15 }, { 21, 22, 23, 24, 25 },
        { 31, 32, 33, 34, 35 }, { 41, 42, 43, 44, 45 },
        { 51, 52, 53, 54, 55 }, { 61, 62, 63, 64, 65 },
        { 71, 72, 73, 74, 75 }, { 81, 82, 83, 84, 85 },
    };

    for (int row = 0; row < 8; row++)
    {
        for (int col = 0; col < 5; col++)
            printf(" %d", array[row][col]);
        putchar('\n');
    }
    return 0;
}

Output:

 11 12 13 14 15
 21 22 23 24 25
 31 32 33 34 35
 41 42 43 44 45
 51 52 53 54 55
 61 62 63 64 65
 71 72 73 74 75
 81 82 83 84 85
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • But still the 8 rows and five columns are not getting printed. the leemnts in the rows and columns are jumbling... –  Oct 01 '20 at 07:40
  • You also have to fix your printing code. But since it isn't clear what you want as the structure, it isn't clear what should happen. Assuming you want `int array[5][8]`, you should have loops like `for (int row = 0; row < 5; row++) { for (int col = 0; col < 8; col++) printf(" %d", array[row][col]); putchar('\n'); }`. But if you really want `int array[8][5]`. then you exchange the limits. In a 2D array in C, the first index is the row, the second is the column. – Jonathan Leffler Oct 01 '20 at 07:45
  • I want 8 rows and 5 columns. every 5 set of elements should be printed in 8 separate rows.. –  Oct 01 '20 at 07:49
  • OK — so define `int array[8][5]` and loop on 8 rows (0..7) first and 5 columns (0..4) second, printing `array[row][col]` values. Note that `array[row][col]` is consistent — as I said before, in a 2D array in C, the first index is the row, the second is the column. Don't forget to ensure that the numbers are separated in the printing; your current format doesn't ensure that the numbers are separate. – Jonathan Leffler Oct 01 '20 at 07:53
  • `#include #include void main() { int matrix[8][5]={ { 11, 12, 13, 14, 15 }, { 21, 22, 23, 24, 25 }, { 31, 32, 33, 34, 35 }, { 41, 42, 43, 44, 45 }, { 51, 52, 53, 54, 55 }, { 61, 62, 63, 64, 65 }, { 71, 72, 73, 74, 75 }, { 81, 82, 83, 84, 85 }}; int columns,rows; for(columns=0;columns<=4;columns++){ for(rows=0;rows<=7;rows++){ printf(" %d " ,matrix[rows][columns]); } printf("\n"); } }` I tried fixing the code but even this is not printing 5 elemts in each row ( for instance , row 1 should only consist 11,12,13,14,15 –  Oct 01 '20 at 08:02
  • 1
    I showed you in my [first comment](https://stackoverflow.com/questions/64150544/printing-a-2d-array-warning-excess-elements-in-array-initializer/64150575#comment113440526_64150575) that you MUST do ROWS BEFORE COLUMNS in your loops. I said it again in my [prior comment](https://stackoverflow.com/questions/64150544/printing-a-2d-array-warning-excess-elements-in-array-initializer/64150575#comment113440713_64150575). I'm telling you again. It isn't rocket science — it is straight-forward. You are required to think a little for yourself. – Jonathan Leffler Oct 01 '20 at 08:13
  • Thank you. running the inner loop in 'rows' is the convention but in this case it should have been reversed. –  Oct 01 '20 at 08:18
  • In C, the inner loop will not conventionally be 'rows' — it will be 'columns'. I'm not sure which language you learned anything different. The printed output requires you choose the row you want to output, and then print the column values for that row. You can, if you wish, transpose the matrix on output, printing 5 lines of data with 8 values in each row; and you can print columns or rows in reverse order if you wish, and all sorts of variations. But the basic 'dump the data in the matrix' technique prints by rows then columns — the outer loop is over rows and the inner is over columns. – Jonathan Leffler Oct 01 '20 at 08:24