Suspicious Pointer Conversion

This is a discussion on Suspicious Pointer Conversion within the C Programming forums, part of the General Programming Boards category; will this work for all size matrices and how do i relate the variables created earlier in my program such ...

  1. #16
    Registered User
    Join Date
    Apr 2003
    Posts
    32

    Cool

    will this work for all size matrices and how do i relate the variables created earlier in my program such as the order size and element values of thier matrices into this function? Thank you for the help, it makes sense sorta

  2. #17
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Thats the hard part, but yes, it will work for all sizes, as long as the matrices are opposite size. Opps, didn't realize this wont work for an array[3][2] * array[5][2]. LoL, sorry, i'll write something to work on all sized matrices. BRB.
    The keyboard is the standard device used to cause computer errors!

  3. #18
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    your a star mate

  4. #19
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Here ya go, this should work, i just defined my size array, and yours will already have values, just change this code and the for loops to it counts correctly.
    Code:
    #include <stdio.h>
    
    #define SIZE1 4
    #define SIZE2 2
    #define SIZE3 2
    #define SIZE4 2
    
    int main(){
    
    	int j, l, h, m = 0, sum = 0, blah;
    	int array1[SIZE1][SIZE2] = {{1,2},{3,4},{5,6},{7,8}};
    	int array2[SIZE3][SIZE4] = {{2,4},{6,1}};
    	
    	for (j = 0; j < SIZE1; j++){
    		for(l = 0; l < SIZE2; l++){
    			for(h = 0; h < SIZE4; h++){
    				sum += array1[j][l] * array2[m][h];
    				printf("[ %d * %d += %d ]\n", array1[j][l], array2[m][h], sum);
    			};
    			m++;
    		};
    		m = 0;
    	};
    	return 0;
    }
    The keyboard is the standard device used to cause computer errors!

  5. #20
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    Sorry but doesnt give out the right values for me.
    Last edited by mr_spanky202; 04-09-2003 at 03:50 PM.

  6. #21
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    seeing how you are multiplying matrices, the size of the final matrice will be the max columns of A by the max rows of B. so this would be 4 x 2. Here is the code, and the output
    Code:
    include <stdio.h>
    
    #define SIZE1 4
    #define SIZE2 2
    #define SIZE3 2
    #define SIZE4 2
    
    int main(){
    
    	int j, l, h, m = 0, sum = 0;
    	int array1[SIZE1][SIZE2] = {{1,2},{3,4},{5,6},{7,8}};
    	int array2[SIZE3][SIZE4] = {{2,4},{6,1}};
    	int array3[SIZE1][SIZE4] = {0};
    	
    	for (j = 0; j < SIZE1; j++){
    		for(l = 0; l < SIZE2; l++){
    			for(h = 0; h < SIZE4; h++){
    				sum += array1[j][l] * array2[m][h];
    				printf("[ %d * %d += %d ]\n", array1[j][l], array2[m][h], sum);
    				array3[j][h] += array1[j][l] * array2[m][h];
    			};
    			m++;
    		};
    		m = 0;
    	};
    	printf("\n\n");
    	for (j = 0; j < SIZE1; j++){
    		for(l = 0; l < SIZE4; l++)
    			printf("[%d]", array3[j][l]);
    		printf("\n");
    		};
    
    	return 0;
    }
    
    
    /* Output
    [ 1 * 2 += 2 ]
    [ 1 * 4 += 6 ]
    [ 2 * 6 += 18 ]
    [ 2 * 1 += 20 ]
    [ 3 * 2 += 26 ]
    [ 3 * 4 += 38 ]
    [ 4 * 6 += 62 ]
    [ 4 * 1 += 66 ]
    [ 5 * 2 += 76 ]
    [ 5 * 4 += 96 ]
    [ 6 * 6 += 132 ]
    [ 6 * 1 += 138 ]
    [ 7 * 2 += 152 ]
    [ 7 * 4 += 180 ]
    [ 8 * 6 += 228 ]
    [ 8 * 1 += 236 ]
    
    
    [14][6]
    [30][16]
    [46][26]
    [62][36]
    */
    --edit--
    If you add the output matrix it will equal the last line of the output above.

    --edit 2--
    Here is a explination of how to get that final matrix, its kinda hard to picture it.
    Code:
    [ 1 * 2 += 2 ] - part of matrix 0,0
    [ 1 * 4 += 6 ] - part of matrix 0,1
    [ 3 * 6 += 24 ] - part of matrix 0,0
    [ 3 * 1 += 27 ] - part of matrix 0,1
    [ 2 * 2 += 31 ] - part of matrix 1,0
    [ 2 * 4 += 39 ] - part of matrix 1,1
    [ 1 * 6 += 45 ] - part of matrix 1,0
    [ 1 * 1 += 46 ] - part of matrix 1,1
    [ 4 * 2 += 54 ] - part of matrix 2,0
    [ 4 * 4 += 70 ] - part of matrix 2,1
    [ 7 * 6 += 112 ] - part of matrix 2,0
    [ 7 * 1 += 119 ] - part of matrix 2,1
    [ 2 * 2 += 123 ] - part of matrix 3,0
    [ 2 * 4 += 131 ] - part of matrix 3,1
    [ 9 * 6 += 185 ] - part of matrix 3,0
    [ 9 * 1 += 194 ] - part of matrix 3,1
    
    
    [20][7]
    [10][9]
    [50][23]
    [58][17]
    Last edited by stumon; 04-09-2003 at 04:32 PM.
    The keyboard is the standard device used to cause computer errors!

  7. #22
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    int array1[SIZE1][SIZE2] = {{1,2},{3,4},{5,6},{7,8}};
    int array2[SIZE3][SIZE4] = {{2,4},{6,1}};
    int array3[SIZE1][SIZE4] = {0};
    tells me that this is a ilegal inisilisation, and you set you matrix size hard coded into the program, wheras i have a function i run to let the user enter the order of both matrices first. sorry for being a pain.
    heres my code
    Code:
    #include <stdio.h>
    #define MAX_R 4
    #define MAX_C 4
    #define MAX_C2 4
    typedef int coord[2];
    int OrdEnt (char 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 Display_Mat (coord *p_2d, int r, int c, int num)
    {
    	int r_i, c_i;
    	printf("\nMatrix %d is: \n",num);
    
    
    	for(r_i=0; r_i<r; r_i++){
    	for(c_i=0; c_i<c; c_i++)
    	{
    
    		printf("%5d",p_2d [r_i] [c_i]);
    		if(c_i != (c - 1))
    		continue;
    		printf("\n");
    
    	}
    	}
    }
    void EleEnt (coord *p_2d, int r, int c, int num)
    {
      int r_i, c_i;
    for(r_i=0; r_i<r; r_i++){
    		printf("\nfor row %d of Matrix_%d\n",r_i +1,num);
    	for(c_i=0; c_i<c; c_i++){
    		printf("\tEnter element value for column %d:",c_i +1);
    		scanf("%d",&p_2d [r_i] [c_i]);
    				}
    				}
    
    }
    void MatCalc (coord *p2d_1, coord *p2d_2, coord *p2d_3, int r, int c, int n)
    {
    
    	int c_i, r_i, N, sum;
    
    	for(r_i=0; r_i<r; r_i++){
    	for(c_i=0; c_i<c; c_i++){
    		sum = 0;
    	for(N=0; N<n; N++){
    	sum += (p2d_1 [r_i] [N]) * (p2d_2 [N] [c_i]);
    	p2d_3 [r_i] [c_i] = sum;
    
    				}
    				}
    			  }
    }
    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_C2];
    	int Matrix_3 [MAX_R] [MAX_C2];
    	int a = 1, b = 2, c = 3;
    	char in_c;
    
    do{
    	printf("This program will multiply 2 matrices (up to the order or 4x4),and display the result\n");
    
    	do{
    		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');
    	   }while(R2 != C);
    
           do{
    		EleEnt(Matrix_1,R,C,a);
    		Display_Mat(Matrix_1,R,C,a);
    		printf("\nIs this correct,y/n:?:");
    		scanf(" %c,&in_c");
    	  }while(in_c == 'n' || in_c == 'N');
    
    
           do{
    		EleEnt(Matrix_2,R2,C2,b);
    		Display_Mat(Matrix_2,R2,C2,b);
    		printf("\nIs this correct,y/n:");
    		scanf(" %c,&in_c");
    	 }while(in_c == 'n' || in_c == 'N');
    
    	MatCalc(Matrix_1,Matrix_2,Matrix_3,R,C2,C);
    	Display_Mat(&Matrix_3,R,C2,c);
    
    
    	printf("\nWould you like to multiply more matrices? y/n:");
    	scanf(" %c,&in_c");
    
    }while(in_c == 'n' || in_c == 'N');
    
    return (0);
    
    }

  8. #23
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    I am sorry, but im at work, and i dont have my usuall compiler. Im using some basic compiler with no debug, so i cant load my code to your program and get it working, I have changed my code so you can see how you might want to pass the arrays to a function calculate the items and print them out. Yes i have hardcoded my values in, you wont have to, your values will be in from the user by the time you call he function, so leave those declared, but uninitialized. here ya go, sorry i cant do more, learn from this and the reast should be easy, it does work.if i can, i will take a look at everything when i get home from work, lol thats late though. You need to learn this anyway, try to get my examples to work for you.
    Code:
    #include <stdio.h>
    
    #define MAX_R 2
    #define MAX_R2 2
    #define MAX_C 2
    #define MAX_C2 2
    
    void matcalc(int [][MAX_C], int [][MAX_C2], int [][MAX_C2]);
    
    int main(){
    
    	int array1[MAX_R][MAX_C];
    	int array2[MAX_R2][MAX_C2];
    	int array3[MAX_R][MAX_C2];
    
    	matcalc(array1, array2, array3);
    
    	return 0;
    }
    
    void matcalc(int Matrix_1[][MAX_C], int Matrix_2[][MAX_C2], int Matrix_3[][MAX_C2])
    {
    	int j, l, h, m = 0, sum = 0;
    
    	for (j = 0; j < MAX_R; j++){
    		for(l = 0; l < MAX_C; l++){
    			for(h = 0; h < MAX_C2; h++){
    				sum += Matrix_1[j][l] * Matrix_2[m][h];
    				printf("[ %d * %d += %d ]\n", Matrix_1[j][l], Matrix_2[m][h], sum);
    				Matrix_3[j][h] += Matrix_1[j][l] * Matrix_2[m][h];
    			};
    			m++;
    		};
    		m = 0;
    	};
    	printf("\n\n");
    	for (j = 0; j < MAX_R; j++){
    		for(l = 0; l < MAX_C2; l++)
    			printf("[%d]", Matrix_3[j][l]);
    		printf("\n");
    	};
    }
    Last edited by stumon; 04-09-2003 at 05:43 PM.
    The keyboard is the standard device used to cause computer errors!

  9. #24
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    I doint mind waiting, tried to implement last code you posted, had some sucess but 0 0 in the middle of matrices and element values seemed to go funny 2.

  10. #25
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Are you filing up the full 2d arrays? What do you mean by this:
    >>had some sucess but 0 0 in the middle of matrices and element values seemed to go funny 2.
    The keyboard is the standard device used to cause computer errors!

  11. #26
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    tried as a test run doing a 3x1 x a 1x2
    having set all the MAX to 4 as 4x4 is the biggest order i have to code for.
    did 54
    2 * 71 16
    9
    and got
    3834 864
    0 0
    639 144

  12. #27
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    If you understand the debugger in your compiler, put some break points right before it tried to sort through the matrices and watch the arrays, check their values, to make sure they are correct with what you inputted......if they are, there is a problem with the conversion, that i can help with, if your array entries are screwed, then check your input code.
    The keyboard is the standard device used to cause computer errors!

  13. #28
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    got rid of all my functions with pointers to the 2D arrays and then tried and its gone even more wrong!
    what have i done wrong?
    Code:
    #include <stdio.h>
    #define MAX_R 4
    #define MAX_R2 4
    #define MAX_C 4
    #define MAX_C2 4
    typedef int coord[2];
    int OrdEnt (char 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_C], int Matrix_2[][MAX_C2], int  Matrix_3[][MAX_C2])
    {
    
    	int j,l,h, m =0, sum = 0;
    	for (j=0; j<MAX_R; j++){
    	for (l=0; l<MAX_C; l++){
    	for (h=0; h<MAX_C2; h++){
    	sum += Matrix_1[j][l] * Matrix_2[m][h];
    	printf("[ %d * %d += %d ]\n", Matrix_1[j][l], Matrix_2[m][h], sum);
    	Matrix_3[j][h] += Matrix_1[j][l] * Matrix_2[m][h];
    	};
    	m++;
    	};
    	m = 0;
    	};
    
    	printf("\n\n");
    	for (j = 0; j<MAX_R; j++){
    	for (l=0; l<MAX_C2; l++){
    	printf("[%d]", Matrix_3[j][l]);
    	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_C2];
    	int Matrix_3 [MAX_R] [MAX_C2];
    	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 or 4x4),and display the result\n");
    
    	do{
    		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');
    	   }while(R2 != C);
    
    
    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",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);
    
    
    
    
    
    return (0);
    
    }

  14. #29
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Ok, a few things. If you have the user enter the max amount of size the matrices can be, and they enter 2x2, you can run through an array using the MAX_R which you declare at 4. Thats why you were getting the weird numbers. Also, i changed it back to pass what the user enters for the size and changed the matcalc() to use those entries as its maximum amount. I did find a flaw in my original matcalc() function, it would not do good if they user entered a larger column than row, like (1x4, 1x4) it would go out of bounds, i fixed that with another for loop. Another thing, you have no prototypes for your functions, thats where most of your warnings were coming from. I added those too. This was actually very easy to debug, i didn't change much, and all your logic was good, just that running through the array with the max values was not right, it need to only search what the user enters into the array. If you want it to be max 100 now, just change the #defines and the user can enter as big a matrix as he wants.
    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 ]\n", Matrix_1[j][l], Matrix_2[m][h], sum);
    					Matrix_3[j][h] += Matrix_1[j][l] * Matrix_2[m][h];
    				};
    			};
    		};
    	};
    	
    	printf("\n\n");
    	for (j = 0; j<r; j++){
    		for (l=0; l<c2; l++){
    			printf("[%d]", Matrix_3[j][l]);
    		};
    		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);
    	
    }
    Last edited by stumon; 04-09-2003 at 10:23 PM.
    The keyboard is the standard device used to cause computer errors!

  15. #30
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    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
    Im sorry to be a pain but its really bugging me as i cant see why it wont work, ive searched for other functions that do this and compared and they all pretty much use the same method yet i always get the wrong answer whatever variation i try. Only happens when the order is something like 3x3 * 3x2 rather than a 3x1 * 1x2.
    Last edited by mr_spanky202; 04-10-2003 at 08:27 AM.

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

Similar Threads

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

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