I have an array with a lot of numbers. How can I create intervals from these numbers?

what's the value of the intervals?
I see in your example that the first set has a length of 5 but the next one only has 2 and at the last is probably for the remaining elements, is there a pattern for this?

once you know the value of the …

Answered by WaltP 2,905 in a post from

Why 0-5, 6-9, 12-13?
Why not 0-3, 4-8, 9-13?
Or 0-1, 2-3, 4-5, 6-8, 9-13?

My attempt is here, but there is a problem with this:

So what's the problem with this?

Because 7 , 10 and 11 is not in the array.

what does missing numbers have to do with intervals?

So the length is not important. The intervals will be different long.

I meant that the value of the interval is important to act as a …

Okay this is how I understand it

so the point is numbers whose next value is missing will always be in a set regardless of the length between these numbers
e.g.
`input: 0, 2, 3, 4, 5, 6`
`output: 0, 2-6`

`input: 0, 2, 4, 5, 6`
`output: 0-2, 4-6`

`input: 0, 1, …`

All 16 Replies

Well, this is obviously homework, but fine...

The easiest way to do it is to increment i(and n!) by a value of your choice when you read the number.
Example :

``````#include<stdio.h>
int v[1001];
int main(){
int i,n;
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%d",&v[i]);
i+=4;
n+=4;
}
return 0;
}
``````

If you print the entire array, it's probably going to show something along these lines :

``````1 0 0 0 0 2 0 0 0 0 3 0 0 0 0...
``````

So, there you have it, the easiest way to do it. Now go and actually learn something, not just copy it from us.

commented: No, not fine. We don't do other's homework for them. -3

Yes, I see, but my problem is not this. I have an array with numbers and I should create intervals from these numbers.
Exapmle:
Array: `0 1 2 3 4 5 6 8 9 12 13`
Output:`0 - 5 , 6 - 9, 12 - 13`

My attempt is here, but there is a problem with this:

``````  tomb3[0]=tomb2[0];
i=1;

for(k=1;k<temp;k++){
if(tomb2[k+1] != tomb2[k]+1){
tomb3[i] = tomb2[k];
i++;
}
}
``````

what's the value of the intervals?
I see in your example that the first set has a length of 5 but the next one only has 2 and at the last is probably for the remaining elements, is there a pattern for this?

once you know the value of the interval all you need to do is assign the value per interval to the output array

Why 0-5, 6-9, 12-13?
Why not 0-3, 4-8, 9-13?
Or 0-1, 2-3, 4-5, 6-8, 9-13?

My attempt is here, but there is a problem with this:

So what's the problem with this?

Why 0-5, 6-9, 12-13?

Because 7 , 10 and 11 is not in the array.

And what's the problem with this?

This code gives wrong result, but I do not know why.

I see in your example that the first set has a length of 5 but the next one only has 2 and at the last is probably for the remaining elements, is there a pattern for this?

Because 7 , 10 and 11 is not in the array. So the length is not important. The intervals will be different long.

Because 7 , 10 and 11 is not in the array.

what does missing numbers have to do with intervals?

So the length is not important. The intervals will be different long.

I meant that the value of the interval is important to act as a counter on which value to store on the other array. If the interval value is unknown then the next value to store is guesswork

This code gives wrong result, but I do not know why.

we can't tackle the coding part of this until we get a clear picture on how the logic on the interval works

what does missing numbers have to do with intervals?

The reason of the exercise are the missing numbers. There is nothing to do with the missing numbers, but I should create intervals from the existing numbers, beacuse of the missing numbers. If there is no missing number the whole array will be only 1 interval.

Okay this is how I understand it

so the point is numbers whose next value is missing will always be in a set regardless of the length between these numbers
e.g.
`input: 0, 2, 3, 4, 5, 6`
`output: 0, 2-6`

`input: 0, 2, 4, 5, 6`
`output: 0-2, 4-6`

`input: 0, 1, 2, 9, 10`
`output: 0-1, 2, 9-10`

`input: 2, 4, 6, 9, 10`
`output: 2-6, 9-10`

is this right?

is this right?

No, the sequential numbers should be in one interval like in my exapmle.

huh? in my example the sequential numbers are in a set and those with next values missing are in another...

could you tell which example I did is wrong and post how it should have been done

also maybe you should be the one giving more examples

Some more examples:

input: `0, 1, 2, 9, 10`
output: `0-2, 9-10`

input: `0, 2, 4, 5, 6`
output: `0, 2, 4-6`

input: `0 1 2 3 4 5 6 7 8 9 10`
output: `0-10`

can numbers whose next or previous values be in a set like in your first example?

Array: 0 1 2 3 4 5 6 8 9 12 13
Output:0 - 5 , 6 - 9, 12 - 13

cause this contradicts the 2nd example in your last post where 0 and 2 is not in a set
if not then shoudn't the output of your first example be like
`0 - 6, 8 - 9, 12 - 13`

If the present number+1 not equal to the next number, the next number will start the next interval.

If the present number+1 not equal to the next number, the next number will start the next interval.

`0 - 6, 8 - 9, 12 - 13`
so this would be the correct output of your very 1st example right?

``````While loop until no more numbers
set intervalStart to current number
Loop until you don't get the "next number" in sequence
Set previous number to intervalEnd
print interval
endWhile
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.