Hello folks,
I need a small help for writing the varible x[i][j] at each iteration but I do not know how to do that. I have a function to update the value of x[i][j] and a main function I call this function.
int main()
{
for (int i=0, i<iter_limit,++i)
{
updateX() // I call the the function which updates the x[i][j]
}
return 0;
}
I want to write the values of x[i][j] at each iteration in a txt file. How can I do that ? Thank you so much
Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.
You can use the preview button at the bottom to see how it looks.
I found the second link to be the most help.
You could start by showing all of your code. There is a good chance that your update function can be modified to write to a file. If not yo could at least show what you have tried and give a better idea of what you know.
Somewhere you need to define a file stream variable and open a file. This could be in "main" or in the function. Most likely in a write function.
#include <fstream>
int main()
{
constint M = 5;
constint N = 7;
int x[M][N] {};
// filling in with some example values
for (int i = 0; i < M; i++)
for (int j = 0; j < N; j++)
{
x[i][j] = (i + 2)*(j + 3) % 17;
}
std::ofstream fout("file.txt");
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
fout << x[i][j] << ' ';
}
fout << '\n';
}
return 0;
}
Thank you Ganado, I guess you understood what I want . I do similar thing but it Always writes the values of x[i][j] in the latest iteration . However, the format I want is in the txt file:
int main()
{
...
ofstream fout("file.txt"); // Open the file BEFORE the iteration loop
for (int i=0, i<iter_limit,++i)
{
updateX( x, ... ); // call the function which updates x[]][]
writeX( x, fout, ... ); // write to stream fout
}
}