1 Replies - 435 Views - Last Post: 04 October 2012 - 12:41 PM Rate Topic: -----

#1 Vans1228  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 38
  • Joined: 04-July 10

Sorting Malloc list of numbers

Posted 04 October 2012 - 12:18 PM

Hey guys,
I am working on a program that generates a random list of numbers with the size denoted by the user through keyboard input and then sorts the list. I cannot figure out why the qsort function is not working. Any ideas?

isort.c
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "sort_integer_array.h"

void copy(int *t, int *s, int size);
void print_list(int *l, int size);
void reset(int *p, int size);

int main()
{
	srandom(time(NULL));
	printf("How many numbers will the list have? ");
	int size;
	scanf("%d", &size);

	int *p, *a, *begin, *end;
	p = malloc(size * sizeof(int));
	if(p == NULL)
	{
		perror("malloc returned NULL");
		exit(1);
	}

	a = malloc(size * sizeof(int));
	if(a == NULL)
	{
		perror("malloc returned NULL");
		exit(1);
	}
	/*d = malloc(size * sizeof(int));
	if(d == NULL)
	{
		perror("malloc returned NULL");
		exit(1);
	}*/
	int i;
	printf("The original list of numbers:\n");
	for(i = 0; i < size; i++)
	{
		*p = random();
		printf("%d\n", *p);
		p++;
	}
	copy(a, p, size);
	
	end = a;

	for(i = size; i > 0; i--)
	{
		p--;
	}
	
	begin = a;

	printf("The list in ascending order:\n");
	sort_integer_array(begin, end, 1);
	print_list(a, size);
	
		
	free(p);
	free(a);
	//free(d);
	return 0;
}

void copy(int *t, int *s, int size)
{
	int i;
	for(i = size; i > 0; i--)
		s--;	
	for(i = 0; i < size; i++)
	{
		*t = *s;
		s++;
		t++; 
	}
}

void print_list(int *l, int size)
{
	int i;
	for(i = 0; i < size; i++)
	{
		printf("%d\n", *l);
		l++;	
	}
}  




sort_integer_array.c
#include <stdio.h>
#include "sort_integer_array.h"
#include <stdlib.h>

void sort_integer_array(int *begin, int *end, int ascending)
{
	int size = end - begin;
	if(ascending == 1)
	{
		qsort(begin, size, sizeof(int), compInt);
	}
}

int compInt(void *b, void *e)
{
	int x = *(int *)b;
	int y = *(int *)e;

	if(x > y)
		return 1;
	else if(x == y)
		return 0;
	else
		return -1;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Sorting Malloc list of numbers

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3469
  • View blog
  • Posts: 10,698
  • Joined: 05-May 12

Re: Sorting Malloc list of numbers

Posted 04 October 2012 - 12:41 PM

From what I can see, begin and end point to the same location (see lines 54 and 47 respectively). So when you compute size in sort_integer_array(), you have a zero elements to sort.

My recommendations are:
1. Use descriptive names for your arrays: p and a don't cut it.
2. If you are going to iterate over an array using pointers, use a different pointer so that you don't have to move the pointer back to the beginning after you've iterated through it.
3. If you really insist on moving you pointer forward instead of using a different pointer, you are using pointer math in one of your functions, why aren't you using pointer math to move you pointer back to the beginning?
4. Since you now the size of the arrays you've allocated, why not use pointer math to determine the end of the array?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1