Printing array
Author |
Message |
Andy
|
Posted: Mon Nov 14, 2005 8:21 pm Post subject: (No subject) |
|
|
umm is it me or does he have i >= 26?? shouldnt it be i <= 26? cuz it'd never enter the for loop
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
wtd
|
Posted: Mon Nov 14, 2005 8:24 pm Post subject: (No subject) |
|
|
Nice catch. For some reason I saw "<=".
|
|
|
|
|
 |
Andy
|
Posted: Mon Nov 14, 2005 8:25 pm Post subject: (No subject) |
|
|
if he changed that, his original code would've worked lol.. it would be bad code, but it would work haha
|
|
|
|
|
 |
md

|
Posted: Mon Nov 14, 2005 8:32 pm Post subject: (No subject) |
|
|
From the text of hte problem it seems like a rather poorly thought out assignment... and from the way the OP took to wtd's comments methinks he should perhaps try being a little more open to help (and critisism).
|
|
|
|
|
 |
Tubs

|
Posted: Tue Nov 15, 2005 1:07 am Post subject: (No subject) |
|
|
I am perfectly open to criticism but criticising my school is unnecessary. I know for a fact UW intro to programming is C++...
|
|
|
|
|
 |
md

|
Posted: Tue Nov 15, 2005 1:24 am Post subject: (No subject) |
|
|
Tubs wrote: I am perfectly open to criticism but criticising my school is unnecessary. I know for a fact UW intro to programming is C++... well then you've got your facts wrong... being a UW student in CS, and knowing for a fact that java (or scheme, depending) are used for into, with C++ not even being taught until 3rd year (pretty sure... haven't seen ti yet in second year at least).
|
|
|
|
|
 |
Tubs

|
Posted: Tue Nov 15, 2005 1:35 am Post subject: (No subject) |
|
|
My friend is in first year at UW and he doesn't stop complaining about C++ and he has no programming background.
|
|
|
|
|
 |
wtd
|
Posted: Tue Nov 15, 2005 1:36 am Post subject: (No subject) |
|
|
Tubs wrote: I am perfectly open to criticism but criticising my school is unnecessary. I know for a fact UW intro to programming is C++...
What you're being taught is not C++. It's C. They are distinct languages.
|
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
wtd
|
Posted: Tue Nov 15, 2005 1:43 am Post subject: (No subject) |
|
|
Tubs wrote: My friend is in first year at UW and he doesn't stop complaining about C++ and he has no programming background.
Neither C nor C++ are good languages to teach to beginners. It'd be somewhat acceptable if there weren't better choices, but there are plenty.
|
|
|
|
|
 |
Andy
|
Posted: Tue Nov 15, 2005 3:36 am Post subject: (No subject) |
|
|
umm what program is he in? if hes in system design, or mechatronics or mechanical, he'd be doing c++, if they're in electrical or computer he'd be doing c#, and if they're in math, its either scheme or java. o and cornflakes, you can take c++ starting 2A if you follow through their stupid chart on the website
|
|
|
|
|
 |
Tubs

|
Posted: Tue Nov 15, 2005 11:33 am Post subject: (No subject) |
|
|
Mechatronics, the crazy bastard.
|
|
|
|
|
 |
Tubs

|
Posted: Tue Nov 15, 2005 11:57 am Post subject: (No subject) |
|
|
Anyway, in a different but related assignment I had to print a 25x25 array of X's that is read by a .txt file (not the assignment, but that is my task for right now). The thing is, every 25th character is some gibberish and I can't figure out why.
code: |
#include <stdio.h>
int main(int argc, char *argv[])
{
int x = 0, y = 0, k, j;
char cells[24][24], i;
FILE *in, *out; //Pointers to open and close the file
in = fopen( "data.txt", "r" ); // open a file for reading
while( fscanf( in, "%c", &i ) != EOF ) // Read from the file until EOF
{
if (x == 24)
{
x = 0;
y += 1;
}
cells[x][y] = i;
x += 1;
}
fclose (in);
for (j = 0; j <= 24; ++j)
{
for (k = 0; k <= 24; ++k)
{
printf ("%c", cells[k][j]);
}
printf ("\n");
}
system ("pause");
return 0;
}
|
Description: |
|
 Download |
Filename: |
data.txt |
Filesize: |
625 Bytes |
Downloaded: |
114 Time(s) |
|
|
|
|
|
 |
md

|
Posted: Tue Nov 15, 2005 12:44 pm Post subject: (No subject) |
|
|
Problem 1 is that your array is too small... the number in the [] brackets is the number of elements in the array, not a bound. For a 25 character matrix it'd be [25][25].
|
|
|
|
|
 |
Tubs

|
Posted: Tue Nov 15, 2005 1:09 pm Post subject: (No subject) |
|
|
Ok I changed the array size (again ) and modified the reading code a little bit so I now have a (weird) 25x25 grid. The question is now, why does it print out random characters at the end of some lines and stops without printing the last X?
code: |
XXXXXXXXXXXXXXXXXXXXXXXXXÂ
XXXXXXXXXXXXXXXXXXXXXXXXX@
XXXXXXXXXXXXXXXXXXXXXXXXX☻
XXXXXXXXXXXXXXXXXXXXXXXXXÂ
XXXXXXXXXXXXXXXXXXXXXXXXXÂ
XXXXXXXXXXXXXXXXXXXXXXXXXÂ
XXXXXXXXXXXXXXXXXXXXXXXXXÂ
XXXXXXXXXXXXXXXXXXXXXXXXXl
XXXXXXXXXXXXXXXXXXXXXXXXXÂ
XXXXXXXXXXXXXXXXXXXXXXXXX@
XXXXXXXXXXXXXXXXXXXXXXXXX☻
XXXXXXXXXXXXXXXXXXXXXXXXX\
XXXXXXXXXXXXXXXXXXXXXXXXXÂ
XXXXXXXXXXXXXXXXXXXXXXXXX@
XXXXXXXXXXXXXXXXXXXXXXXXX☻
XXXXXXXXXXXXXXXXXXXXXXXXX☼
XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXX↓
XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXX↓
XXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXÂ
|
|
|
|
|
|
 |
Tubs

|
Posted: Tue Nov 15, 2005 1:11 pm Post subject: (No subject) |
|
|
Why can't I edit my posts?
Here is the code for the above output:
code: |
#include <stdio.h>
int main(int argc, char *argv[])
{
int x = 0, y = 0, k, j;
char cells[25][25], i;
FILE *in, *out; //Pointers to open and close the file
in = fopen( "data.txt", "r" ); // open a file for reading
while( fscanf( in, "%c", &i ) != EOF ) // Read from the file until EOF
{
if (x == 24)
{
cells[x][y] = i;
x = 0;
y += 1;
}
else
{
cells[x][y] = i;
x += 1;
}
}
fclose (in);
for (j = 0; j <= 25; ++j)
{
for (k = 0; k <= 25; ++k)
{
printf ("%c", cells[k][j]);
}
printf ("\n");
}
system ("pause");
return 0;
}
|
|
|
|
|
|
 |
|
|