Skip to content

Instantly share code, notes, and snippets.

@radiofreejohn
Created March 23, 2011 04:27
Show Gist options
  • Save radiofreejohn/882618 to your computer and use it in GitHub Desktop.
Save radiofreejohn/882618 to your computer and use it in GitHub Desktop.
generate a range of integer values range(start,end) - may be neg to pos and vice versa
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int *range(int, int);
int *range(int start, int end)
{
int size = (int) fabs(end-start)+1;
int *values = malloc(size * sizeof(int));
int i=0;
while (size--)
{
*(values + (i++)) = start;
(start < end) ? start++ : start--;
}
return values;
}
int main()
{
int *vals,i;
vals = range(200,150);
for (i=0;i<=50;i++)
printf("%d ", vals[i]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment