saving a series of cos terms in a txt file

hi

i have wrote this piece of code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;

int main()
{
    ofstream function_stream ("some_function.txt") ;
    char x ;
    double some_function = 0 ;
    for (int i = 0 ; i<100 ; i++)
    {
        some_function = ((i+2.)/(i+1.))*cos(i*x)+some_function ;
    }
    function_stream << some_function << endl ;
    return 0;
}


i want to have some_function in a txt file in terms of cos terms with argument x.

but when i run this code in the some_function.txt i just find "0.713623" not a series of cos terms with argument of x.

how can i do this?

Thanks

Your for loop calc's a number. when it finishes, it writes the number to the file. So you need to put the bit that does the writing inside the loop. If you did that now it would give a running total, which is maybe not what you want.

Also you can use the += operator.

Now think about what is going on here

char x ;

int i = 0

*cos(i*x)

What is the type of the argument for cos()? Look it up.

Also, what do you understand about initialisation of variables and variable types?

so according to your hints i think the type of the argument of cos is ambiguous .

in other words i think you are saying that because the type of "i" is int and the type of "x" is char c++ converts their multiplication into an int type.

am i right?

but still i can not figure out how to save that series in terms of x.
so according to your hints i think the type of the argument of cos is ambiguous .


What I meant was, what is the type expected by the cos maths library function. You really shouldn't use functions without finding out how they work.

I think you are confused about different variable types and how they work.

type of "i" is int and the type of "x" is char c++ converts their multiplication into an int type.


A char is really a small int that happens to represent a char. So when i is multiply by x (which isn't set to anything) the answer is an int, but then the compiler converts this to the another type, which is what the cos function is expecting.

The fact that x wasn't set, make me wonder how it even compiled, let alone run.

You are try to output some thing like :

[output]
2.0x 1.5x 1.33x
/output]

How you are doing it, is completely wrong. To print the x character, use the stream, not try to put it in the cos function.

But you need to sort out the cos function first. Another thing what is the value expected by the cos function - Hint: it's not degrees.
Replace the line in the loop in the function that goes:
some_function = ...+some_function;
with
function_stream << ((i+2.)/(i+1.)) << "*cos(" << i << "*x)+";

anything that isn't enclosed in "" will be evaluated as a number in this case.

making the code I've put up prettier but hopefully you get the main idea.

Like TheIdeasMan said, you need to be aware of the types of the variables you're using, and remember that what will be used during evaluation is the value stored by the variable. In your code x isn't set, for basic types like char the compiler might not complain but the value is undefined, typically it will be 0 but it could be anything. You want to write char x = 'x';
@spaggy: You gave the OP the answer, I was hoping he would figure it out himself with the clues.
Topic archived. No new replies allowed.