Thread: I don't know where the error is.

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    44

    I don't know where the error is.

    I've been looking at this for 2 hours now and I cannot find the error.
    Code:
    //TEST CASES
    
        printf("Entered question 2.\n\n");
        char xa[]={0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
        char ya[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0};
        p=xa;
        q=ya;
        thirtytwomultiply(p,q);
    
        printf("\nTest Case 2\n");
        char xb[]={0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
        char yb[]={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        p=xb;
        q=yb;
        thirtytwomultiply(p,q);
    
        char xc[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
        char yc[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
        p=xc;
        q=yc;
        thirtytwomultiply(p,q);
    
        char xd[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1};
        char yd[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0};
        p=xd;
        q=yd;
        thirtytwomultiply(p,q);
    
        char xe[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1};
        char ye[]={1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        p=xe;
        q=ye;
        thirtytwomultiply(p,q);
    
    //END TEST CASES
    
    //FUNCTION
    void thirtytwomultiply(char *x, char *y) {
    	char s[64]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        char t[64];
        int i;
    	char *p, *q, *r, *z;
    	p=x;
    	q=y;
    	z=s;
        printf("  ");
        for (i=0; i<=31; i++) {
    		printf(" ");
    	}
    	for (i=0; i<=31; i++) {
    		printf("%d", *p);
    		p++;
    	}
    	printf("\n");
    	printf("* ");
        for (i=0; i<=31; i++) {
    		printf(" ");
    	}
    	for(i=0; i<=31; i++) {
    		printf("%d", *q);
    		q++;
    	}
    	printf("\n");
        for (i=0;i<=65;i++){
     		printf("-");
     	}
     	printf("\n");
    	p=x;
    	q=y;
    	z=s;
        int j=0;
        int k=0;
        int l=0;
        r=t;
        for (i=0; i<64; i++) {
            *r=0;
            r++;
        }
        q=q+31;
    //-------------------------------------------------------------------
        for (j=1; j<=32; j++){
            q--;    //decrements q
            //-------start making r--------
            printf("enter making r\n");
            for (i=0; i<=63; i++) {
                *r=0;
                r++;
            }
            r=r-64;
            for (i=32-k; i>0; i--) {
                *r=0;
                r++;
            }
            for (i=0; i<32; i++) {
                *r=*p;
                r++;
                p++;
            }
            for (i=k; i>0; i--) {
                *r=0;
                r++;
            }
            //--------stop making r--------
            p=p-32;  //reset p and r to base
            r=r-64;
            k++;  //shift left
    //----------------start addition----------------------
            printf("enter addition\n");
            if (*q==1) {
                printf("Q=1\n");
                //add to z
                for (l=64; l>=1; l--) {
                *z=*r+*z;
                    if (*z==2) {
                        *z=0;
                        r--;
                        *r=*r+1;
                        r++;
                    }
                    else if (*z==3) {
                        *z=1;
                        r--;
                        *r=*r+1;
                        r++;
                    }
                    z++;
                    r++;
                }
            }
            else {
             *z=*z;
            }
            //--------end addition-------
            printf("\n\n");
            printf("reenter loop\n");
        }
    
        z=z-64;
        r=r-64;
        printf("  ");
        for (i=0;i<64;i++) {
            printf("%d",*z);
            if(i<63) {
                z++;
            }
        }
        printf("\npreparing to exit method\n");
    }
    //END FUNCTION
    Now I get to the print statement "preparing to exit method". However, the statement "Test Case 2" doesn't appear. I am using Code::Blocks on Windows XP 32. I'm at the last bit of the function, but it doesn't want to exit. When it gets there, my Visual Studio debugger pops up and the program stops. I could really use a hand. Thank you.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Do you have a short compilable program that shows the problem?

    Are p and q char pointers in the TEST CASES section of code? Why isn't TEST CASES in a function?

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    The test cases are in the main method calling separate methods.

    Code:
    #include <stdio.h>
    #include <Math.h>
    
    void *signedtwobit(char *x, char *y);
    void thirtytwomultiply(char *x, char *y);
    
    int main(void) {
    //-------------------problem1---------------------
    
        printf("Entered question 1.\n\n");
    	char a[]={0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    	char b[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
    
    	char *p, *q;
    	p=a;
    	q=b;
    	signedtwobit(p,q);
    
    	char c[]={0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    	char d[]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    	p=c;
    	q=d;
    	signedtwobit(p,q);
    
    	char e[]={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    	char f[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1};
    	p=e;
    	q=f;
    	signedtwobit(p,q);
    
    	char g[]={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    	char h[]={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    	p=g;
    	q=h;
    	signedtwobit(p,q);
    
    	char i[]={0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
    	char j[]={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
    	p=i;
    	q=j;
    	signedtwobit(p,q);
    
    
    //--------------------------------------------------
    
    
    //-----Question 2-----------------------------------
        printf("Entered question 2.\n\n");
        char xa[]={0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
        char ya[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0};
        p=xa;
        q=ya;
        thirtytwomultiply(p,q);
    
        printf("\nTest Case 2\n");
        char xb[]={0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
        char yb[]={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        p=xb;
        q=yb;
        thirtytwomultiply(p,q);
    
        char xc[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
        char yc[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
        p=xc;
        q=yc;
        thirtytwomultiply(p,q);
    
        char xd[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1};
        char yd[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0};
        p=xd;
        q=yd;
        thirtytwomultiply(p,q);
    
        char xe[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1};
        char ye[]={1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        p=xe;
        q=ye;
        thirtytwomultiply(p,q);
    
    //---------------------------------------------------
    
    //--------------Question 3---------------------------
    
    //---------------------------------------------------
    
    
    //--------------Question 4---------------------------
        double a;
        a=2.71828;
    
        a=1.61803;
    
        a=-1.41421;
    
        a=-1.73205;
    
        a=-2.23606;
    //---------------------------------------------------
    
    }
    
    
    
    
    void *signedtwobit(char *x, char *y) {
    	char s[33];
    	int i;
    	char *p, *q, *z;
    
    	p=x;
    	q=y;
    
    	printf("  ");
    	for (i=0; i<=31; i++) {
    		printf("%d ", *p);
    		p++;
    	}
    	printf("\n");
    
    	printf("+ ");
    	for(i=0; i<=31; i++) {
    		printf("%d ", *q);
    		q++;
    	}
    	printf("\n");
    
    	p--;
    	q--;
    //----start adding----
    	for (i=32; i>=1; --i) {
    		s[0]=0;
    		s[i]=*p+*q;
    		if (s[i]==2) {
    			if (i==1) {
    				s[0]=1;
    				s[1]=0;
    			}
    			else {
    				s[i]=0;
    				p--;
    				*p=*p+1;
    				p++;
    
    			}
    		}
    		else if (s[i]==3) {
    			if (i==1) {
    				s[0]=1;
    				s[1]=1;
    			}
    			else {
    				s[i]=1;
    				p--;
    				*p=*p+1;
    				p++;
    
    			}
    		}
    		p=p-1;
    		q=q-1;
    	}
    //-----end adding------
    
     	z=s;
     	for (i=0;i<=65;i++){
     		printf("-");
     	}
     	printf("\n");
    
    	for (i=0; i<=32; i++) {
    		printf("%d ", *z);
    		z++;
    	}
    	printf("\n\n\n\n");
    }
    
    
    
    
    void thirtytwomultiply(char *x, char *y) {
    	char s[64]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
        char t[64];
        int i;
    	char *p, *q, *r, *z;
    	p=x;
    	q=y;
    	z=s;
        printf("  ");
        for (i=0; i<=31; i++) {
    		printf(" ");
    	}
    	for (i=0; i<=31; i++) {
    		printf("%d", *p);
    		p++;
    	}
    	printf("\n");
    	printf("* ");
        for (i=0; i<=31; i++) {
    		printf(" ");
    	}
    	for(i=0; i<=31; i++) {
    		printf("%d", *q);
    		q++;
    	}
    	printf("\n");
        for (i=0;i<=65;i++){
     		printf("-");
     	}
     	printf("\n");
    	p=x;
    	q=y;
    	z=s;
        int j=0;
        int k=0;
        int l=0;
        r=t;
        for (i=0; i<64; i++) {
            *r=0;
            r++;
        }
        q=q+31;
    //-------------------------------------------------------------------
        for (j=1; j<=32; j++){
            q--;    //decrements q
            //-------start making r--------
            printf("enter making r\n");
            for (i=0; i<=63; i++) {
                *r=0;
                r++;
            }
            r=r-64;
            for (i=32-k; i>0; i--) {
                *r=0;
                r++;
            }
            for (i=0; i<32; i++) {
                *r=*p;
                r++;
                p++;
            }
            for (i=k; i>0; i--) {
                *r=0;
                r++;
            }
            //--------stop making r--------
            p=p-32;  //reset p and r to base
            r=r-64;
            k++;  //shift left
    //----------------start addition----------------------
            printf("enter addition\n");
            if (*q==1) {
                printf("Q=1\n");
                //add to z
                for (l=64; l>=1; l--) {
                *z=*r+*z;
                    if (*z==2) {
                        *z=0;
                        r--;
                        *r=*r+1;
                        r++;
                    }
                    else if (*z==3) {
                        *z=1;
                        r--;
                        *r=*r+1;
                        r++;
                    }
                    z++;
                    r++;
                }
            }
            else {
             *z=*z;
            }
            //--------end addition-------
            printf("\n\n");
            printf("reenter loop\n");
        }
    
        z=z-64;
        r=r-64;
        printf("  ");
        for (i=0;i<64;i++) {
            printf("%d",*z);
            if(i<63) {
                z++;
            }
        }
        printf("\npreparing to exit method\n");
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Let me pop it into the "toaster".

    Ok:

    1) the double a, has the same name as the array a[]

    2) the function signedtwobit() should return a void * (pointer). Typo maybe?

    I named the number a, as "numa", what say you about the void * return?
    Last edited by Adak; 10-12-2009 at 03:39 AM.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    I switched the a, and the *. However that's not where my issues lie.

    The only test cases that should matter are the one's commented as Test Case 2, the rest can be commented out. My problem lies when I'm trying to exit the method thirtytwomultiply. It'll go through the first test case, in Test Case 2, and print out my desired result. However, it won't exit the method.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just for fun, I removed the return type, just making it "void", and ran it.

    The screen now rolls out lots of lines like:

    "enter making r" and such.

    Good, bad, ugly, or indifferent? Describe the beast.

    Just saw your new post. So I'm still looking for "Test Case 2"?

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    I'm sorry it read //---------------------Question 2-----------------------

    so far, i'm indifferent. I got the same results, but it broke after the first entrance to the method. I need five of them.

    I'm sorry if I don't make sense, I'm really tired.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Here's one error:

    In function thirtytwomultiply()

    Code:
    //-------------------------------------------------------------------
        for (j=1; j<=32; j++){
            q--;    //decrements q
            //-------start making r--------
            printf("enter making r\n");
            for (i=0; i<=63; i++) {
                *r=0;
                r++;
            }
    i is going too far, and *r=0 winds up setting j equal to zero. So the for loop end condition, is never met.

    If you remove the = sign, then it promptly finishes the function, but there is another error which totally crashes the program.

    I haven't figured that one out, but I'm sure it's a memory error, where a pointer or index, is running outside it's array bounds.

    Edit:
    I'm the same. Why don't we pick it up, tomorrow, earlier?
    Last edited by Adak; 10-12-2009 at 04:04 AM.

  9. #9
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    That's the error I can't find out either...

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In thirtytwomultiply(), the function does ok it appears, until it gets to this point (about line 230, for the *SECOND time. Then r goes out of bounds.

    Code:
        for (j=1; j<=32; j++){
            q--;    //decrements q
            //-------start making r--------
            printf("enter making r\n");
            for (i=0; i<63; i++) {
                *r=0;
                r++;
            }
    By the end of the second time, all the pointers are out of bounds. R is the first one that does it, and this is the block that starts it all.

    Maybe r or some other variable needs to be reset, before arriving here, again.

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    44
    the problem is, the function won't exit though. If I can print out the result I want I should be able to exit, shouldn't I?

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by TIMBERings View Post
    the problem is, the function won't exit though. If I can print out the result I want I should be able to exit, shouldn't I?
    I don't know what else the runaway pointers have goofed up, but it crashes the whole program upon exit from the function. We're not talking about 1 array that goes one index number too far. This is *lots* of coloring outside the lines.

    The return addresses may have been corrupted.

Popular pages Recent additions subscribe to a feed

Tags for this Thread