Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Don't de-reference <code>centroid</code> when passing in. Doing that will only pass your first row. Do this:</p> <pre><code>initialize_centroids( centroid, noofcentroids, newnoofvar ); </code></pre> <p>Then you need to use the correct type here:</p> <pre><code>void initialize_centroids( int **carray, int p, int q ) </code></pre> <p>Your array is presumably an <code>int**</code>. That means it's a pointer to a location in memory that contains an array of <code>int*</code> pointers. Each of those pointers (I assume) references memory that has been allocated one row of your array.</p> <p>Now, once you obtain one of these pointers by doing <code>carray[i]</code> you have selected row <code>i</code>. You now have an <code>int*</code> which means it points to an array of <code>int</code>.</p> <p>Finally, <code>carray[i][j]</code> selects a specific integer from column <code>j</code> on row <code>i</code>.</p> <p><em>[edit]</em></p> <p>Theory about what might be going wrong... You may not be allocating a 2D array in a dynamic sense, or indeed it might be a 1D array and you are supposed to use stride-lengths etc. Here is a solution that you can use for allocating a 2D array dynamically, and indexing it as <code>carray[i][j]</code>.</p> <p><em>Note: Some people have gotten upset at me for this method in the past due to alignment concerns, but I have never had issues. I think they were just having a grumpy day. For those who are worried about alignment, it's easy to adjust for that, but is too complicated for this example.</em></p> <pre><code>int** allocate_array2d( int rows, int cols ) { int i, **arr, *data; arr = (int**)malloc( rows * sizeof(int*) + rows*cols*sizeof(int) ); data = (int*)(arr + rows); for( i = 0; i &lt; rows; i++ ) { rows[i] = data; data += cols; } return arr; } void free_array2d( int** arr ) { free((void*)arr); } </code></pre> <p>Now, in your main:</p> <pre><code>int** centroid = allocate_array2d( noofcentroids, newnoofvar ); initialize_centroids( centroid, noofcentroids, newnoofvar ); </code></pre> <p><em>[more...]</em></p> <blockquote> <p>Okay.Yes I allocated memory like this centroid[100][100] – Bobby 11 mins ago</p> </blockquote> <p>In that case, you should declare:</p> <pre><code>void initialize_centroids( int carray[100][100], int p, int q ) { //... } </code></pre> <p>No need to use my dynamic array example</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload