#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    2D array filling


    Hi, I need to fill 2D array with random numbers
    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <time.h> 
    int main() 
    { int i, j, n, m, *rod; 
    srand(time(NULL)); 
    n=rand()%10+1; 
    m=rand()%10+1; 
    printf("size %d x %d\n",n,m); 
    rod=(int*)malloc(n*m*sizeof(int)); 
    for(i=0;i<=n;i++) 
    for(j=0;j<=m;j++) 
    *(rod+j)=rand()%10+1; 
    for(i=0;i<=n;i++) 
    for(j=0;j<=m;j++) 
    printf("%d\n",*(rod+j)); 
    system("pause"); 
    return 0; }
    I think my code doesn't work properly, any ideas? :)
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,021
    Rep Power
    1285
    One thing I notice here is that when you place an element in the array, you don't use the value of i.

    Code:
    for(i=0;i<=n;i++) 
    for(j=0;j<=m;j++) 
    *(rod+j)=rand()%10+1;
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Observe how I used k. Most programmers, I expect, would prefer to increment k as a separate statement.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main() {
      int k;
      int i, j, n, m, *rod;
      srand(time(NULL));
      n=rand()%10+1;
      m=rand()%10+1;
      printf("size %d x %d\n",n,m);
      rod=(int*)malloc(n*m*sizeof(int));
      k = 0;
      for(i=0;i<n;i++)
        for(j=0;j<m;j++)
          *(rod+k++)=rand()%10+1;
    
      puts("in-order access");
      k = 0;
      for(i=0;i<n;i++) {
        for(j=0;j<m;j++)
          printf(" %2d",*(rod+k++));
        putchar('\n');
      }
    
      puts("random access");
      for(i=0;i<n;i++) {  /*    i <= n  was incorrect */
        for(j=0;j<m;j++)
          printf(" %2d",*(rod+j+i*m));
        putchar('\n');
      }
    
    # ifndef __unix__
        system("pause");
    # endif
      return 0;
    }
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    In addition to what bullet said, I add

    + work on your indentation
    + the cast to the return value of malloc() is, at best, redundant; and it may hide an error (not in your code as presented) the compiler would catch in the absence of the cast
    + your loops are going once more than they should. The idiomatic way to loop is for (k = 0; k < 10; k++). Your condition inside the loop has a less than or equal operator.
    + you didn't free the allocated memory therby causing a memory leak.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    For one thing, even though you used code tags in your very first post here (extremely laudable, that!), you completely and utterly failed to format your code. The purpose of code tags is to preserve your code's formatting, so obviously you should have formatted it. All you accomplished was to completely defeat the purpose of using code tags, so why even bother?

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <time.h> 
    
    int main() 
    { 
        int i, j, n, m, *rod; 
    
        srand(time(NULL)); 
        n=rand()%10+1; 
        m=rand()%10+1; 
        printf("size %d x %d\n",n,m); 
        rod=(int*)malloc(n*m*sizeof(int)); 
    
        for(i=0;i<=n;i++) 
            for(j=0;j<=m;j++) 
                *(rod+j)=rand()%10+1; 
    
        for(i=0;i<=n;i++) 
            for(j=0;j<=m;j++) 
                printf("%d\n",*(rod+j)); 
    
        system("pause"); 
        return 0; 
    }
    See what a difference that makes?

    Second, you're not using a 2-D array, but rather trying to emulate one with a simple array. I guess that could work, but if you want to use a 2-D array, then why not simply use one?

    Third, your for-statement test expressions are wrong, causing you to insert an extra loop. To range over n values, you index with 0 through n-1. If instead you do as you have done, you're indexing with 0 through n, which means that you'll loop n+1times. That does not work.

    Fourth (though it should have been first or second), what do you "my code doesn't work properly"? Since we cannot read your mind, we have no idea what you are talking about. You see, you need to tell us these things!

    If it doesn't compile or gives you warnings, then tell us what those messages are (copy-and-paste is a good idea). Also telling us what compiler you're using can be very helpful, as would the operating system.

    If it compiles but crashes when you try to run it, then tell us that. Also, if it seems to crash in response to certain inputs, then provide us with those inputs.

    If it runs and doesn't crash but does not give you the output you expect, then describe to us:
    a. what that wrong output is,
    b. what output you were expecting, and
    c. what inputs you gave it that produced that output.

    For us to be able to help you, you need to help us.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Ok, sorry guys for the mess (and for bad grammar :) ), next time i will do better. And thanks to
    b49P23TIvg it helped me :)
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Now I want in each line find the smallest number, I tried with this code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main() 
    {
      int i, j, n, m, k, *min, *rod;
        srand(time(NULL));
        n=rand()%10+1;
        m=rand()%10+1;
            printf("lines %d columns %d\n",n,m);
            rod=(int*)malloc(n*m*sizeof(int));
            k = 0;
            
        for(i=0;i<n;i++)
            for(j=0;j<m;j++)
                *(rod+k++)=rand()%10+1;
        free(rod);
        k = 0;
        
        for(i=0;i<n;i++)
            { 
            printf("\n");
        
                for(j=0;j<m;j++)
                 printf(" %d",*(rod+k++));
                 printf("\n");
            }
    //------------------------------------------//
    
      k=0;
      min=rod;
      *min=*rod;
        for(i=0;i<n;i++)
      {
             for(j=0;j<m;j++)
            
                 if (*(rod+k++) < *min)
                     *min = *(rod+k++);
                       printf("smallest number in the line: %d\n", *min);
                
            
        }
      
    system("pause");
      return 0;
    }
    but always i get zero's

    http://img838.imageshack.us/img838/1096/llllv.jpg

    what should I do?
    Thanks for the help :)
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    33
    You cannot (safely) use rod after you free() it.
    Move the free() to the end of your program, after all uses of rod.

    Other than that I haven't checked the workings of your program
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Now when I moved free() I get nonses like this:
    goo.gl/cxnIf
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    You do not understand the rod+k++ expression.
    And, having freely copied it you've run into big trouble.
    Code:
                 if (*(rod+k++) < *min)
                     *min = *(rod+k++);
    rod+k++ results in rod+k, and also incremented k. Oh!
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo