Suspicious Pointer Conversion

This is a discussion on Suspicious Pointer Conversion within the C Programming forums, part of the General Programming Boards category; Dude, i dont know what your doing wrong then cause it runs fine on both my computers, here at work ...

  1. #31
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Dude, i dont know what your doing wrong then cause it runs fine on both my computers, here at work and at home. this is my output(top is cut off because my dos does not scroll - win98). I ran a 2x3 * 3x2 matrice and entered all 2's.
    Code:
    [ 2 * 2 += 68 ]
    [ 2 * 2 += 72 ]
    [ 2 * 2 += 76 ]
    [ 2 * 2 += 80 ]
    [ 2 * 2 += 84 ]
    [ 2 * 2 += 88 ]
    [ 2 * 2 += 92 ]
    [ 2 * 2 += 96 ]
    [ 2 * 2 += 100 ]
    [ 2 * 2 += 104 ]
    [ 2 * 2 += 108 ]
    [ 2 * 2 += 112 ]
    [ 2 * 2 += 116 ]
    [ 2 * 2 += 120 ]
    [ 2 * 2 += 124 ]
    [ 2 * 2 += 128 ]
    [ 2 * 2 += 132 ]
    [ 2 * 2 += 136 ]
    [ 2 * 2 += 140 ]
    [ 2 * 2 += 144 ]
    
    
    [36][36]
    [36][36]
    Here is a 2x3 * 2x3 matrice and its output, again the top is cut off.
    Code:
    [ 2 * 2 += 68 ]
    [ 2 * 2 += 72 ]
    [ 2 * 2 += 76 ]
    [ 2 * 2 += 80 ]
    [ 2 * 2 += 84 ]
    [ 2 * 2 += 88 ]
    [ 2 * 2 += 92 ]
    [ 2 * 2 += 96 ]
    [ 2 * 2 += 100 ]
    [ 2 * 2 += 104 ]
    [ 2 * 2 += 108 ]
    [ 2 * 2 += 112 ]
    [ 2 * 2 += 116 ]
    [ 2 * 2 += 120 ]
    [ 2 * 2 += 124 ]
    [ 2 * 2 += 128 ]
    [ 2 * 2 += 132 ]
    [ 2 * 2 += 136 ]
    [ 2 * 2 += 140 ]
    [ 2 * 2 += 144 ]
    
    
    [24][24][24]
    [24][24][24]
    Something is wrong on your end, lol, not the code.
    The keyboard is the standard device used to cause computer errors!

  2. #32
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    any idea what could be wrong?im running borland turbo C++ 2.01

  3. #33
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Not really, try a different compiler, go to www.cnet.com and search there for their compilers they are very basic, but work. Here at work, im using Digital Mars. It has no UI, but works fine.
    The keyboard is the standard device used to cause computer errors!

  4. #34
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Originally posted by mr_spanky202
    Thanks a lot, copied the code into the compiler and gave it a test ran. Still throws up wrong answers once C and R2 are bigger than one
    eg
    Code:
    7 0 9            4    13             1522   1216 
    77 2 63   *  64    17       =    13774  10792
    24 91 6       29    46              11737   9196
    When should be
    Code:
    289 505 
    2263  3933
    6094   2135
    I see the same results. How about something like this?
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    double *multiply(const double *a, int arows, int acols,
                     const double *b, int brows, int bcols,
                     int *crows, int *ccols)
    {
        double *c;
        if ( acols != brows )
        {
            return NULL;
        }
        *crows = arows;
        *ccols = bcols;
        c = malloc(*crows * *ccols * sizeof(*c));
        if ( c != NULL )
        {
            int i, j, k;
            for ( i = 0; i < *crows; ++i )
            {
                for ( j = 0; j < *ccols; ++j )
                {
                    int ij = *ccols * i + j;
                    c[ij] = 0;
                    for ( k = 0; k < acols; ++k )
                    {
                        int ik = acols * i + k;
                        int kj = bcols * k + j;
                        double product = a[ik] * b[kj];
                        c[ij] += product;
                    }
                }
            }
        }
        return c;
    }
    
    void display(const double *matrix, int rows, int cols)
    {
        int i,j;
        for ( i = 0; i < rows; ++i )
        {
            for ( j = 0; j < cols; ++j )
            {
                printf("%7g ", matrix[i * cols + j]);
            }
            putchar('\n');
        }
        putchar('\n');
    }
    
    int main(void)
    {
        int i,j;
        double a[] = /* 3x3 */
        {
             7.0,  0.0,  9.0,
            77.0,  2.0, 63.0,
            24.0, 91.0,  6.0,
        };
        double b[] = /* 3x2 */
        {
             4.0, 13.0,
            64.0, 17.0,
            29.0, 46.0,
        };
        double *c = multiply(a,3,3, b,3,2, &i, &j);
        display(a,3,3);
        display(b,3,2);
        if ( c != NULL )
        {
            display(c,i,j);
            free(c);
        }
        return 0;
    }
    
    /* my output
          7       0       9
         77       2      63
         24      91       6
    
          4      13
         64      17
         29      46
    
        289     505
       2263    3933
       6094    2135
    */
    The following links may be of interest.By the way, for the all-2's 2x3 * 3x2 I get this.
    Code:
    +-       -+   +-    -+   +-                                    -+   +-      -+
    | 2  2  2 |   | 2  2 |   | (2*2)+(2*2)+(2*2)  (2*2)+(2*2)+(2*2) |   | 12  12 |
    | 2  2  2 | * | 2  2 | = | (2*2)+(2*2)+(2*2)  (2*2)+(2*2)+(2*2) | = | 12  12 |
    +-       -+   | 2  2 |   +-                                    -+   +-      -+
                  +-    -+
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #35
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Ops, didn't see the logic error. Guess what, i fixedit with 1 little thing, that is highlighted in red
    Code:
    #include <stdio.h>
    
    #define MAX_R 4
    #define MAX_C 4
    
    typedef int coord[2];
    
    int OrdEnt (int, int*, int*);
    void MatCalc (int [][MAX_R], int [][MAX_C], int[][MAX_C], int, int, int, int);
    
    int OrdEnt (int a, int *r, int *c)
    {
    	do {
    		printf("Enter the order of Matrix %d in the format NxN:\n",a);
    		
    		scanf ("%dx%d",r,c);
    		if (*r>4 || *c>4)
    		{
    			printf("Order too great\n");
    		}
    		
    	} while  (*r>4 || *c>4);
    	return (0);
    }
    
    void MatCalc (int Matrix_1[][MAX_R], int Matrix_2[][MAX_C], int  Matrix_3[][MAX_C], 
    						int r, int c, int r2, int c2)
    {
    	
    	int j,l,h, m = 0, sum = 0;
    	for (j=0; j<r; j++){
    		for (l=0; l<c; l++){
    			for (m=0; m<r2; m++){
    				for (h=0; h<c2; h++){
    					sum += Matrix_1[j][l] * Matrix_2[m][h];
    					printf("[ %d * %d += %d ]", Matrix_1[j][l], Matrix_2[m][h], sum);
    					Matrix_3[j][h] += Matrix_1[j][l] * Matrix_2[m][h];	
    				};
    				printf("\n");
    			};
    		};
    	};
    	
    	printf("\n\n");
    	for (j = 0; j<r; j++){
    		for (l=0; l<c2; l++){
    			printf("[%d]", (Matrix_3[j][l])/r2) ;
    		};
    		printf("\n");
    	};
    }
    
    
    
    int main(void)
    
    {
    	int R = 0,R2 = 0, C = 0, C2 = 0;
    	int Matrix_1 [MAX_R] [MAX_C];
    	int Matrix_2 [MAX_R] [MAX_C];
    	int Matrix_3 [MAX_R] [MAX_C] = {0};
    	int a = 1, b = 2, c = 3, r_i, c_i;
    	char in_c;
    	
    	
    	printf("This program will multiply 2 matrices (up to the order of 4x4),"
    		" and display the result\n");
    	
    	
    	do{
    		OrdEnt(a,&R,&C);
    		printf("Is this correct,y/n?:");
    		scanf(" %c",&in_c);
    	} while(in_c == 'n' || in_c == 'N');
    	
    	
    	do{
    		OrdEnt(b,&R2,&C2);
    		printf("Is this correct,y/n?:");
    		scanf(" %c",&in_c);
    	} while(in_c == 'n' || in_c == 'N');
    	
    	
    	
    	for(r_i=0; r_i<R; r_i++){
    		printf("\nfor row %d of Matrix_1\n",r_i +1);
    		for(c_i=0; c_i<C; c_i++){
    			printf("\tEnter element value for column %d:",c_i +1);
    			scanf("%d",&Matrix_1 [r_i] [c_i]);
    		}
    				}
    	printf("\n\n");
    	for (r_i=0; r_i<R; r_i++){
    		for (c_i=0; c_i<C; c_i++){
    			printf("[%d]", Matrix_1[r_i][c_i]);
    			printf("\n");
    		}
    	}
    	
    	
    	
    	for(r_i=0; r_i<R2; r_i++){
    		printf("\nfor row %d of Matrix_2\n",r_i +1);
    		for(c_i=0; c_i<C2; c_i++){
    			printf("\tEnter element value for column %d:",c_i +1);
    			scanf("%d",&Matrix_2 [r_i] [c_i]);
    		}
    				}
    	
    	printf("\n\n");
    	for (r_i=0; r_i<R2; r_i++){
    		for (c_i=0; c_i<C2; c_i++){
    			printf("[%d]", Matrix_2[r_i][c_i]);
    			printf("\n");
    		}
    	}
    	
    	
    	MatCalc(Matrix_1,Matrix_2,Matrix_3, R, C, R2, C2);
    	
    	
    	
    	
    	
    	return (0);
    	
    }
    The keyboard is the standard device used to cause computer errors!

  6. #36
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >Ops, didn't see the logic error. Guess what, i fixedit with 1 little thing
    You don't test the code too much, do you?
    Code:
    This program will multiply 2 matrices (up to the order of 4x4), and display the
    result
    Enter the order of Matrix 1 in the format NxN:
    3x3
    Is this correct,y/n?:y
    Enter the order of Matrix 2 in the format NxN:
    3x2
    Is this correct,y/n?:y
    
    for row 1 of Matrix_1
            Enter element value for column 1:7
            Enter element value for column 2:0
            Enter element value for column 3:9
    
    for row 2 of Matrix_1
            Enter element value for column 1:77
            Enter element value for column 2:2
            Enter element value for column 3:63
    
    for row 3 of Matrix_1
            Enter element value for column 1:24
            Enter element value for column 2:91
            Enter element value for column 3:6
    
    
    [7]
    [0]
    [9]
    [77]
    [2]
    [63]
    [24]
    [91]
    [6]
    
    for row 1 of Matrix_2
            Enter element value for column 1:4
            Enter element value for column 2:13
    
    for row 2 of Matrix_2
            Enter element value for column 1:64
            Enter element value for column 2:17
    
    for row 3 of Matrix_2
            Enter element value for column 1:29
            Enter element value for column 2:46
    
    
    [4]
    [13]
    [64]
    [17]
    [29]
    [46]
    [ 7 * 4 += 28 ][ 7 * 13 += 119 ]
    [ 7 * 64 += 567 ][ 7 * 17 += 686 ]
    [ 7 * 29 += 889 ][ 7 * 46 += 1211 ]
    [ 0 * 4 += 1211 ][ 0 * 13 += 1211 ]
    [ 0 * 64 += 1211 ][ 0 * 17 += 1211 ]
    [ 0 * 29 += 1211 ][ 0 * 46 += 1211 ]
    [ 9 * 4 += 1247 ][ 9 * 13 += 1364 ]
    [ 9 * 64 += 1940 ][ 9 * 17 += 2093 ]
    [ 9 * 29 += 2354 ][ 9 * 46 += 2768 ]
    [ 77 * 4 += 3076 ][ 77 * 13 += 4077 ]
    [ 77 * 64 += 9005 ][ 77 * 17 += 10314 ]
    [ 77 * 29 += 12547 ][ 77 * 46 += 16089 ]
    [ 2 * 4 += 16097 ][ 2 * 13 += 16123 ]
    [ 2 * 64 += 16251 ][ 2 * 17 += 16285 ]
    [ 2 * 29 += 16343 ][ 2 * 46 += 16435 ]
    [ 63 * 4 += 16687 ][ 63 * 13 += 17506 ]
    [ 63 * 64 += 21538 ][ 63 * 17 += 22609 ]
    [ 63 * 29 += 24436 ][ 63 * 46 += 27334 ]
    [ 24 * 4 += 27430 ][ 24 * 13 += 27742 ]
    [ 24 * 64 += 29278 ][ 24 * 17 += 29686 ]
    [ 24 * 29 += 30382 ][ 24 * 46 += 31486 ]
    [ 91 * 4 += 31850 ][ 91 * 13 += 33033 ]
    [ 91 * 64 += 38857 ][ 91 * 17 += 40404 ]
    [ 91 * 29 += 43043 ][ 91 * 46 += 47229 ]
    [ 6 * 4 += 47253 ][ 6 * 13 += 47331 ]
    [ 6 * 64 += 47715 ][ 6 * 17 += 47817 ]
    [ 6 * 29 += 47991 ][ 6 * 46 += 48267 ]
    
    
    [517][405]
    [4591][3597]
    [3912][3065]
    It seems we'd like to get this.
    Code:
    [289][505]
    [2263][3933]
    [6094][2135]
    You might want to take a look at these elements.
    Code:
    void MatCalc (int Matrix_1[][MAX_R], int Matrix_2[][MAX_C], int  Matrix_3[][MAX_C],
                  int r, int c, int r2, int c2)
    {
    
        int j,l,h, m = 0, sum = 0;
        for ( j=0; j<r; j++ )
        {
            for ( l=0; l<c; l++ )
            {
                for ( m=0; m<r2; m++ )
                {
                    for ( h=0; h<c2; h++ )
                    {
                        sum += Matrix_1[j][l] * Matrix_2[m][h];
                        printf("[ %d * %d += %d ]", Matrix_1[j][l], Matrix_2[m][h], sum);
                        Matrix_3[j][h] += Matrix_1[j][l] * Matrix_2[m][h];
                    };
                    printf("\n");
                };
            };
        };
    
        printf("\n\n");
        for ( j = 0; j<r; j++ )
        {
            for ( l=0; l<c2; l++ )
            {
                printf("[%d]", (Matrix_3[j][l])/r2) ;
            };
            printf("\n");
        };
    }
    Maybe try this.
    Code:
    void MatCalc (int Matrix_1[][MAX_C], int Matrix_2[][MAX_C], int  Matrix_3[][MAX_C],
                  int r, int c, int r2, int c2)
    {
    
        int j,l,m;
        for ( j=0; j<r; j++ )
        {
            for ( l=0; l<c2; l++ )
            {
                for ( m=0; m<r2; m++ )
                {
                    Matrix_3[j][l] += Matrix_1[j][m] * Matrix_2[m][l];
                    printf("[ %d * %d += %d ]\n", Matrix_1[j][m], Matrix_2[m][l],
                           Matrix_3[j][l]);
                }
            }
        }
    
        printf("\n\n");
        for ( j = 0; j<r; j++ )
        {
            for ( l=0; l<c2; l++ )
            {
                printf("[%d]", (Matrix_3[j][l])) ;
            };
            printf("\n");
        }
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  2. Pointer array's and ASCII to bit conversion
    By AdamLAN in forum C++ Programming
    Replies: 12
    Last Post: 05-06-2005, 05:55 PM
  3. Do I have a scanf problem?
    By AQWst in forum C Programming
    Replies: 2
    Last Post: 11-26-2004, 05:18 PM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Replies: 2
    Last Post: 02-07-2002, 08:39 AM

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