resolving a list of numbers to a range

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
 
A

AM

Modify the code as follows:
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[­i]);
else if( (prevNum+1) != atoi(argv) && i==argc-1)
printf("%s",argv[i-1]);


}
prevNum = atoi(argv);
}
}


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);
x=0; /* To display '-' for next series of numbers */
}
}
prevNum = atoi(argv);
}
/* 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==argc-1)
printf("%s",argv[i-1]);
 
C

Coder

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[1]);

printf("%d",seriesStart);

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

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;
}
 
P

PRadyut

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);
can somebody please clarify

I'm running Borland 5.5.1 compiler under windows xp sp2

Thanks
Pradyut
http://pradyut.tk
http://spaces.msn.com/members/oop-edge/
http://groups-beta.google.com/group/oop_programming
India
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[1]);

printf("%d",seriesStart);

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

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

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
 
P

PRadyut

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
Please check the code appropriately

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

Thanks

Pradyut
http://pradyut.tk
http://spaces.msn.com/members/oop-edge/
http://groups-beta.google.com/group/oop_programming
India
Modify the code as follows:
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[­i]);
else if( (prevNum+1) != atoi(argv) && i==argc-1)
printf("%s",argv[i-1]);


}
prevNum = atoi(argv);
}
}


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);
x=0; /* To display '-' for next series of numbers */
}
}
prevNum = atoi(argv);
}
/* 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==argc-1)
printf("%s",argv[i-1]);
 
P

PRadyut

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

Please clarify

Thanks

Pradyut
http://pradyut.tk
http://spaces.msn.com/members/oop-edge/
http://groups-beta.google.com/group/oop_programming
India
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[1]);

printf("%d",seriesStart);

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

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

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
 
O

Old Wolf

placid said:
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))
{
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))
printf("%s,%s",argv[i-1],argv);

else if( (prevNum+1) != atoi(argv) && 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);
}

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.
 
C

Coder

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);
can somebody please clarify

I'm running Borland 5.5.1 compiler under windows xp sp2

Thanks
Pradyut
http://pradyut.tk
http://spaces.msn.com/members/oop-edge/
http://groups-beta.google.com/group/oop_programming
India
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[1]);

printf("%d",seriesStart);

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

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

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
 
R

Richard Bos

[ 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
 
C

Coder

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

Please clarify

Thanks

Pradyut
http://pradyut.tk
http://spaces.msn.com/members/oop-edge/
http://groups-beta.google.com/group/oop_programming
India
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[1]);

printf("%d",seriesStart);

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

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

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
 
P

placid

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;

typedef struct listhead
{
IntNode *head;
int count;
}ListHead

.....

void printList(ListHead * lh)
{
IntNode *current = lh->head;
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
 
W

Walter Roberson

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.]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top