Thread: Displaying Arrays

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

    Unhappy Displaying Arrays

    Have written a program to multiply maatrices( as im sure you can guess by some other posts, signs of a desperation so apologies).think i have solved everything bar actually displaying the matrices on the screen. problem is that the first matrices instead of displaying properly either run in to the the next disply line or miss off the bottom rows of the matrix, however when i remove the following code and reompile it displays fine. secondly the result matrix will only display one or two correct values for the elements and give 0's for the rest.
    code so far
    Code:
    #include <stdio.h>
    #define MAX_R 4
    #define MAX_C 4
    #define MAX_C2 4
    #define MAX_R2 4
    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);
    	     }
    
    int main(void)
    
    {
    	int R = 0, C = 0, R2 = 0, C2 = 0;
    	int Matrix_A [MAX_R] [MAX_C];
    	int Matrix_B [MAX_R2] [MAX_C2];
    	int Matrix_C [MAX_R] [MAX_C2];
    	int a = 1, b = 2;
    	int c_i, r_i, N, sum;
    	char in_c;
    
    	printf("This program will multiply 2 matrices (up to the order or 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');
    
    
    for(r_i=0; r_i<R; r_i++){
    printf("\nfor row %d of Matrix_A\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_A [r_i] [c_i]);
    }
    }
    
    
    	printf("\nMatrix A is: \n");
    
    	for(r_i=0; r_i<R; r_i++){
    	for(c_i=0; c_i<C; c_i++)
    	{
    
    		printf("%3d",Matrix_A [r_i] [c_i]);
    		if(c_i != (C - 1))
    		continue;
    		printf("\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<R2; r_i++){
    printf ("\nFor row % of Matrix B\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_B [r_i] [c_i]);
    }
    }
    
    	printf("\nMatrix B is: \n");
    
    	for(r_i=0; r_i<R2; r_i++){
    	for(c_i=0; c_i<C2; c_i++)
    	{
    
    		printf("%3d",Matrix_B [r_i] [c_i]);
    		if(c_i != (C2 - 1))
    		continue;
    		printf("\n");
    
    
    	}
    }
    	for(r_i=0; r_i<R2; r_i++){
    	for(c_i=0; c_i<C; c_i++){
    	sum = 0;
    	for(N=0; N<R; N++)
    	sum += Matrix_A [r_i] [N] * Matrix_B [N] [c_i];
    	Matrix_C [r_i] [c_i] = sum;
    
      }
    
    	printf("\nMatrix C is: \n");
    
    	for(r_i=0; r_i<R; r_i++){
    	for(c_i=0; c_i<C2; c_i++)
    	{
    
    		printf("%4d",Matrix_C [r_i] [c_i]);
    		if(c_i != (C2 - 1))
    		continue;
    		printf("\n");
    
    
    	}
    }
    }
    
    
    }
    	return (0);
    
    }
    eg of what i mean for result display instead of getting:
    Code:
    Matrix C is:
    3834 864
    142   12
    639   144
    I will get
    Code:
    3844 0
    0       0
    0       0
    Any ideas? im sorry if people on the board are being to think im a bit whiney but this is the first time ive ever tried programming and the frustration of having it half work is beyone description.
    Thanks in advance to anyone

  2. #2
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    Depending on the compiler, run through a debug with break points to check to see what the actual values are. You can also check to see what the values are its trying to access then print. It may be printing right, but the entries are 0 for what ever reason.
    The keyboard is the standard device used to cause computer errors!

  3. #3
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    Just a comment on this line,
    Code:
    scanf ("%dx%d",&*r,&*c);
    here, &*r is a bit strange. r is already a pointer to an integer. And scanf() is expecting a pointer. So instead of the above code , you could simply say,
    Code:
    scanf ("%dx%d",r,c);
    . Again, the above code is not incorrect.

    Getting back to your code,
    The easiest way to deal with these problems is to make use of conditional compilation. That is compilation will happend if a certain condition is met.
    Code:
    #define __DEBUG__
    ..
    ..
    ..
    #ifdef __DEBUG__
     printf("debug output....... r is ",r)
    #endif
    with this way, you could print intermideate valuse of variables ( for debugging purposes) and once it is finish compile the poduction code, simple by undefining the macro __DEBUG__.
    Try this technique with your code.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    32
    Thanks for debug advivce, discoevered that display method is fine so have functioned it and everything else is fine the problem part of code is for entering the 2d array,it refuses to record the proper values once the row number is greater than 1. ie
    i enter:\
    Code:
    11 12
    21 22
    and it will record it as
    Code:
    11 12
    0   0
    The code i am using to enter the element values (for varying values of R and C ,the matix order,) is
    Code:
     for(r_i=0; r_i<R; r_i++){
    		printf("\nfor row %d of Matrix_A\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]);
                                            }
                                             }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM
  5. displaying data from a structure of arrays
    By Unregistered in forum C Programming
    Replies: 8
    Last Post: 03-14-2002, 12:35 PM