1

Q2: Implelment the follwoing function ArrPrintMatrix(char *(p)[7]) which prints the content of the matrix[m][7] ={"SHAHBAZ","AYAZ"} in to 3x3 Matrix

Sample Output

S H A                      
H B A                       
Z A Y ..

My question is : here is code only problem i am getting is a space after one name is completed..how to remove that space . I have this question in my assignments , that have to be submitted on sunday (11-11-18).. My code is:

#include<stdio.h>
int main()
{
    void ArrPrintMatrix(char *p);//function declaration
    char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
    ArrPrintMatrix(&matrix[0][0]);//calling function with base address

}

void ArrPrintMatrix(char *p)
{
    int i;
    for(i=0;i<16;i++)
    {
        if(i>=9)//since 3 by 3 matrix is required
            break;
        if(i==3||i==6||i==9)//changing line since 3 by 3 matrix is needed
            printf("\n");
        printf("%c ",*(p+i));//prininting chracters

    }
}
2
  • Nasty question. It tells you to write a function like this ArrPrintMatrix(char *(p)[7]) and gives you a string that's 8 characters long ("SHAHBAZ" plus a terminating zero). Commented Nov 9, 2018 at 16:42
  • thank you so much for quick reply . I donot understand hot to use ArrPrintMatrix(char *(p)[7]) and to pass it to function can you please please help me ....please Commented Nov 9, 2018 at 16:46

3 Answers 3

1

You should use char (*p)[8] not char* p

The following code could wrok:

#include<stdio.h>
int main()
{
    void ArrPrintMatrix(char (*p)[8]);//function declaration
    char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
    ArrPrintMatrix(matrix);//calling function with base address

}
void ArrPrintMatrix(char (*p)[8])
{
    // i will point to one of the strings in the set of strings
    // j will point into the string we are inspecting
    // k will count how many characters we have printed
    int i = 0, j = 0, k = 0;

    // we only need to print the first 9 printable characters we find
    while (k != 9)
    {
        // if we have reached the end of an input string (the null-terminator),
        // then move on to the next element in the array, and reset
        // the string pointer to the beginning of the new string
        if (p[i][j] == '\0') {
            ++i;
            j = 0;
        }

        // print the character we are now pointing at,
        // and increment the string pointer
        printf("%c ", p[i][j++]);

        // keep count of how many characters we have printed
        ++k;

        // if k is divisible by 3, start a new row
        if(k%3 == 0)
            printf("\n");
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

perfectly done i must appreciate bou unfortunately i don't understand how this code is working ..I also have exams on monday :(
can you please tech me your code by commenting or something like dry run..I would be thankfull
0

As a follow-on to my other answer, if you do use my logic to skip over the '\0' that terminates the strings, you will need to use a different variable to keep track of how many characters you've actually printed, and just let i keep track of where you are in the input string(s). Like so:

#include<stdio.h>
int main()
{
    void ArrPrintMatrix(char *p);//function declaration
    char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation
    ArrPrintMatrix(&matrix[0][0]);//calling function with base address

}

void ArrPrintMatrix(char *p)
{
    int i, j;
    for(i=0, j=0;i<16;i++)
    {
        if(j>=9)//since 3 by 3 matrix is required
            break;
        if(j==3||j==6||j==9)//changing line since 3 by 3 matrix is needed
            printf("\n");
        if (*(p+i)==0) continue; //don't try to print the trailing '\0'
        printf("%c ",*(p+i));//prininting chracters
        j++; //increment counter of characters actually printed

    }
}

Output:

S H A 
H B A 
Z A Y 

Note the use of the j variable, and how it is incremented with j++ only after actually printing a character.

13 Comments

if (*(p+i)==0) continue; //don't try to print the trailing '\0' I donot understand this point , i mean why donot i say thay if *(p+j)=='0'??
'0' is very different from 0; '\0' on the other hand equates to 0
you could use either if (*(p+i)==0) or if (*(p+i)=='\0'), but not if *(p+j)=='0', which is what you have in your comment
can you help me understand this code please your explanation is tremendous #include<stdio.h> int main() { void ArrPrintMatrix(char (*p)[8]);//function declaration char matrix[2][8] ={"SHAHBAZ","AYAZ"};//2d array initiliation ArrPrintMatrix(matrix);//calling function with base address } void ArrPrintMatrix(char (*p)[8]) { int i = 0, j = 0, k = 0; while (k != 9) { if (p[i][j] == '\0') { ++i; j = 0; } printf("%c ", p[i][j++]); ++k; if(k%3 == 0) printf("\n"); } }
Sorry about that i have implemented your answer . But just for the sake of understanding i asked you, cause i like your explanation...
|
0

What you are missing is that there is a trailing '\0' at the end of SHAHBAZ, which you are also "printing", but because '\0' does not have a character representation, you are seeing what looks like an 'extra' space.

Here is the smallest change I can think of to address this exact problem; add:

if (*(p+i)==0) continue; //don't try to print the trailing '\0'

just above your existing line:

printf("%c ",*(p+i));//prininting chracters

Output:

S H A 
H B A 
Z A 

There are other things I would do differently than how you are doing them, but this addresses your exact question, using your coding style.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.