Click here to Skip to main content
15,887,776 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have an array suppose "ar" which has a number of elements.I want to travers or process the array elements according to a integer variable suppose "a" which can have 0 or 1 or 2 or 3 or 4 or or 5 or 6 and so on.....

Suppose ar has elements 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31...

What i want when a=0 then the loop process the element 1 of ar
If a=1 thn process the elements 2,3 of ar
If a=2 thn process the elements 4,5,6,7 of ar
if a=3 thn process the elements 8,9,10,11,12,13,14,15 of ar
If a=4 thn process the elements 16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 of ar
and so on......

Please if any one can do this i m thankful to him

thanks a lot in advance.........

Please everyone code project is a community with the greatest minds i need to solve this problem urgently please help..........
Posted
Updated 23-Oct-12 19:23pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Oct-12 1:00am    
Not a question. What did you try? What's the problem?
--SA
VINAY SOROUT 24-Oct-12 1:04am    
Its a question sir. I am doing a multi level marketing project where i need this implementation.......
Sergey Alexandrovich Kryukov 24-Oct-12 10:25am    
Even this is not a question. Do I need to explain what's a question? Just do your work by yourself, show your code, and explain where it works not as you expected, to get help. Right now, you want someone to do this boring work for you, without explanation of the purpose. For your evil marketing (yes, this is evil)? Then what this sequences do to your work?
OK, nobody will do you work for you anyway, I guess. What technical aspects you don't understand? We can help you.
--SA
VINAY SOROUT 24-Oct-12 13:36pm    
furrrrrrrr f*** u i did it by my own i dont need people like you to help me.....
Sergey Alexandrovich Kryukov 24-Oct-12 15:12pm    
Like me..? Now, that nice of you.
I can imagine how your anger improves you reputation.
--SA

Seems to me like in your simple for next you need to multiply a count variable by 2.
You have to declare the count variable outside the loop and initialise it to 1.
Inside the loop you have to use the count variable to do you processing of the elements in the array starting from the count and for the next count elements. So you'll need a second loop within the first loop.
 
Share this answer
 
C#
var x = Convert.ToInt32(Math.Pow(2, a));
var result = new int[x];
for (var i = 0; i < x; i++)
    result[i] = x + i;
 
Share this answer
 
This Code may help you
C#
for (int i = 0; i < arrayName.Length; i++)
        {
            for (int j = Convert.ToInt32(Math.Pow(2, i)); j < arrayName.Length; j++)
            {
                //write your Code here.
            }
        }


Philip Stuyck gave very good Idea.
 
Share this answer
 
First of all you must have a function to retrieve power of 2
public int powerto(int b)
   {
           int res=1;
           for (int i = 1; i <= b; i++)
           {

                   res = res * 2;

           }
           return res;
   }

get your array filled with required numbers from user.
(you can use dynamic array too.)


then use the code below to retrieve your series of no.

C#
a = Convert.ToInt16(Console.ReadLine());
          int from = o.powerto(a)-1;
          int to = from+from ;
          for (int p = from; p <= to; p++)
          {
              Console.WriteLine(ar[p]);
          }
            

          Console.ReadLine();
 
Share this answer
 
You should see a pattern in the requirements, namely a value selects both the start of the to-be-processed sequence and its length:
start = 2^a -1
length = 2^a
 
Share this answer
 
v2
At last i did it correct solution of this is:
if a=0 do coding remeber this if a=0 we just simply process the first element of array

if a>0 than

int t=0;
for(int t1=1;t1<=a;t1++)
{
t=t+math.pow(2,t1-1);
}

now to get the elements of array

for(int f=t;f<=t*2;f++)
{
process ar[f];
}

its working properly thanks for all of your supports....
 
Share this answer
 
Comments
CPallini 24-Oct-12 4:50am    
You don't need the first loop, since: 2^0 + 2^1 + .. + 2^n = 2^(n+1) - 1
VINAY SOROUT 24-Oct-12 5:01am    
same thing doesnt matter thanks a lot

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900