# Homework Help: Can't write calculated resulted to file in C++

1. Sep 20, 2010

1. The problem statement, all variables and given/known data

Dear physics forum,

I'm start learning C++ and I'm trying to program a space transform function for linearized convection-diffusion problem using Code:block
Everything was fine until I wrote calculated result into a file. When I run the program, some error message popped up which related to "memory could not be read" and std:stream::sentry::sentry() in debug windows

Code (Text):

#include <iostream>
#include <fstream> // for working with file
#include <cmath>
#include <iomanip>
#include <stdio.h>
#include <math.h>
#include <cstdlib> // for exit function

using namespace std;

// Function for calculating exponental part of transfer function //
double  expo(double dx, double celerity, double time, double dif)
{
double result;
result = -(dx - celerity * time );
result = exp ( pow (result, 2.0) / ( 4.0 * dif * time ));
return (result);
}

// Main program
int main ()
{
// Creat a file to store the result
ofstream outdata;
outdata.open ("Result.txt", ios::out);

if( !outdata ) {                                     // file couldn't be opened
cerr << "Error: file could not be opened" << endl;
exit(1);
}

outdata << "OK??" << endl;

// Declare variables
double  PI = 3.14159,
u  = 1.5,
c,
D  = 50.0,
diffu = 0.0,
t,
t0 = 0.0,
deltat = 1.0,
deltax = 0.5;

// Introduce number of interval
int     n  = 50;

// Create an array to store results of transfer function
double  transfer[50] = {0.0};

// Approximate c by u
c = 5.0/3.0 * u;

cout << "Time" << setw (21) << "Transfer function" << endl;

// Loop for calculating transfer function
for (int i = 1; i <= n; i++) {
t = t0 + deltat * i;
transfer[i] = deltax / ( 2.0 * sqrt( PI * D * t ));         // Calculate convection part
diffu = expo(deltax, c, t, D);                              // Calculate diffusion part
transfer[i] = transfer [i] * diffu;                         // Put 2 parts together

cout << setw(2) << t << setw(18) << transfer[i] << endl;    // Print out results
}

for (int i = 1; i <= n; ++i) {
outdata << transfer[i] << endl;
}

outdata.close();

// Terminate the program
return 0;
}

2. Relevant equations

T(t,deltax) = $$\frac{deltax}{2\sqrt{\Pi*D*t^{3}}}*e^{-\frac{(deltax - c*t)^2}{4*D*t}$$

Plz help me with this. Thank you very much for your time

Regards,

1. The problem statement, all variables and given/known data

2. Relevant equations

3. The attempt at a solution

2. Sep 20, 2010

### D H

Staff Emeritus
Arrays are index from 0 in C and C++. The valid indices into the array "transfer" are 0 to 49. You are setting and then accessing elements 1 to 50.

3. Sep 20, 2010

### Staff: Mentor

Your for loop runs from i = 1 to 50, and your transfer array is declared to have 50 members.

In C and C++, arrays are zero-based, which means that the elements of your array are transfer[0], transfer[1], ..., transfer[49]. Your for loop is attempting to write to transfer[50], which is outside your array. I believe that's what is causing your problem.

Code (Text):
for(i = 0; i < n; i++)
{
// body of loop
}

4. Sep 20, 2010

You guys are absolutely right! This solves my problem now !

Thank you again !

Regards,

5. Sep 20, 2010

### D H

Staff Emeritus
Even better, use
Code (Text):
for(i = 0; i < n; ++i)
{
// body of loop
}

In this case use of post-increment versus pre-increment doesn't really matter. However, as soon as you start using iterators, the post-increment operator can be quite expensive compared to the pre-increment operator. In C it is fairly standard to use post-increment by default, deferring the use of pre-increment as a signal that the programmer is being tricky. In C++ it is the other way around.

6. Sep 21, 2010

Could you also tell me which one is better:

pow (a, 3) or a * a * a

Thanks

Have a nice day every1

7. Sep 22, 2010

### fizzynoob

well of course a*a*a will be faster since it does not have to deal with the general case in pow(). Micro optimizing like this is rarely needed and when it does it is a last move optimization that is carefully profiled. If your you really want to optimize your code heres a few things to try to accomplish:

write better algorithms,
reduce dynamic memory (when possible),
ect..

Trying to micro optimize your code at such a level will actually cause a decrease in code readability, which is a bigger con than having a minimal optimization

8. Sep 22, 2010

### D H

Staff Emeritus
Unfortunately, it is a*a*a. C and C++ lack an exponentiation operator and the pow() function is a very poor substitute for it. With most compilers, pow(a,3) is equivalent to exp(3*log(a)). This is one of the two key issues that makes some Fortran programmers think that C and C++ are terrible languages for numerical programming.

9. Sep 23, 2010

Thank you guys for your info.

MT