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:
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");
}