Like Tree1Likes
  • 1 Post By Adak

Missing bits

This is a discussion on Missing bits within the C Programming forums, part of the General Programming Boards category; I have this block of code which is meant to output the lines of numbers generated and which should do ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    18

    Missing bits

    I have this block of code which is meant to output the lines of numbers generated and which should do so in a patter whereby the first line has 300 1s+0s and each subsequent line has ten fewer characters until the final one which has only 10. For some reason when I run it however I get some lines shorter than they should be. Specifically the fourth line generated by this is only 205 characters long instead of the 260 characters it should be. Also line 12 is only 55 characters long. Where is the problem in the code?

    Code:
    		for(bi=1; bi<31; bi++) { 			b[bi].trait[0]=b[31].trait[bi*10];
    			b[bi].counter[0]=b[31].counter[bi*10];
    			for(i=1; i<(310-(bi*10)); i++) { 
    				if(ri>1689) for(i=0; i<2000; i++) { 
    					ran[i]=rand();
    					ri=0;
    				}
    				if(b[bi].trait[i-1]) { 
    					b[bi].trait[i]=gen();
    					if(!b[bi].trait[i]) b[bi].counter[i+1]++;
    				}
    				else { 
    					b[bi].trait[i]=rev(b[bi].counter[i], ii);
    					if(!b[bi].trait[i]) b[bi].counter[i+1]=b[bi].counter[i]+1;
    				}
    				fprintf(fp, "%d", b[bi].trait[i]); 
    			}
    			fprintf(fp, "\n");
    			matrix[bi][ii]=b[bi].trait[309-(bi*10)]; 
    		}

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    Perhaps you should post ALL the declarations of those arrays you're using.

    It sounds like a pretty fair bet that you're running off the end of at least one of them.

    Perhaps add some code to verify your subscripts (or at least the apparent ranges).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You're incrementing i in this outer for loop, and then incrementing i again, in the inner loop.

    That's usually an error in logic.

    Code:
    for(i=1; i<(310-(bi*10)); i++) {
            if(ri>1689) for(i=0; i<2000; i++) {
                ran[i]=rand();
                ri=0;
            }
    Salem likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting certain bits from sequence of bits
    By lucaspewkas in forum C Programming
    Replies: 5
    Last Post: 10-06-2007, 12:22 AM
  2. bits in C
    By ch4 in forum C Programming
    Replies: 12
    Last Post: 07-10-2007, 11:43 AM
  3. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM
  4. copy some bits into a 8 bits binary number
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 05-29-2002, 10:54 AM
  5. the stl: bits
    By ygfperson in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21