469,917 Members | 1,673 Online

resolving a list of numbers to a range

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);

for(i=1;i<argc;i++)
{
if(prevNum > 0)
{
if( (prevNum+1) == atoi(argv[i]))
{
if(x==0)
{
printf("-");
x=1;
}
}
else if( (prevNum+1) != atoi(argv[i]))
printf("%s,%s",argv[i-1],argv[i]);
else if( (prevNum+1) != atoi(argv[i]) && i==argc-1)
printf("%s",argv[i-1]);

}
prevNum = atoi(argv[i]);
}
}

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

Nov 15 '05 #1
13 1451 Modify the code as follows:
for(i=1;i<argc;i++)
{
if(prevNum > 0)
{
if( (prevNum+1) == atoi(argv[i]))
{
if(x==0)
{
printf("-");
x=1;
}
}
else if( (prevNum+1) != atoi(argv[i]))
printf("%s,%s",argv[i-1],argv[*i]);
else if( (prevNum+1) != atoi(argv[i]) && i==argc-1)
printf("%s",argv[i-1]);
}
prevNum = atoi(argv[i]);
}
}
for(i=1;i<argc;i++)
{
if(prevNum > 0)
{
if( (prevNum+1) == atoi(argv[i]))
{
if(x==0)
{
printf("-");
x=1;
}
}
else if( (prevNum+1) != atoi(argv[i]))
{
printf("%s,%s",argv[i-1],argv[i]);
x=0; /* To display '-' for next series of numbers */
}
}
prevNum = atoi(argv[i]);
}
/* To fix your problem */
if ( x == 1 )
printf("%s",argv[i-1]);

Also you don't need following lines anymore. else if( (prevNum+1) != atoi(argv[i]) && i==argc-1)
printf("%s",argv[i-1]);

Nov 15 '05 #2
Following code also does the same thing but uses a different logic ....
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int main(int argc, char*argv[])
{
int i=0;
int seriesStart=0;
int curNum=0,prevNum=0;

if(argc < 1)
exit(EXIT_SUCCESS);

printf("\n");

seriesStart = prevNum = atoi(argv);

printf("%d",seriesStart);

for(i = 2; i < argc; i++)
{
curNum = atoi(argv[i]);

if(curNum != (prevNum+1))
{
if(seriesStart != prevNum)
printf(" - %d,",prevNum);
else
printf(",");

seriesStart = curNum;
printf("%d",seriesStart);

}
prevNum = curNum;
}

if((seriesStart != curNum) && (curNum == prevNum))
printf(" - %d",curNum);

return 0;
}
placid wrote:
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);

for(i=1;i<argc;i++)
{
if(prevNum > 0)
{
if( (prevNum+1) == atoi(argv[i]))
{
if(x==0)
{
printf("-");
x=1;
}
}
else if( (prevNum+1) != atoi(argv[i]))
printf("%s,%s",argv[i-1],argv[i]);
else if( (prevNum+1) != atoi(argv[i]) && i==argc-1)
printf("%s",argv[i-1]);

}
prevNum = atoi(argv[i]);
}
}

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

Nov 15 '05 #3
i went through your code and found that it only responds to arguments
if given
else it would exit. But unlike the code "AM" gave out it, this code
doesn't exit out silently. It returns a runtime error and halts the
system process. Although both the codes contain the same
if (argc < 1 )
exit(EXIT_SUCCESS);

I'm running Borland 5.5.1 compiler under windows xp sp2

Thanks
http://spaces.msn.com/members/oop-edge/
India

Coder wrote:
Following code also does the same thing but uses a different logic ....
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int main(int argc, char*argv[])
{
int i=0;
int seriesStart=0;
int curNum=0,prevNum=0;

if(argc < 1)
exit(EXIT_SUCCESS);

printf("\n");

seriesStart = prevNum = atoi(argv);

printf("%d",seriesStart);

for(i = 2; i < argc; i++)
{
curNum = atoi(argv[i]);

if(curNum != (prevNum+1))
{
if(seriesStart != prevNum)
printf(" - %d,",prevNum);
else
printf(",");

seriesStart = curNum;
printf("%d",seriesStart);

}
prevNum = curNum;
}

if((seriesStart != curNum) && (curNum == prevNum))
printf(" - %d",curNum);

return 0;
}
placid wrote:
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);

for(i=1;i<argc;i++)
{
if(prevNum > 0)
{
if( (prevNum+1) == atoi(argv[i]))
{
if(x==0)
{
printf("-");
x=1;
}
}
else if( (prevNum+1) != atoi(argv[i]))
printf("%s,%s",argv[i-1],argv[i]);
else if( (prevNum+1) != atoi(argv[i]) && i==argc-1)
printf("%s",argv[i-1]);

}
prevNum = atoi(argv[i]);
}
}

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

Nov 15 '05 #4
Your code doesn't respond to 1 3 4 5 6 7 8 (2 missing)

It only responds for 1 2 3 4 5 6 7 8

btw my compiler borland 5.5.1 halts the system process after
encountering the problem under windows xp sp2, any suggestions for
that...

Thanks

http://spaces.msn.com/members/oop-edge/
India

AM wrote:
Modify the code as follows:
for(i=1;i<argc;i++)
{
if(prevNum > 0)
{
if( (prevNum+1) == atoi(argv[i]))
{
if(x==0)
{
printf("-");
x=1;
}
}
else if( (prevNum+1) != atoi(argv[i]))
printf("%s,%s",argv[i-1],argv[*i]);
else if( (prevNum+1) != atoi(argv[i]) && i==argc-1)
printf("%s",argv[i-1]);
}
prevNum = atoi(argv[i]);
}
}

for(i=1;i<argc;i++)
{
if(prevNum > 0)
{
if( (prevNum+1) == atoi(argv[i]))
{
if(x==0)
{
printf("-");
x=1;
}
}
else if( (prevNum+1) != atoi(argv[i]))
{
printf("%s,%s",argv[i-1],argv[i]);
x=0; /* To display '-' for next series of numbers */
}
}
prevNum = atoi(argv[i]);
}
/* To fix your problem */
if ( x == 1 )
printf("%s",argv[i-1]);

Also you don't need following lines anymore.
else if( (prevNum+1) != atoi(argv[i]) && i==argc-1)
printf("%s",argv[i-1]);

Nov 15 '05 #5
In your code if i changed the type of seriesStartm curNum and prevNum
to float and gave in the command line
series1 1.1 1.2 1.3 1.4 1.5
I get the o/p
1.10,0,0,0,0

while using int i get o/p

1.11,1,1,1,1

Thanks

http://spaces.msn.com/members/oop-edge/
India

Coder wrote:
Following code also does the same thing but uses a different logic ....
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int main(int argc, char*argv[])
{
int i=0;
int seriesStart=0;
int curNum=0,prevNum=0;

if(argc < 1)
exit(EXIT_SUCCESS);

printf("\n");

seriesStart = prevNum = atoi(argv);

printf("%d",seriesStart);

for(i = 2; i < argc; i++)
{
curNum = atoi(argv[i]);

if(curNum != (prevNum+1))
{
if(seriesStart != prevNum)
printf(" - %d,",prevNum);
else
printf(",");

seriesStart = curNum;
printf("%d",seriesStart);

}
prevNum = curNum;
}

if((seriesStart != curNum) && (curNum == prevNum))
printf(" - %d",curNum);

return 0;
}
placid wrote:
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);

for(i=1;i<argc;i++)
{
if(prevNum > 0)
{
if( (prevNum+1) == atoi(argv[i]))
{
if(x==0)
{
printf("-");
x=1;
}
}
else if( (prevNum+1) != atoi(argv[i]))
printf("%s,%s",argv[i-1],argv[i]);
else if( (prevNum+1) != atoi(argv[i]) && i==argc-1)
printf("%s",argv[i-1]);

}
prevNum = atoi(argv[i]);
}
}

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

Nov 15 '05 #6
placid wrote:

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?

for(i=1;i<argc;i++)
{
if(prevNum > 0)
{
if( (prevNum+1) == atoi(argv[i]))
{
if(x==0)
{
printf("-");
x=1;
This means you will only ever get one dash ... so your code
cannot handle 1 2 3 5 6 7 (which should produce 1-3,5-7).
}
}
else if( (prevNum+1) != atoi(argv[i]))
printf("%s,%s",argv[i-1],argv[i]); else if( (prevNum+1) != atoi(argv[i]) && i==argc-1)
printf("%s",argv[i-1]);
This last section can never execute. Your first two cases
were exhaustive -- every possible case must fall into
either (A == B) or (A != B) , for all A and B.

}
prevNum = atoi(argv[i]);
}

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

To cut a long story short, your program logic is wrong.
I would recommend you write a flow-chart or some other
natural-language description of how the program logic
should work. Trace it through for various examples
(eg. 1 2 3 4 like you suggested). Once you're sure that
the steps are right, then start coding.

Nov 15 '05 #7
the code that is causing the runtime error is not

if (argc < 1 )
exit(EXIT_SUCCESS);

if we don't give any command line arguements to the program then argc
will be 1 and the argv contains the path of the program from the
root.instead of checking argc for its equality with 1 we used <
operator because of which the program is not terminated even if there
are no arguements supplied to it.had we used - is equal to ( == )
operator istead of less than ( < ) operator the error would not have
been there.

The correct code should be ...

if (argc == 1 )
exit(EXIT_SUCCESS);
i went through your code and found that it only responds to arguments
if given
else it would exit. But unlike the code "AM" gave out it, this code
doesn't exit out silently. It returns a runtime error and halts the
system process. Although both the codes contain the same
if (argc < 1 )
exit(EXIT_SUCCESS);

I'm running Borland 5.5.1 compiler under windows xp sp2

Thanks
http://spaces.msn.com/members/oop-edge/
India

Coder wrote:
Following code also does the same thing but uses a different logic ....
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int main(int argc, char*argv[])
{
int i=0;
int seriesStart=0;
int curNum=0,prevNum=0;

if(argc < 1)
exit(EXIT_SUCCESS);

printf("\n");

seriesStart = prevNum = atoi(argv);

printf("%d",seriesStart);

for(i = 2; i < argc; i++)
{
curNum = atoi(argv[i]);

if(curNum != (prevNum+1))
{
if(seriesStart != prevNum)
printf(" - %d,",prevNum);
else
printf(",");

seriesStart = curNum;
printf("%d",seriesStart);

}
prevNum = curNum;
}

if((seriesStart != curNum) && (curNum == prevNum))
printf(" - %d",curNum);

return 0;
}
placid wrote:
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);

for(i=1;i<argc;i++)
{
if(prevNum > 0)
{
if( (prevNum+1) == atoi(argv[i]))
{
if(x==0)
{
printf("-");
x=1;
}
}
else if( (prevNum+1) != atoi(argv[i]))
printf("%s,%s",argv[i-1],argv[i]);
else if( (prevNum+1) != atoi(argv[i]) && i==argc-1)
printf("%s",argv[i-1]);

}
prevNum = atoi(argv[i]);
}
}

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

Nov 15 '05 #8
"Coder" <ra**********@gmail.com> wrote:

[ Don't top-post, dammit. And do learn to snip. ]
the code that is causing the runtime error is not

if (argc < 1 )
exit(EXIT_SUCCESS);

if we don't give any command line arguements to the program then argc
will be 1 and the argv contains the path of the program from the
root.
This is not necessarily true. It is legal for the startup code not to
provide any members of argv, not even the program name. In this case,
argc will be 0, and argv will consist of an array containing only a null
pointer.
The correct code should be ...

if (argc == 1 )
exit(EXIT_SUCCESS);

And therefore, this correction is unsafe. It should be neither < nor ==,
but

if (argc <= 1 )
exit(EXIT_SUCCESS);

Richard
Nov 15 '05 #9
when variables declared with int datatype are given float values the
float values are truncated to its nearest int number.so the output
1,1,1 for the input - 1.1 1.2 1.3 makes sense . But when the input is
float and variables are also float the output is not as expected that
is because we use atoi to convert stings to ints not to floats.so
instead of atoi we should use atof and declare variables with double
datatype.along with these changes logic also needs to be changed to
make the program work with float values.
In your code if i changed the type of seriesStartm curNum and prevNum
to float and gave in the command line
series1 1.1 1.2 1.3 1.4 1.5
I get the o/p
1.10,0,0,0,0

while using int i get o/p

1.11,1,1,1,1

Thanks

http://spaces.msn.com/members/oop-edge/
India

Coder wrote:
Following code also does the same thing but uses a different logic ....
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int main(int argc, char*argv[])
{
int i=0;
int seriesStart=0;
int curNum=0,prevNum=0;

if(argc < 1)
exit(EXIT_SUCCESS);

printf("\n");

seriesStart = prevNum = atoi(argv);

printf("%d",seriesStart);

for(i = 2; i < argc; i++)
{
curNum = atoi(argv[i]);

if(curNum != (prevNum+1))
{
if(seriesStart != prevNum)
printf(" - %d,",prevNum);
else
printf(",");

seriesStart = curNum;
printf("%d",seriesStart);

}
prevNum = curNum;
}

if((seriesStart != curNum) && (curNum == prevNum))
printf(" - %d",curNum);

return 0;
}
placid wrote:
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);

for(i=1;i<argc;i++)
{
if(prevNum > 0)
{
if( (prevNum+1) == atoi(argv[i]))
{
if(x==0)
{
printf("-");
x=1;
}
}
else if( (prevNum+1) != atoi(argv[i]))
printf("%s,%s",argv[i-1],argv[i]);
else if( (prevNum+1) != atoi(argv[i]) && i==argc-1)
printf("%s",argv[i-1]);

}
prevNum = atoi(argv[i]);
}
}

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

Nov 15 '05 #10

Richard Bos wrote:
"Coder" <ra**********@gmail.com> wrote:

[ Don't top-post, dammit. And do learn to snip. ]
Thanks for the correction . I am new to newsgroups.What is top-posting
, learning to snip ?

the code that is causing the runtime error is not

if (argc < 1 )
exit(EXIT_SUCCESS);

if we don't give any command line arguements to the program then argc
will be 1 and the argv contains the path of the program from the
root.

This is not necessarily true. It is legal for the startup code not to
provide any members of argv, not even the program name. In this case,
argc will be 0, and argv will consist of an array containing only a null
pointer.
The correct code should be ...

if (argc == 1 )
exit(EXIT_SUCCESS);

And therefore, this correction is unsafe. It should be neither < nor ==,
but

if (argc <= 1 )
exit(EXIT_SUCCESS);

Richard

Nov 15 '05 #11
Thanks everyone i got it to work at last but how would we resolve
numbers inside a integer linked-list and ive been writing out
flow-chart for this problem but i cant seem to be getting my head
around it

here is a code that i wanted

typedef struct intnode
{
int data;
struct intnode *next;
}IntNode;

{
int count;

.....

{
int prevNum =0, x=0;
while (current != NULL)
{
if (prevNum > 0)
{
if( (prevNum+1) == current->data)
{
if(x==0)
{
printf("-");
x=1;
}
}
else if( (prevNum+1) != current->data)
{
printf("%s,%s",prevnum,current->data);
x=0; /* To display '-' for next series of numbers */
}
}
}
prevNum = current->data;
current = current->next;
}
}
the problem i think was i was thinking of a solution to this problem if
arrays where used now that i want to change it to LL structure im
confused, so any help will be appreciated again

Nov 15 '05 #12
placid <Bu****@gmail.com> wrote:
Thanks everyone i got it to work at last but how would we resolve
numbers inside a integer linked-list and ive been writing out
flow-chart for this problem but i cant seem to be getting my head
around it

Are your inputs always in sorted order? If not, then can your
code "afford" to sort them first, or does it need to do the insertions
on the fly? Are values ever removed from the list (e.g., you
have 1-7 and you remove 3, leaving 1-2 and 4-7 ?)

I have code that handles insertions and deletions and uses linked
lists. There are a couple of cases that aren't immediately obvious,
but once you've worked out all the boundary conditions then the
code comes out fairly clean. [The code was originally written
with the context of building IP ranges, but it works with any 32 bit
unsigned values.]
--
Any sufficiently old bug becomes a feature.
Nov 15 '05 #13
my code always inserts node in ascending sort order and yes nodes can
be deleted too,

Nov 15 '05 #14

 7 posts views Thread by Klaus Neuner | last post: by 17 posts views Thread by Thomas M. | last post: by 6 posts views Thread by placid | last post: by 4 posts views Thread by tron.thomas | last post: by 12 posts views Thread by Ju Hui | last post: by 22 posts views Thread by joh12005 | last post: by 12 posts views Thread by Godzilla | last post: by 10 posts views Thread by erikcw | last post: by 5 posts views Thread by globalrev | last post: by reply views Thread by eddparker01 | last post: by reply views Thread by eddparker01 | last post: by reply views Thread by lanliddd | last post: by 1 post views Thread by Waqarahmed | last post: by 2 posts views Thread by Waqarahmed | last post: by reply views Thread by Salome Sato | last post: by 9 posts views Thread by anoble1 | last post: by 1 post views Thread by skydivetom | last post: by reply views Thread by WIPE | last post: by