nuke1872 said:
Lets put aside the previous code. I am trying this very simple code.
Please give your critique..
Sure. Stop ignoring the advice I've been giving you.
Provide context when you post a followup. Read
#include <stdio.h>
int main(){
This is legal, but "int main(void)" is better.
int n,m;
double x[13][13];
13 is a "magic number", meaning that there's no indication in your
program of why you chose this particular value, or what it means. You
use 13 four times. If you decide to change it to 14, you'll have to
change it in four different locations.
I suggest adding something like "#define SIZE 13", and using SIZE
rather than a literal 13.
FILE *inp;
inp = fopen("net.txt","r");
And what happens if the fopen() call fails? (Answer: inp is set to a
null pointer, and the fscanf call invokes undefined behavior; when I
tried it, I got a segmentation fault.) Always check whether library
calls succeeded, even if your only response to a failure is to exit.
For example:
inp = fopen("net.txt", "r");
if (inp == NULL) {
fprintf(stderr, "Failed to open net.txt\n");
exit(EXIT_FAILURE);
}
(This requires a "#include said:
for (n = 0; n < 13; ++n) {
for (m = 0; m < 13; ++m)
fscanf (inp, "%lf", &x[n][m]);
This reads a double literal from inp, and stores the converted value
in x[n][m]. Ok, but what happens if there wasn't a double literal to
read from the input file? fscanf() returns a result; use it, and
decide how to respond if the result indicates that it failed.
Your program should work, but *only* if "net.txt" contains at least
169 floating-point literals (actually, fscanf with "%lf" also accepts
integer literals, converting them to floating-point). Don't assume
your input is correct; check it.
}
fclose (inp);
return(0);
A minor point: the parentheses are harmless, but unnecessary.
Personally, I prefer to avoid parentheses on a return statement; they
make it look too much like a function call, and the distinction is
important. But "return(0);" is perfectly legal, and some people even
prefer it.
You don't do anything with the values once you've read them. At least
for testing purposes, it would be good to write a loop that displays
the contents of your array.
*Please* indent your code. It's very difficult to read with
everything left-justified, and it will get much worse as your code
becomes more complex.