0

for an uni assignment I have to do the following: https://ibb.co/5WpMS0V (I cant upload a photo, the server is messed up or something)

What I've got so far is the following: Im trying to get all the characters that I need to put in, into an array but it's not working properly. Can anyone explain to me how to do this? Also, the subject this week is recursion, so I should make this with the use of recursion. If anyone knows how to get the characters in properly, then I know how to move on! Thanks in advance.


// libraries
#include <stdio.h>  // endables input/output commands
#include <stdlib.h> // enables standard library of functions

// main
int main(int argc, char *argv[]) {

int numberOfRows;
int numberOfColoms; 
scanf("%d %d", &numberOfRows,&numberOfColoms);  
char matrix[numberOfRows][numberOfColoms];
char letter = getchar();
scanf("%c" , &letter);

do {
    for (int i = 0; i < numberOfRows; i++) {
        for (int j = 0; j < numberOfColoms; j++) {
            scanf("%c" , &matrix[i][j]);
        }
    }
} while (letter != '\n');

 for (int i = 0; i < numberOfRows; i++) {
        for (int j = 0; j < numberOfColoms; j++) {
            printf("%c" , matrix[i][j]);
        }
 }
    return 0;
}
Justus148
  • 1
  • 1
  • 1
    What is the use of `letter`. If it is not '\n' the while loop will be endless. – CGi03 Oct 13 '22 at 16:12
  • Please show exactly what you entered, and see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Normally we put a space in front of `%c` to prevent it reading whitespace. Otherwise it reads *every* character, unlike `%d`. So you can simplify `char letter = getchar(); scanf("%c" , &letter);` to `scanf(" %c" , &letter);` – Weather Vane Oct 13 '22 at 16:18
  • 2
    `while (letter != '\n');` but the loop does not change `letter`. – Weather Vane Oct 13 '22 at 16:22
  • 2
    With "lines" and `scanf` you're set for failure. Use `fgets` to read lines and `sscanf` to parse them. – Cheatah Oct 13 '22 at 17:00
  • 1
    "With scanf you're set for failure" is equally true. http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html – William Pursell Oct 13 '22 at 17:10
  • In this case, I don't think you need `stdlib.h`. – Neil Oct 13 '22 at 17:12
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 13 '22 at 17:12
  • This is a good exercise. I think the big catch here is that when looking for the next letter you should skip the previous found letter. This is because words with loops are not valid. – Jardel Lucca Oct 14 '22 at 14:15

1 Answers1

0

The letter variable and the do .. while loop are unnecessary. You just need to read rows * cols characters.

Use " %c" with scanf to skip leading whitespace when reading a character. This will have the effect of ignoring the newlines that separate each line of input.

Remember to check the return value of scanf, which is the number of successful conversions, so that you do not operate on bad data.

Consider limiting the values of rows and cols to an appropriate range. Currently negative or excessively large values will have adverse effects.

Note that the use of recursion is likely expected for the rest of the task (solving the word search), not populating your matrix.

Keep in mind, the contents of matrix are not strings - they are not null-terminated. Use of <string.h> functions and similar will surely lead to undefined behaviour.

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int rows;
    int cols;

    if (2 != scanf("%d %d", &rows, &cols)) {
        fprintf(stderr, "Failed to read rows and columns.\n");
        return EXIT_FAILURE;
    }

    char matrix[rows][cols];

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            if (1 != scanf(" %c" , &matrix[i][j])) {
                fprintf(stderr, "Unexpected end of valid input.\n");
                return EXIT_FAILURE;
            }
        }
    }

    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            printf("%c" , matrix[i][j]);
        }

        putchar('\n');
    }
}
Oka
  • 23,367
  • 6
  • 42
  • 53
  • Thanks for the response! I understand it a bit however, there is a space in front of the %c in the for loop that scans for the characters, I tried to remove the space and then the scanning goes wrong. I don't understand why this happens. I know it has something to do with the enters / new lines but I do not understand this. Could you elaborate why there has to be a space in front of the %c in the scanf statement? – Justus148 Oct 14 '22 at 18:33
  • As explained, a leading white space causes `%c` to ignore any and all whitespace that proceeds a character. [This answer](https://stackoverflow.com/a/5240807/2505965) also explains it. You need this because your input contains newline characters (whitespace) that separate each row of characters. – Oka Oct 14 '22 at 21:16