1

Hey guys I am trying to finish my code but instead of getting values I am getting address of the values. Why is that?
Is the algorithm is built right? I need to arrange the array Received by the user sorted. All numbers with the rest of the division by m equal to 0 will appear at the beginning of the array, all the numbers with the rest of the division by m equal to 1 will be followed, with the rest of the two numbers will appear later, and so on. Will last the rest of the numbers with a distribution on m equal to m-1.

This is my output:

output of my code

This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void SortByModulo(int *arr,int m,int length);
void main()
{   int length,m,i;
    int *arr;
    printf("Please inseret array length:\n");
    scanf("%d" ,&length);
    arr=(int *)malloc(length*sizeof(int));
    if(!arr) // Terms - if there is not enough memory,print error msg and exit the program.
        {
            printf("alloc failed\n");
            return ;
        }
    for(i=0; i<length; i++)
        arr[i]=(int)malloc(length*sizeof(int)); // Allocate memory for each row
    printf("Please inseret %d elemetns :\n",length);
    for (i=0 ; i<length ; i++)
        {
            scanf("%d" , arr[i]);
        }
    printf("Insert a natural number that you want to sort by modulo:\n");
    scanf("%d" ,&m);
    SortByModulo(arr,m,length);
    system("pause");
    return;
}
void SortByModulo(int *arr,int m,int length)
{   int i,j,temp,k;
    for ( i=length ; i>1 ; i--)
    {
        for ( j=0 ; j<i-1 ; j++)
            {
                if((arr[j]%m)>(arr[j+1]%m))
                    {
                      temp=arr[j];
                      arr[j]=arr[j+1];
                      arr[j+1]=temp;
                    }

            }
    }
    for (j=0 ; j<length ; j++)
    {
        printf("%d ", arr[j]);
    }
printf("\n");
}
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Sagi Binder
  • 69
  • 3
  • 4
  • 13
  • 2
    Unless you are doing this for academic reasons, just use `qsort` from the standard library. You only have to pass it an appropriate comparison function. – firefrorefiddle Sep 20 '13 at 12:38

1 Answers1

5

First: You have memory leak! and arr[i]=(int)malloc(length*sizeof(int)); is not needed. You need only one 1-D array (declaration of arr is correct). Remove following code:

for(i=0; i<length; i++)
    arr[i]=(int)malloc(length*sizeof(int)); // Allocate memory for each row

Note: Don't cast returned address by malloc() and calloc() functions. read:Do I cast the result of malloc() and calloc()

Second missing & in scanf:

  scanf("%d", arr[i]);
  //          ^ & missing 

should be:

  scanf("%d", &arr[i]);
Community
  • 1
  • 1
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
  • Thanks man! About the casting you are talking about that? if(!arr) – Sagi Binder Sep 20 '13 at 15:32
  • @SagiBinder I mean `arr=(int *)malloc(length*sizeof(int));` is better to write as `arr = malloc(length * sizeof(int));` . Avoid casting e.g your are typecasting `int*`. Read the linked answer. – Grijesh Chauhan Sep 20 '13 at 15:37