Thread: Help me with the loop plz ?

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by quzah View Post
    Pay attention. There are two loops. One for the whole array, and one for segments of eight. Use those little round things located near the top of your face.


    Quzah.
    What good are the horns going to do?

  2. #17
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    So that's why you store it in your second array? My example was to show you how to touch each element in chunks of 8. To modify it, you would do:
    Code:
    for(int i=0;i<sizeof(inputBuffer);i+=8){
    	for(int j=0; j<8; j++){
    		printf("%d\n", i+j);
    		//whatever multiplication you want for
    		//and store in second array
    	}
    	//whatever you want to do with the 8 elements
    	//This section represents each chunk of 8
    }
    Last edited by AndrewHunter; 08-11-2011 at 08:47 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  3. #18
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    yes quzah, i actually have three for loops, (1) entire array (2) chunks of 8. (3) when it hits 7. thats not the problem. i dont think anyone gets the problem. you think i just need to go through the loop and convert all 0s and 1s to decimal, well thats not the case. here is my updated loop. that gets some characters right but not quite.

    Code:
    int array01[34] = {0,1,0,0,0,1,1,1,0,1,1,0,1,1,1,1,0,1,1,0,1,1,1,1,0,1,1,0,0,1,7,0,0,1};
    int word[10];
    int d=128, j=0, m=0, k=0, n=0;
    
    for (j=0;j<(i-k);j++){
    
          for (n=0;d!=1 && array01[m]!=7;n++){
          word[p]+=array01[m]*d;
          m++;
          d=d/2;} printf("printing word - %d\n",word[p]); d=128; p++; m++;
    
            if (array_01[m-1]==7){
            m++;
            for (n=0; n<8-p ;n++){
            word[p+1]+=array_01[m]*d;
            m++;
            d=d/2;} printf("printing word - %d\n",word[p]);
    }
    }
    
    }
    Last edited by vegan; 08-11-2011 at 08:31 PM.

  4. #19
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So have you traced your logic when array is 7? You increment m, and then keep on reading from array01. If you don't want to do that, then don't do that. You don't have to do anything if you want the rest of the numbers to be 0, because adding 0 doesn't change anything.
    Last edited by tabstop; 08-11-2011 at 08:32 PM. Reason: names

  5. #20
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    7 is just a number it doesnt mean chunks of 8. 7 could be 3 or 4 as long as it not 0 or 1. yep, the code exits the loop because the rest of numbers supposed to be 0 so im not adding them cos multiplication would be 0 anyway.
    Last edited by vegan; 08-11-2011 at 08:35 PM.

  6. #21
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    We don't know what you are talking about because it is hard, at least for me, to follow the logic in your code. If you want to convert each binary number individually based on byte order, then convert the resultant 8 bit sections to a word look at the logic in the loop I posted above. You could fill in the areas I commented with your conversions and it will work.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #22
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    thanks andrew and thank you all for trying))

  8. #23
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    suppose it is a long array and there are more than 7s. so say 01001 7 1010 7 01100 (i use spaces for clarity) supposed to be the same as 01001[000]1010[0000]01100[000] i.e. every time it hits 7 it should start multiplying by 128 not continuing where it stopped before 7.

  9. #24
    Registered User
    Join Date
    Jul 2011
    Posts
    29
    off to bed.. thanks again you all)

  10. #25
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by vegan View Post
    yes quzah, i actually have three for loops, (1) entire array (2) chunks of 8. (3) when it hits 7.
    It's usually a bad idea to just keep throwing more code at a problem -- especially since you don't seem to even understand your problem. I've got an idea, how about simply printing your array in groups of X?
    Code:
    for( x = 0; x < size of array; x += block size )
        for( y = 0; y < block size; y++ )
            do something with array[ x + y ]
    Once you have something simple working, then try to do more. Don't just keep slapping more loops on hoping it happens to work right. That hardly ever works.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #26
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by vegan View Post
    say i have an array of integers:
    Code:
    int array01[34] = {0,1,0,0,0,1,1,1,0,1,1,0,1,1,1,1,0,1,1,0,1,1,1,1,0,1,1,0,0,1,7,0,0,1};
    int word[10];
    int d=128, j=0, m=0, k=0, n=0;
    
    for (j=0;j<(i-k);j++){
    
    //j is just a counter
          for (n=0;d!=1 && array01[m]!=7;n++){
          word[p]+=array01[m]*d;
          m++;
          d=d/2;}
    
    printf("printing word - %d\n",word[p]); d=128; p++; m++;
    
    if (array01[m1]==7){
    
    }
    
    }
    The variable i is neither declared nor intialized... your loop is going to fail... in fact I'm surprised it compiles.

  12. #27
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Quote Originally Posted by CommonTater View Post
    The variable i is neither declared nor intialized... your loop is going to fail... in fact I'm surprised it compiles.
    Well spotted.
    Code:
    while(!asleep) {
       sheep++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Nested while loop inside for loop
    By Sonny in forum C Programming
    Replies: 71
    Last Post: 07-31-2011, 08:38 PM
  2. change var inside loop and run loop again
    By d387420489 in forum C Programming
    Replies: 5
    Last Post: 07-29-2011, 01:19 AM
  3. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  4. The Infinit loop that doesn't loop.
    By errigour in forum C Programming
    Replies: 1
    Last Post: 11-09-2010, 11:31 AM
  5. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM