the following problem sorts all sequences of k elements in a descending order...the algorithms are correct, as i tested them on a static allocation program...
i am having trouble with pointers and parameters transmition...
Code:
#include<stdio.h>
#include<stdlib.h>
void sort(int*v,int m,int k)
{
int ok=0;
int last =m+k-1, aux, i;
while(!ok) {
ok=1;
for(i=m; i<last; i++)
if(v[i]<v[i+1]) { aux=v[i];
v[i]=v[i+1];
v[i+1]=aux;
ok=0;
}
last-=1;
}
}
void reading(int*v,int*n)
{
int i;
printf("n= ");
scanf("%d",n);
v=(int*)malloc(*n*sizeof(int));
if(v==NULL)
printf("allocation error!");
for(i=0;i<*n;i++) {
printf("v[%d]= ",i);
scanf("%d",&v[i]);
}
}
void printing(int*v,int n)
{
int i;
for(i=0;i<n;i++)
printf("%d ",v[i]);
}
void sort_array(int*v,int n,int k)
{
int j=0;
while(j<n-k)
{
sort(v,j,k);
j+=k;
}
}
int main()
{
int n, k;
int*v;
printf("k= ");
scanf("%d",&k);
reading(v,&n);
sort_array(v,n,k);
printing(v,n);
printf("\n");
return 0;
}
i am compiling and executing under linux...