1

The code is to store rows and columns of matrix in a linear manner in another array.

Example: Given a matrix:

a b c d

e f g h

i j k l

m n o p

The task is to store values in a new matrix:

Matrix H contains:

a b c d

e f g h

i j k l

m n o p

Matrix V contains:

a e i m

b f j n

c g k o

d h l p

The code I have written is:

#include<stdio.h>

int main() {
  int m, n;
  char a[10][10];

  scanf("%d%d", &m, &n);

  char horizontal[m][10], vertical[n][10];
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      scanf(" %c", &a[i][j]);
    }
  }

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      horizontal[i][j] = a[i][j];
      vertical[i][j] = a[j][i];
    }

  }
  printf("horizontal values are:\n");
  for (int i = 0; i < m; i++) {
    printf("%s\n", horizontal[i]);
  }
  printf("vertical values are:\n");
  for (int i = 0; i < m; i++) {
    printf("%s\n", vertical[i]);
  }
}

The output when I execute this code is:

4
4
a b c d
e f g h
i j k l
m n o p

horizontal values are:
abcd
efgh
ijklñ²b
mnopb

vertical values are:
aeim
bfjn
cgko
dhlpb

What is wrong in this code?

3
  • You are treating the arrays as strings but you have not written any null string terminator to them (and perhaps not allocated enough room for them). One fix could be to initialise your arrays: char horizontal[m][10] = { 0 }; etc. Commented Mar 21, 2020 at 19:07
  • @WeatherVane: Good idea, but it's a VLA, so that easy fix is out. (I think all the arrays should be VLA's here and they shouldn't use the magic number 10 anywhere.) Commented Mar 21, 2020 at 19:09
  • @MOehm mybad MS doesn't let me use them. Commented Mar 21, 2020 at 19:10

2 Answers 2

3

You have extra characters, because when you treat the character rows as C strings, they must be null-terminated. Your arrays are VLA's, variable length arrays that cannot be initialized when they are defined.

There are several ways to solve this.

(1) Don't treat the char array as C string and print your matrix with two nested loops. Boring, but gets the job done.

(2) Null-terminate your arrays. To do this, you must allocate an extra space for each row, where you put the '\0'. (Let's also get rid of the 10 here. You know how big the arrays should be, so allocate accordingly.)

char horizontal[m][n + 1], vertical[n][m + 1];

Then set the last char in each row to the null character:

for (int i = 0; i < m; i++) horizontal[i][n] = '\0';
for (int i = 0; i < n; i++) vertical[i][m] = '\0';

(Weather Vane has suggested to initialize all arrays to zero, but unfortunately, that works only for arrays whose size is known at compile time. Your arrays have a variable length, though. You could usememset from <string.h> to zero out the arrays if you feel comfortable with raw data and sizeof.)

(3) Use the capabilities of printf format strings: You can give a precision to a string. That's a number that means "print at most that many characters". By specifying an asterisk, you can read that number from the argument list. So:

for (int i = 0; i < m; i++) {
    printf("%.*s\n", n, horizontal[i]);
    //       ^^      ^ precision
}

for (int i = 0; i < n; i++) {
    printf("%.*s\n", m, vertical[i]);
    //       ^^      ^ precision
}
Sign up to request clarification or add additional context in comments.

Comments

1

your input is 4 and 4 matrix but you have decalred a 2D array of char [m][10] and [n][10] when you use %s your printf will be for 10 chars since you didn't use \0 to terminate your string(you will print what was in buffer)

you should use \0 or print the same way you scan with %c

int main()
{
    int m, n;
    char a[10][10];

    scanf("%d%d", &m, &n);

    char horizontal[m][10], vertical[n][10];
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            scanf(" %c", &a[i][j]);
        }
    }

    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            horizontal[i][j] = a[i][j];
            vertical[i][j] = a[j][i];
        }

    }
    printf("horizontal values are:\n");
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        printf("%c", horizontal[i][j]);
        printf("\n");
    }
    printf("vertical values are:\n");
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        printf("%c", vertical[i][j]);
        printf("\n");
    }
}

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.