Finding Number of iterations

Discuss all kind of algorithms and data structures from their mathematical and programming sides.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

Finding Number of iterations

Postby ranjanrakesh » Thu Mar 20, 2008 7:39 am

Hi all,

[input]
array of size n
window size: int
overlapvar : int

we have to read elements from array as per window size, and from next iteration we have to introduce overlap factor too. i.e if there are 11 elements X1,...X11. with window size = 3, and overlap which will be less than window size, so it can be either 0,1,2 in above case. so we will be reading array as follows:

x1,x2,x3
x2,x3,x4,
x3,x4,x5,
x4,x5,x6,
x5,x6,x7,
x8,x9,x10,
x9,x10,x11

taking window size = 3 and overlap as 2. No. of iterations: (sizeofarray-windowsize/windowsize-overlap)+1;

Now the problem is if sizeof array is 10 and winsize = 3, and overlap = 1, and no of iterations will be 4 with full window size
x1,x2,x3,
x3,x4,x5,
x5,x6,x7,
x7,x8,x9,
x9,x10

so we left x9 and x10. so we have to detect which no of elements were left and have to use them directly, (in my cases these x9 and x10 are file pointers).

Hope for a answer on the same.

Thanks in Advance.
rakesh
ranjanrakesh
 
Posts: 14
Joined: Wed Mar 12, 2008 10:10 am
Location: Delhi, India

Postby ranjanrakesh » Thu Mar 20, 2008 7:42 am

Typo error in above problem statement
x1,x2,x3
x2,x3,x4,
x3,x4,x5,
x4,x5,x6,
x5,x6,x7,
x6,x7,x8,
x7,x8,x9,
x8.x9,x10
x9,x10,x11
ranjanrakesh
 
Posts: 14
Joined: Wed Mar 12, 2008 10:10 am
Location: Delhi, India

Postby Alvaro » Thu Mar 20, 2008 8:06 am

What have you tried?
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby ranjanrakesh » Thu Mar 20, 2008 8:56 am

Hi ,
i think i have found solution, it was easy.

while(n >= window)
{
n = n - window;
n = n + overlap;
counter++;
}
n = n - overlap;
Unutilizedindex = sizeofarray - n;
ranjanrakesh
 
Posts: 14
Joined: Wed Mar 12, 2008 10:10 am
Location: Delhi, India

Postby Alvaro » Thu Mar 20, 2008 9:19 am

I don't think that works... Can you try to turn it into a complete program and post it?
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby ranjanrakesh » Thu Mar 20, 2008 9:34 am

int main(){

int n,n2; n = n2 = 26;
int overlap = 12;
int window = 13;

int counter=0;

while(n >= window)
{
n = n - (window);
n = n + overlap;
counter++;
}
n = n - overlap;
int index = n2 - n;



int S = (n2-window)/ (window-overlap) + 1;

printf("counter %d", counter);

}
//counter will be total number of iteration
//index will be the index number which are left (not fitting in the window size trailing indices
ranjanrakesh
 
Posts: 14
Joined: Wed Mar 12, 2008 10:10 am
Location: Delhi, India

Postby Alvaro » Thu Mar 20, 2008 9:51 am

If all you want to get is the count, try this:
[syntax="c"]#include <stdio.h>

int counter(int n, int overlap, int window){
return (n-overlap)/(window-overlap);
}

int main(){
printf("counter %d\n",counter(26,12,13));
return 0;
}

[/syntax]
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA

Postby ranjanrakesh » Thu Mar 20, 2008 9:59 am

thanks but, i too want the numbers which are left in the iteration. suppose last iteration includes only x5,x6,x7 of a 9 length array. then i want to know the index of 8 and 9,
ranjanrakesh
 
Posts: 14
Joined: Wed Mar 12, 2008 10:10 am
Location: Delhi, India

Postby Alvaro » Thu Mar 20, 2008 11:06 am

Something like (n-overlap)%(window-overlap) is the number of elements not covered. In any case, you shouldn't need a loop to determine these numbers.
User avatar
Alvaro
Moderator
 
Posts: 5185
Joined: Mon Sep 22, 2003 4:57 pm
Location: NY, USA


Return to Algorithms & Data Structures

Who is online

Users browsing this forum: No registered users and 0 guests