ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am, however, declaring my two-dimensional array of characters as a char array[17][17], to give a buffer all the way around the grid of characters. I'm trying to initialize this buffer to be a simple ' ' character, (just a space character), but when I try to output the results, my algorithm "looks as if it has failed". Could someone take a look at my functions and see what I may be doing wrong?
My function to red in the list of letters, and declare spaces around the grid of characters is:
My high level function that implements its usage is:
Code:
void GenerateLetterList (char inputFileName [ ])
{
FILE *inputFile;
char puzzle [LETTERARRAYROWSIZE][LETTERARRAYCOLUMNSIZE];
inputFile = fopen (inputFileName, READFILE);
if (inputFile == NULL)
{
fprintf (stderr, "Error - Cannot open %s for reading!\n",
inputFileName);
exit (-1);
}
ReadLetterList (inputFile, puzzle);
int i, j;
for (i = 0; i < LETTERARRAYROWSIZE; i++)
{
for (j = 0; j < LETTERARRAYCOLUMNSIZE; j++)
{
printf("%c", puzzle[i][j]);
}
if (i < 1 || i > 15)
{
printf ("\n\n");
}
}
printf ("\n\n");
printf("%c\n\n", puzzle[0][0]);
/*for (i = 1; i <= 15; i++)
{
for (j = 1; j <= 15; j++)
{
printf("%c", puzzle[i][j]);
}
}*/
/*DisplayLetterList (puzzle);*/
}
When I run the program and try to display the grid of letters, I get this:
Code:
EHUKXDICNMYTYLF
WOCJSKLTIMHONI I
NOTGNIHSAWZIF NP
IRBWNIIWPPIE RCE
BMETVELIIOK OWOK
QUOWCLEVEL ANDLN
VNCCOORTH TSNUNA
AMKHRHFE LSIONOM
DULNAAN ETXUQNSU
AIOATN VEORKBARR
MMPZV EANSUALBET
SYSD SWGNBITCLFI
OHZ OMYDENNEKWFD
IA OTNARGRNVTFEF
K
I want it to look like my grid of characters has a buffer all the way around, full of spaces, when it is output to the console.
Am I doing something wrong with code implementation, or perhaps output. If any of the functions are commented out in the high-level generate words function, it is because they are not being used at this time, and it is in its primordial test phase.
The problem lies in getting input. Specifically, the algorithm uses fgetc() and stores the return value in the array no matter what. Now look back at the input file. It may look like there are not other characters in the file, but what about newlines and EOF?
Take a look at the code you added. What happens when the character pulled in is a newline? What gets stored in that array location? That's the reason why your output has weird characters in it (like '?' and '@'). If a newline character is read in, the program correctly ignores it. However, the program incorrectly advances to the next array location without putting anything in the array; the location that would have stored the newline is left with random, uninitialized garbage.
The program will need to wait until characterCopy contains a legitimate value, copy that value into the character array, and then advance to the next array location.
Is there a way to do this while keeping the two for-loops, I tried thinking about it using a while loop in my nested for loops, but this doesnt seem to work:
I've worked on it a little bit more, and I am getting closer, but I don't understand why some of the characters are not being read and stored properly:
I figured out the problem, I wanted to do too much to fast basically. Thanks to dark_helmet for his guidance, it was extremely helpful.
I guess my urge to call functions like a billion times inside loops stems from my mathematical analysis of the problem, but after looking at it in incremental steps:
The reason I say that is because it'sconfusing. What happens first? The assignment to characterCopy? The comparison between characterCopy and the newline?
For the stuff in blue, I changed that simply because it makes more sense to me to use "==" comparisons rather than "<" or ">". Also, you created #defines for LETTERARRAYROWSIZE and LETTERARRAYCOLUMNSIZE... use them By using literal values of 15, your program breaks if you ever change the #define values. This way, you can change them to whatever you want and it'll still be happy. As a side note, I prefer underscores in all-caps #defines; it makes them easier to read.
For the stuff in green, this is a simple way to do it. If you want, you could add tests for EOF. However, the newlines are what throw your program off. It's just a bad idea to assign a value in a conditional and use that same value in the same statement. Fight the urge to make "condensed" code. Spell it out as simply as you can. Trust me, when you go back to look at it later, it will be extremely helpful. Condensed/obscure code is for competitions; not for real work.
I see what you're saying. I changed some things to #defines for better readibility and understanding, but I prefer to keep my code for reading in the characters similar to the one I posted in my last post. I understand how both would work, and how yours is more efficient, but for logic purposes, it seems like the problem is better addressed by my code, if such a thing is possible.
I guess what I'm trying to say is both algorithms address the problem, but mine kind of addresses the problem in context more so than initializing characterCopy as '\n'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.