0

Hello everyone!! I have a problem about memory allocation, i use calloc() to initialize all elements in array to be zero.

This is my code:

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

#define sizes 1200



 main()
{
    double** arr;
    int m[sizes];
    int i,j,k;

    srand((unsigned)time(NULL));

    arr= (double **) calloc(sizes,sizeof(double*));
    for(k=0; k< sizes; k++){
    arr[k]=(double*)calloc(sizes,sizeof(double));
    }

    for(j=0; j< sizes; j++){        
        m[j]= 0;
        while(m[j] <30){
            i= (int) rand()%1200;
            if (arr[i][j] == 0.0) {
                arr[i][j]= (double) rand()/RAND_MAX;
                if(arr[i][j] > 0){
                    m[j]++;
                    printf("arr[%d][%d] = %lft", i, j, arr[i][j]);
                    printf("n");
                }
            }
        }
    }
    // FREEING MEMORY        
//  for (k = 0; k<sizes; i++)    {        
//      free(arr[k]);        
//  }   
    free(arr);  

    getch();

}

My problem is the result of this code was show only some data not the hold data. Could anyone plz help me what's wrong with my code?

Thanks in advance!

Edited by km2011: i'm typing wrong

2
Contributors
6
Replies
7
Views
4 Years
Discussion Span
Last Post by km2011
Featured Replies
  • delete this if statement: if(arr[i][j] > 0){ and this one: if(arr[i][j] > 0){ ~~~ #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <time.h> #define sizes 1200 main() { double** arr; int m[sizes]; int i,j,k; srand((unsigned)time(NULL)); arr= (double **) calloc(sizes,sizeof(double*)); for(k=0; k< sizes; k++){ arr[k]=(double*)calloc(sizes,sizeof(double)); } for(j=0; j< sizes; j++){ for(m[j] = … Read More

0

Not sure what you are trying to accomplish with that code, but the reason that you don't see all the data is probably due to those two if conditions. Remove them and just set each element to some random number.

for(i = 0; i < sizes; i++)
{
    for(j = 0; j < sizes; j++)
        arr[i][j] = rand();
}

Edited by Ancient Dragon: correct

0

Thanks for your reply sir, i can't do like you suggest because i don't want to random the hold elament of columns but i want to random with the fix m[j] . Do you have any other way to slove this problem?

1

delete this if statement: if(arr[i][j] > 0){

and this one: if(arr[i][j] > 0){

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>

#define sizes 1200



 main()
{
    double** arr;
    int m[sizes];
    int i,j,k;

    srand((unsigned)time(NULL));

    arr= (double **) calloc(sizes,sizeof(double*));
    for(k=0; k< sizes; k++){
    arr[k]=(double*)calloc(sizes,sizeof(double));
    }

    for(j=0; j< sizes; j++){        

        for(m[j] = 0; m[j] <30, m[j]++){
            i= (int) rand()%1200;
            arr[i][j]= (double) rand()/RAND_MAX
            printf("arr[%d][%d] = %lft", i, j, arr[i][j]);
            printf("n");
        }
    }
    // FREEING MEMORY        
  for (k = 0; k<sizes; i++)    {        
      free(arr[k]);        

  }   
    free(arr);  

    getch();

}

Edited by Ancient Dragon: because

0

It is showing you all the rows/columns that it populates. If you display them all then you will see that there are lots of cells that were not populated. Print them to a text file instead of on the screen so that you can easily see what cells were or were not populated.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.