Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I am sure your compiler told you what was wrong.</p> <p>You are passing a <code>long**</code> to a function that expects a <code>int*</code> at the line</p> <pre><code>fill_array(&amp;arrayIn, n); </code></pre> <p>function prototype is</p> <pre><code>void fill_array(int *arr, int n) </code></pre> <p>Same problem with the other function. From there, anything can happen.</p> <p>Always, ALWAYS heed the warnings your compiler gives you.</p> <p><strong>MAJOR EDIT</strong></p> <p>First - yes, the name of an array is already a pointer.</p> <p>Second - declare a function prototype at the start of your code; then the compiler will throw you helpful messages which will help you catch these</p> <p>Third - if you want to pass the address of a simple variable to a function, there is no need for a <code>malloc</code>; just use the address of the variable.</p> <p>Fourth - the <code>rand()</code> function returns an integer between 0 and <code>RAND_MAX</code>. The code</p> <pre><code>a[i] = (RAND_MAX + 1) * rand() + rand(); </code></pre> <p>is a roundabout way of getting </p> <pre><code>a[i] = rand(); </code></pre> <p>since <code>(RAND_MAX + 1)</code> will overflow and give you zero... If you actually wanted to be able to get a "really big" random number, you would have to do the following:</p> <p>1) make sure <code>a</code> is a <code>long *</code> (with the correct prototypes etc)</p> <p>2) convert the numbers before adding / multiplying: </p> <pre><code>a[i] = (RAND_MAX + 1L) * rand() + rand(); </code></pre> <p>might do it - or maybe you need to do some more casting to <code>(long)</code>; I can never remember my order of precedence so I usually would do</p> <pre><code>a[i] = ((long)(RAND_MAX) + 1L) * (long)rand() + (long)rand(); </code></pre> <p>to be 100% sure.</p> <p>Putting these and other lessons together, here is an edited version of your code that compiles and runs (I did have to "invent" a <code>print_array</code>) - I have written comments where the code needed changing to work. The last point above (making long random numbers) was not taken into account in this code yet.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; // include prototypes - it helps the compiler flag errors: void fill_array(int *arr, int n); void InsertionSort(int *arr, int n, int *swap_count, int *compare_count); void print_array(int *arr, int n, int *swap_count, int *compare_count); int main(void) { // change data type to match function int *arrayIn; // instead of mallocing, use a fixed location: int swap_count, compare_count; // often a good idea to give your pointers a _p name: int *swap_count_p = &amp;swap_count; int *compare_count_p = &amp;compare_count; // the pointer must not be set to zero: it's the CONTENTs that you set to zero *compare_count_p = 0; *swap_count_p = 0; int i, j; for (j = 10; j &lt;= 1000; j*=10) { for (i = 1; i &lt;= 5; i++){ if (i == 1 || i == 2 || i == 5) { int n = i * j; arrayIn = malloc(sizeof(long)*n); fill_array(arrayIn, n); InsertionSort(arrayIn, n, swap_count_p, compare_count_p); print_array(arrayIn, n, swap_count_p, compare_count_p); swap_count = 0; compare_count = 0; free(arrayIn); } } } return 0; } void fill_array(int *arr, int n) { int i; for (i = 0; i &lt; n; i++) { // arr[i] = (RAND_MAX + 1)*rand() + rand(); // causes integer overflow arr[i] = rand(); } } void InsertionSort(int *arr, int n, int *swap_count, int *compare_count) { int i, j, t; for (j = 0; j &lt; n; j++) { (*compare_count)++; t = arr[j]; i = j - 1; *swap_count = *swap_count + 2; while (i &gt;= 0 &amp;&amp; arr[i]&gt;t) { //tady chybí compare_count inkrementace *compare_count = *compare_count + 2; arr[i + 1] = arr[i]; (*swap_count)++; i--; (*swap_count)++; } arr[i + 1] = t; (*swap_count)++; } } void print_array(int *a, int n, int* sw, int *cc) { int ii; for(ii = 0; ii &lt; n; ii++) { if(ii%20 == 0) printf("\n"); printf("%d ", a[ii]); } printf("\n\nThis took %d swaps and %d comparisons\n\n", *sw, *cc); } </code></pre>
 

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