0

I was trying to answer this question and after writing this code

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

int M =10;
int N =30;
int K = 10;

struct element {
    int *i;
    int *j;
    int k;
};

struct element *create_structure();
void print_element(struct element *);
int compare (const void *, const void * );
void sort(struct element *); // changed the return value of sort
// to void as the argument will be changed directly because it is a
// pointer


int main()
{
    srand(time(NULL));
    struct element *lista;
    lista=create_structure();
    printf("\n--------- i ---  j  ---------\n\n");
    print_element(lista);
    printf("\n---------------------------\n");

    sort(lista);
    print_element(lista);
    return 0;

}


struct element *create_structure()
{
    int aux1=0,count=0;
    struct element *structure;
    // Changed the allocation of structure pointer
    structure = (struct element *) malloc (sizeof(struct element));
    structure->k=K;
    structure->i= (int *)malloc(K*sizeof(int));
    structure->j=(int *)malloc (K*sizeof(int));
    for (count = 0; count < K; count ++)
    {
        aux1=rand()%N;
        // we kept only the first aux1 and copied it in the two arrays
        (structure->i)[count]=aux1;
        (structure->j)[count]=aux1;
    }
    return (structure);
}

void print_element(struct element *lista)
{
    int count=0;
    for(count = 0; count < K; count++)
    {
        printf("row=%2d :  %2d     %2d\n",count+1,(lista->i)[count],(lista->j)[count]);
    }
}


int compare(const void *a, const void *b)
{
    // compare the values of two case of array pointed by i of type int
    return *(int*)a-*(int*)b;
}


void sort(struct element *list)
{
  // we will sort the array pointed by i which contains K elements
  // of type int and size sizeof(int) by using the compare function
    qsort(list->i, K , sizeof(int), compare);
}

I realized that compiling it with gcc command from terminal gives correct output but using the codeblocks v13.12 which uses the same internal GNU GCC compiler gives wrong output !!

The output of gcc command: gcc -g -Wall main.c -o exec

gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)

--------- i ---  j  ---------

row= 1 :   8      8
row= 2 :  29     29
row= 3 :   9      9
row= 4 :  11     11
row= 5 :  20     20
row= 6 :  21     21
row= 7 :   4      4
row= 8 :  16     16
row= 9 :   9      9
row=10 :   2      2

---------------------------
row= 1 :   2      8
row= 2 :   4     29
row= 3 :   8      9
row= 4 :   9     11
row= 5 :   9     20
row= 6 :  11     21
row= 7 :  16      4
row= 8 :  20     16
row= 9 :  21      9
row=10 :  29      2

the output of codeblocks (please note the number of the starting row 3)

row= 3 :  20     20
row= 4 :   4      4
row= 5 :  13     13
row= 6 :   8      8
row= 7 :   4      4
row= 8 :   7      7
row= 9 :  21     21
row=10 :  12     12

---------------------------
row= 1 :   4     24
row= 2 :   4     22
row= 3 :   7     20
row= 4 :   8      4
row= 5 :  12     13
row= 6 :  13      8
row= 7 :  20      4
row= 8 :  21      7
row= 9 :  22     21
row=10 :  24     12

enter image description here

So I was wondering what can lead to such behavior ?

Community
  • 1
  • 1

1 Answers1

0

I have tried out the code in my Code::Blocks (Windows 8, Code::Blocks 13.12, GCC 4.7.1), it writes the output properly! It may be, that your terminal emulator is cropping the program output (or something along these lines). I would log the output to a file to make sure.

After you have posted the screenshot, I am now sure of this.

meskobalazs
  • 15,741
  • 2
  • 40
  • 63
  • I am getting the same behavior as OP !! – tissa Jan 22 '15 at 15:56
  • Then this is a Linux specific Code::Blocks bug (or maybe Ubuntu specific) - I cannot test this right now, in Windows 8 it works fine. I will fire up my own machine after work, and take a look. – meskobalazs Jan 22 '15 at 15:58