2D array filling

This is a discussion on 2D array filling within the C Programming forums, part of the General Programming Boards category; Hi, I need to fill 2D array with random numbers Code: #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    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
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,681
    First style your code nicely .Read here .

    You do not allocate memory as you should be.Remember that the 2D array is a normal array (1D) of pointers to int.Every pointer is set to a normal array.
    Also because it is a 2D array, it should be a double pointer, not just a pointer,so not *rod but **rod.

    If i want to access the element in i-th row and j-th column i have to write
    Code:
    array[i][j];
    PS - i suggest you not to cast what malloc returns to you

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    2,435
    What are you exact requirements? What exactly is not "working properly" - please tell us what you expect and what is happening.

    You're supposed to fill a 2D array with random numbers. Instead, you're randomly determining the size of storage - are you sure this is right? I don't see any arrays declared in your code.

    And properly indent your code so it's readable.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Your loops are set up properly to iterate through each possible combination of i and j, but then when you try to read or write a value, you're only using the current value of j, so you're basically looping over the first row a bunch of time, instead of moving on to the next row each time. What you should do is have a single value that keeps incrementing as you move through your matrix (instead of resetting it to 0 after each run, like your loops do), or you should calculate your position using i and j (e.g. i*j + j).

    Also, I'd recommend using curly braces around your loops. Even though in this case it's not technically necessary, it helps makes the code easier to read and can help you avoid mistakes in your code - because you're being explicit about where the loop begins and ends. Also, don't forget to use nice indentation when you post - it's easier for people to help you.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Oh sorry for that, I'm new here.
    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


    Name:  llllv.jpg
Views: 357
Size:  58.1 KB


    what should I do?
    Thanks for the help

  6. #6
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Code:
    if (*(rod+k++) < *min)
        *min = *(rod+k++);
    The ++ operator is dangerous when you use it like this. You're using it to mean "k+1" but it has side effects. The value of k will get incremented every time the conditional is evaluated, and then again every time it evaluates to true. This might also be why you're ending up with zeros. But what else have you tried to debug this? Learning to step through the code and see if what you think is happening is really happening is a critical skill - you won't get far if you just post your code and ask why it isn't working.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    You mustn't free something and then continue to use it. That is asking for a crash.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. filling an array..
    By kromagnon in forum C Programming
    Replies: 3
    Last Post: 11-06-2011, 05:53 PM
  2. Filling up a 2D (or 3D) array.
    By omnificient in forum C Programming
    Replies: 1
    Last Post: 01-20-2008, 12:22 PM
  3. Filling an array
    By GCNDoug in forum C Programming
    Replies: 18
    Last Post: 04-25-2007, 02:46 PM
  4. Filling an array
    By nizbit in forum C Programming
    Replies: 3
    Last Post: 01-23-2005, 01:02 PM
  5. filling an array
    By Flex in forum C Programming
    Replies: 7
    Last Post: 02-28-2002, 02:11 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21