Your output looks good, but that was without the need to read the matrix file, so the access type would not matter.
Here's my program and output. Some of the weirder characters do not show up correctly. Also, I noticed there was a beep while running the program. What does that mean?
Code:
#include <stdio.h> //standard input/output library
#include <stdlib.h> //required for srand()
#define ROW 20
#define COL 20
int main (void) //main function
{
/* declaration */
FILE *f1, *f2; //declares pointers to file
char input1[100], input2[100]; //declares file location variables
char str[100]; //declares string variable
int i, j, c; //declares integers
int matrix[ROW][COL];//declares numeric matrix
char alpha[ROW][COL]; //declares alpha matrix
/* data input */
printf("Input the matrix file: ");
gets(input1); //inputs location of numeric matrix
printf("Input the alphabet file: ");
gets(input2); //inputs location of alpha matrix
printf("\n");
f1 = fopen(input1, "w+"); //opens numeric array for writing
f2 = fopen(input2, "w+"); //opens alpha array for writing
/* checks if the file(s) is accessible */
if (f1 == NULL || f2 == NULL) //executes if a file is inaccessible
{
printf("File access error.\n"); //displays error message
return 1; //exits the program with error code
}
srand(time(NULL)); //seeds random number with current time
/* generates the numeric matrix */
for (i = 0; i < ROW; i++) //manipulates row
{
for (j = 0; j < COL; j++) //manipulates column
{
matrix[i][j] = rand() % 10; //random number from 0 to 9
fprintf(f1, "%d ", matrix[i][j]);
}
fseek(f1, -1L, SEEK_CUR); //clears the last space of each row in f1
fputc('\n', f1); //starts a new row in f1
}
rewind(f1);
/* reads from file and converts the numeric matrix into an alpha matrix */
for (i = 0; i < ROW; i++) //manipulates row
{
for (j = 0; j < COL; j++) //manipulates column
{
if ((c = fgetc(f1)) != ' ')
{
alpha[i][j] = c + '0'; //conversion
fprintf(f2, "%c ", alpha[i][j]); //writes to alpha file
}
}
fseek(f2, -1L, SEEK_CUR); //clears the last space of each row in f2
fputc('\n', f2); //starts a new row in f2
}
/* displays numeric arrays in console */
for (i = 0; i < ROW; i++) //manipulates row
{
for (j = 0; j < COL; j++) //manipulates column
{
printf("%d ", matrix[i][j]); //prints numeric matrix
}
printf("\n"); //prints newline
}
printf("\nThe above matrix converts to:\n\n");
/* displays alpha array in console */
for (i = 0; i < ROW; i++) //manipulates row
{
for (j = 0; j < COL; j++) //manipulates column
{
printf("%c ", alpha[i][j]); //prints alpha matrix
}
printf("\n"); //prints newline
}
/* checks if the file(s) closes correctly */
if (fclose(f1) != 0 || fclose(f2) != 0) //executes if the file does not close correctly
printf("File close error.\n"); //displays error message
return 0; //exits the program normally
}
Code:
Input the matrix file: a.txt
Input the alphabet file: b.txt
7 3 7 0 3 4 4 3 4 8 2 8 0 6 5 8 9 0 0 1
0 3 1 0 8 5 1 3 0 9 7 8 1 6 0 4 0 6 6 5
9 9 7 6 4 2 4 8 6 6 1 7 3 3 2 0 9 8 8 9
2 2 1 4 3 8 3 0 0 1 1 2 2 6 1 5 6 8 7 3
1 9 4 7 9 3 1 8 8 6 4 1 2 3 9 1 9 7 5 8
1 5 1 7 5 0 4 3 2 2 6 6 8 2 3 6 7 4 7 5
6 6 9 3 0 1 3 6 9 2 4 3 4 7 0 7 6 1 2 9
8 9 3 2 2 6 7 3 5 1 1 5 1 9 0 9 7 8 5 2
9 3 6 5 1 5 7 3 5 1 8 5 5 7 5 0 0 7 5 1
2 8 1 7 8 0 1 9 4 9 8 4 8 1 2 7 7 0 0 2
4 8 0 8 2 9 5 2 4 6 7 0 0 2 9 3 2 6 2 3
5 0 5 3 1 2 4 4 3 5 7 3 7 8 1 6 1 1 2 5
5 3 1 9 6 1 7 1 3 5 0 4 4 0 0 0 3 1 6 2
9 0 7 4 4 8 1 8 3 1 0 2 4 6 1 7 3 3 5 2
4 4 8 2 8 3 7 8 8 2 1 0 8 7 8 6 7 5 0 3
5 1 9 8 4 5 0 9 5 0 4 1 0 0 0 2 0 4 0 6
6 7 0 2 7 7 1 5 6 7 1 1 5 7 0 0 3 0 5 2
5 7 1 6 5 3 4 2 3 1 5 4 4 0 4 9 6 1 6 9
1 9 4 5 6 8 5 3 1 7 2 5 0 4 1 2 5 2 6 2
9 7 4 7 3 9 1 1 8 7 9 5 1 1 4 5 1 1 0 6
The above matrix converts to:
g c g ` c d d c d h
b h ` f e h i ` ` a :
` c a ` h e a c ` i
g h a f ` d ` f f e :
i i g f d b d h f f
a g c c b ` i h h i :
b b a d c h c ` ` a
a b b f a e f h g c :
c a h h f i
d a b c i ! a # i % g ' e ) h :
a - e / a 1 g 3 e 5 ` 7 d 9 c ; b = b ?
f A f C h E b G c I f K g M d O g Q e :
f U f W i Y c [ ` ] a _ c A f C i E b G
d I c K d M g O ` Q g S f U a W b Y i :
h } i c b b f g c e a
a e a i ` i g h e b :
i c f e a e g c e a
h e e g e ` ` g e a :
b h a g h ` a i d i
h d h a b g g ` ` b :
Process returned 0 (0x0) execution time : 3.479 s
Press any key to continue.
Here's the actual output printed in b.txt
Code:
t v t y u t t s q t
s z t v y y q u z y K
y s q t x y v r q y
x z r x s y r s r q K
u u x w y u t s t x
s s w r z r r w z v K
u w x q x w y w s x
t y w x v y y r y s K
q q y w s x v z z v
u u q y s r w t y x K
v t y z w s u z v q
z u u q u u w z x q K
t q q y x r z y y y
u u y s w t w v w y K
u s r q w q y r y s
s w s w s z z q x z K
z y u x z t x t t x
x v u s v w t w t q K
q r s y u q v t s s
y u y t v r w q u s K