P
placid
Hi all,
I was just if anyone knows a way to resolve a list of integers say
{1,2,3,4,5} into a range that is 1-5?
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int main(int argc, char*argv[])
{
int i=0;
int prevNum = 0;
int x=0;
if(argc < 1)
exit(EXIT_SUCCESS);
printf("\n");
printf("%s",argv[1]);
for(i=1;i<argc;i++)
{
if(prevNum > 0)
{
if( (prevNum+1) == atoi(argv))
{
if(x==0)
{
printf("-");
x=1;
}
}
else if( (prevNum+1) != atoi(argv))
printf("%s,%s",argv[i-1],argv);
else if( (prevNum+1) != atoi(argv) && i==argc-1)
printf("%s",argv[i-1]);
}
prevNum = atoi(argv);
}
}
this is what i was able to come up with, but for some reason it works
for numbers like , 1 2 3 4 7 which prints out 1-4,7 but if you input
numbers of 1 2 3 4 it outputs 1
I was just if anyone knows a way to resolve a list of integers say
{1,2,3,4,5} into a range that is 1-5?
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
int main(int argc, char*argv[])
{
int i=0;
int prevNum = 0;
int x=0;
if(argc < 1)
exit(EXIT_SUCCESS);
printf("\n");
printf("%s",argv[1]);
for(i=1;i<argc;i++)
{
if(prevNum > 0)
{
if( (prevNum+1) == atoi(argv))
{
if(x==0)
{
printf("-");
x=1;
}
}
else if( (prevNum+1) != atoi(argv))
printf("%s,%s",argv[i-1],argv);
else if( (prevNum+1) != atoi(argv) && i==argc-1)
printf("%s",argv[i-1]);
}
prevNum = atoi(argv);
}
}
this is what i was able to come up with, but for some reason it works
for numbers like , 1 2 3 4 7 which prints out 1-4,7 but if you input
numbers of 1 2 3 4 it outputs 1