Leo Storm
Leo Storm

Reputation: 37

2d array filling

I would like to fill 2d array, and this is what I do. However, it would give compile errors such as warning: value computed is not used and i dont understand why. I would appreciate if anyone could show me the problem and explain what could be done. thanks!

    #include <stdio.h>
    #include <string.h>

    int main()
    {
       int array1[4][4];
       int len = sizeof(array1) / sizeof(array1[0]);
       int wid = sizeof(array1[0]) / sizeof(array1[0][0]);

         int i, j , z = 0;
         //compile error
         for(i = 0, i < len; i++)
         {
            for(j = 0, j < wid; j++)
            {
              array1[i][j] = z;
            }
            z++;      
         }

         int a, b;
         for(a = 0, a < len; a++)
         {
            for(b = 0, b < wid; b++)
            {
              printf("%d", array1[a][b]);
             }
         }
           return 0;
         }

Upvotes: 1

Views: 2683

Answers (1)

rob mayoff
rob mayoff

Reputation: 385500

You have put a comma after the initialization part of each of your for statements. You should put a semicolon. For example, you wrote this:

     for(i = 0, i < len; i++)

You need to change it to this:

     for(i = 0; i < len; i++)

Also, you will probably want to print spaces between array elements, and a newline after each row:

     for(a = 0; a < len; a++) {
        for(b = 0; b < wid; b++) {
          printf("%d ", array1[a][b]);
        }
        printf("\n");
     }

Upvotes: 2

Related Questions