0

Hey so this is frustrating the hell out of me and I don't know why it isn't working. As of right now I just want to make a basic 2 by 2 matrix. The in_file would be

   2.000000000 3.00000000
   6.000000000 5.00000000

So the code is as follows

#include <stdlib.h>
#include <stdio.h>
#define ELEMENTS 1
#define n 1

double Angstroms[ELEMENTS];
double Energy[ELEMENTS];
double ax[ELEMENTS][n];

void file_input ();
void polynomial ();
// void conversion ();

int main () {
     file_input ();
     polynomial ();
//     conversion ();
   return 0;
 }

void file_input () {

     float a, b;
     int i;

     FILE * in_file = fopen("H2Mini.txt", "r");
     for (i = 0; i <= ELEMENTS; i++) {
          fscanf(in_file, "%f %f\n", &a, &b);
          Angstroms[i] = a;
          Energy [i] = b;
     }
     fclose(in_file);
}

void polynomial () {

      int i;
      FILE * outfile = fopen("PolyTest1.txt", "w");
      if (outfile == NULL) {
         printf ("Error file does not exist");
         exit (-1);
       }

      for (i = 0; i <=ELEMENTS; i++) {
          ax[i][0] = 1;
         // printf ("\n");
          ax[i][1] = Angstroms[i];
          fprintf (outfile, "%.8f %.8f\n", ax[i][0], ax[i][1]);
      }

}

The outfile is making this

 [1.00000000 2.00000000
  1.00000000 1.00000000]

I want the outfile to look like this

 [1.00000000 1.00000000
  2.00000000 6.00000000]

I really don't understand why the outfile is looking like that so any input please let me know.

Suliman Sharif
  • 607
  • 1
  • 9
  • 26
  • I don't understand why are you using functions with global variables, you know you can pass parameters to the functions, don't you? Also it's funny that you check if the file was opened for writing but you don't check for the input file, which is much more likely to fail. And you also need to `fclose()` the output file. – Iharob Al Asimi Apr 29 '15 at 22:10
  • Look at the first line of your last for-loop. `a[i][0] = 1;` Obviously, this is why the value in the first column is 1. It appears as though you've copied someone else's work without understanding it. :shrugs: – enhzflep Apr 29 '15 at 22:11

2 Answers2

4

This code

#define ELEMENTS 1
....
double Angstroms[ELEMENTS];
double Energy[ELEMENTS];

Is making two arrays with one element each (so max index would be 0). When you try to access the element at index 1 of Angstroms, you're getting into undefined behavior. You just happen to have stumbled upon a situation in which it didn't segfault.


Edit: see, for example this question. It looks like you made a similar mistake defining n, which I believe should also be 2.

Furthermore, I don't think that you're going to get the output you said you want when the program runs correctly, I think you'd get

1.00000000 2.00000000
1.00000000 6.00000000

You're setting ax[i][0] to 1 each time, and then printing it as the first number on each line, then printing Angstroms[i], which was the first number on the ith input line. At least if I'm understanding this code right:

ax[i][0] = 1;
ax[i][1] = Angstroms[i];
fprintf (outfile, "%.8f %.8f\n", ax[i][0], ax[i][1]);

Don't be discouraged if it seems confusing!

Community
  • 1
  • 1
sabreitweiser
  • 643
  • 3
  • 13
1

This might help:

#include <stdlib.h>
#include <stdio.h>
#define ELEMENTS 2

double Angstroms[ELEMENTS];
double Energy[ELEMENTS];

void file_input ();
void polynomial ();

int main () {
     file_input ();
     polynomial ();
   return 0;
 }

void file_input () {

     float a, b;
     int i;

     FILE * in_file = fopen("H2.txt", "r");
     for (i = 0; i < ELEMENTS; i++) {
          fscanf(in_file, "%f %f\n", &a, &b);
          Angstroms[i] = a;
          Energy [i] = b;
     }
     fclose(in_file);
}

void polynomial () {

      int i;
      FILE * outfile = fopen("PolyTest1.txt", "w");
      if (outfile == NULL) {
         printf ("Error file does not exist");
         exit (-1);
       }
      fprintf (outfile, "%.8f %.8f\n", 1.0, 1.0);
      for (i = 0; i < ELEMENTS; i++) {
          fprintf (outfile, "%.8f ", Angstroms[i]);
      }
      fclose(outfile);
}
skrtbhtngr
  • 2,223
  • 23
  • 29