Thread: Printing out a matrix of characters

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    12

    Unhappy Printing out a matrix of characters

    Hello,

    So this question, I absolutely cannot get. Have been stuck here for hours
    This is way too frustrating. I'm posting the question and my solution (REAL NOOB).
    I have an exam in a few days and this question is probably nothing compared to what might be on it. Thanks a lot in advance!

    The question is:

    Get the number of rows and columns from the user for the matrix. Then ask the user to enter the characters for the matrix. Finally, display the value on the top, right corner of the matrix.

    For example,

    The letter "q" must be displayed for the 4X2 Matrix of:

    a e c q
    d c o n


    My attempt:

    Code:
    #include<stdio.h>
    main()
    {
        char **matrix;
        int i,j;
        int row,col;
        
        printf ("Rows:");
        scanf ("%d",&row);
        printf ("\nColumns: ");
        scanf ("%d",&col);
        
        matrix=(char**)calloc(row,sizeof(char*));
        for(i=0;i<col;i++)
        matrix[i]=(char*)calloc(col,sizeof(char));
        
        for(i=0;i<row;i++)
        
        {
            for(j=0;j<col;j++)
            {
                scanf("%s",matrix[i]);
                if(matrix[i][j]==matrix[0][col-1])
                    printf("Top right corner is: %c",matrix[i][j]);
            }
        }
    }

  2. #2
    Registered User
    Join Date
    Feb 2012
    Posts
    12
    This is the same program I got working, but using integers.

    Code:
    #include<stdio.h>
    main()
    {
    	int **matrix;
    	int i,j;
    	int row,col;
    	
    	printf ("Rows: ");
    	scanf ("%d",&row);
    	printf ("\nColumns: ");
    	scanf ("%d",&col);
    	
    	matrix=(int**)calloc(row,sizeof(int*));
    	for(i=0;i<col;i++)
    	matrix[i]=(int*)calloc(col,sizeof(int));
    	
    	for(i=0;i<row;i++)
    	
    	{
    		for(j=0;j<col;j++)
    		{
    			scanf("%d",&matrix[i][j]);
    			if(matrix[i][j]==matrix[0][col-1])
    				printf("Top right corner is: %d\n",matrix[i][j]);
    		}
    	}
    }

  3. #3
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    So you only have to display the top right corner everytime?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("%s",matrix[i]);
    The problem here is you're reading a string, not a char.

    Perhaps
    scanf(" %c",&matrix[i][j]);
    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.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    12
    Quote Originally Posted by Salem View Post
    > scanf("%s",matrix[i]);
    The problem here is you're reading a string, not a char.

    Perhaps
    scanf(" %c",&matrix[i][j]);
    Yup, tried that. Doesn't work either.

    Code:
    #include<stdio.h>
    main()
    {
    	char **matrix;
    	int i,j;
    	int row,col;
    	
    	printf ("Rows: ");
    	scanf ("%d",&row);
    	printf ("\nColumns: ");
    	scanf ("%d",&col);
    	
    	matrix=(char**)calloc(row,sizeof(char*));
    	for(i=0;i<col;i++)
    	matrix[i]=(char*)calloc(col,sizeof(char));
    	
    	for(i=0;i<row;i++)
    	
    	{
    		for(j=0;j<col;j++)
    		{
    			scanf("%c",&matrix[i][j]);
    			if(matrix[i][j]==matrix[0][col-1])
    				printf("Top right corner is: %c\n",matrix[i][j]);
    		}
    	}
    Doesn't really work properly as the integer one I made

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    12
    Quote Originally Posted by camel-man View Post
    So you only have to display the top right corner everytime?
    Yup, exactly.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Read my post again, you managed to delete a critical space in the scanf call.
    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.

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    12
    Quote Originally Posted by Salem View Post
    Read my post again, you managed to delete a critical space in the scanf call.
    Whoah! THANK YOU SO MUCH! You saved so much of my time. Couldn't believe it worked. And sorry about not noticing that space. Never knew about the spacing and stuff. Once again, I really appreciate your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing non-printing characters in ^ and M- notation
    By sbeard22 in forum C Programming
    Replies: 6
    Last Post: 10-03-2008, 11:12 PM
  2. Printing only certain characters
    By dmkanz07 in forum C Programming
    Replies: 9
    Last Post: 04-18-2007, 07:40 AM
  3. Printing with Unicode characters
    By chibisetsuna7 in forum C Programming
    Replies: 15
    Last Post: 06-19-2006, 10:26 PM
  4. Printing different representations of characters...
    By smoothdogg00 in forum C Programming
    Replies: 3
    Last Post: 03-04-2006, 01:05 PM
  5. Printing a Matrix?
    By Nebbuchadnezzar in forum C++ Programming
    Replies: 4
    Last Post: 07-08-2005, 08:11 PM

Tags for this Thread