GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 04-Nov-2009, 16:17
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road

Sine and Cosine Calculations in C++?


Im trying to generate sample values of x(t), y(t), and z(t) at a sample
rate of 0.01 for "0 less than or equal to" t and "less than or equal to 6"

Basically trying to store the sampled values in their respective files, but kind of lost on where to start?

x(t) = sin(2*pi*t)
y(t) = 2*sin(16*pi*t/3) + cos (2*pi*t/3)
z(t) = x(t)*z(t)

*all i know is this

CPP / C++ / C Code:

#include <math.h>
#include <iostream>
#include <iomanip>
using namespace std;


#define PI 3.14159265

int main ()
{
double rate, result;
rate = 30.0;
result = sin (rate*PI/180);
cout<<"The sine of " << rate << " degrees is " << result <<endl;
return 0;
}

  #2  
Old 04-Nov-2009, 17:07
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: Sine and Cosine Calculations in C++???


You should look into using a "for loop".

CPP / C++ / C Code:
double t;
for(t = 0.0; t <= 6.0; t += 0.01)
{
  // Make calculations based on t

  // Write a data point to the file
}

If you need help writing to a file, let us know.
  #3  
Old 04-Nov-2009, 17:42
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road

Re: Sine and Cosine Calculations in C++???


yes how do you write a data point to a file?

also say for
x(t) = sin(2*pi*t)

would it just be that put into the code?
then
cin>>x(t)

?
  #4  
Old 04-Nov-2009, 18:54
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road
Lightbulb

Re: Sine and Cosine Calculations in C++???


i think its wrong tho,

this is what i got



runs

but doesn't show anything
blank screen (*command center)

how do i write those calculations and write the data points to the file?

CPP / C++ / C Code:

#include <iostream>
#include <cmath>
#include <fstream>
#include <stdlib.h>
#include <cmath>
using namespace std;




int main()
{

double *p = new double[601];
// open your file

int i=0;
for(double t = 0.0; t <= 6.0; t += 0.01)
{
    p[i] = sin(t);
    i++;

}


   
delete p;


    cin.ignore();
    cin.get();
    return 0;
}


  #5  
Old 04-Nov-2009, 20:49
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road

Re: Sine and Cosine Calculations in C++?


also another attempt

this is what i have so far:
doesn't run properly though.

also im trying to store the sampled values in their respective files and use excel to plot the sampled x(t), y(t), and z(t)...how do i do that

my code:

CPP / C++ / C Code:

// Text File Writing (SIN and COS).cpp : main project file.

define _USE_MATH_DEFINES
#include <math.h>
#include <iostream>
#include <iomanip>

using namespace std;




int main()
{

FILE *f;
double x,y,z,t;

f=fopen("c:/data.txt","a+t");
     ///is this where my excel file goes??

for(t = 0.0; t <= 6.0; t += 0.01)
{
  // Make calculations based on t
  x = sin(2*M_PI*t);
  y = 2*sin(16*M_PI*t/3) + cos (2*M_PI*t/3);
  z = x*y; 
  // Write a data point to the file
  fprintf(f,"%.5lf %.5lf %.5lf\n",x,y,z); //5 decimal places of precision
  //want to change this to cout? how do i do this?

}

fclose(f);

  #6  
Old 05-Nov-2009, 00:02
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road

Re: Sine and Cosine Calculations in C++?


can anyone assist me with this program?
  #7  
Old 05-Nov-2009, 08:48
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,218
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Sine and Cosine Calculations in C++?


Quote:
Originally Posted by sl02ggp
lost on where to start...

Look in your text or other course materials or some tutorial on the web. Here's where I might start:http://www.cplusplus.com/doc/tutorial/


If you want to jump to file I/O, you can look at the bottom and see this link: http://www.cplusplus.com/doc/tutorial/files/

However, I would start from the beginning and work my way through the entire thing. There are many examples and many principles that you might not discover trying to pick things up on your own.

CPP / C++ / C Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>

// Some math headers define M_PI for you; some do not
#ifndef M_PI
const double M_PI = 3.14159265358979323846;
#endif

using namespace std;

int main()
{

    double x, y, z, t;
    double omega = 2.0 * M_PI;
    char *outname = "data.txt";
    ofstream outfile(outname);
    if (!outfile) {
        cout << "Can't open file " << outname << " for writing." << endl;
        exit(EXIT_FAILURE);
    }
    cout << "Opened file " << outname << " for writing." << endl;

    outfile << fixed << setprecision(5);
    cout    << fixed << setprecision(5);

    for (t = 0.0; t <= 6.0; t += 0.01) {
        // Make calculations based on t
        x = sin(omega * t);
        y = 2 * sin(8.0*omega * t / 3.0) + cos(omega * t / 3.0);
        z = x * y;

        // Show the output stuff on the console
        // After debugging, you may want to comment out the following line
        // I'll write the value of t as well as the computed
        // values
        cout << t << " " << x << " " << y << " " << z << endl;

        // Write a data point to the file
        // This puts spaces between the fields.  If you want
        // commas or tabs, then use "," or "\t" between
        // the output values instead of " "
        outfile << t << " " << x << " " << y << " " << z << endl;

    }
    outfile.close();
    return 0;
}

Regards,

Dave
  #8  
Old 11-Nov-2009, 18:31
sl02ggp sl02ggp is offline
New Member
 
Join Date: Oct 2009
Posts: 25
sl02ggp is on a distinguished road

Re: Sine and Cosine Calculations in C++?


thank you! and also what does it mean excel plots?

just want to state the problem again:

Write a C++ program to generate the sampled values of x(t), y(t), and z(t) at the sample rate of 0.01 for 0 less than or equal to t less than or equal to 600. Store the sampled values in their respective files and use Excel program to plot the sampled x(t), y(t), and z(t).

x(t) = sin (2*pi*t)
y(t) = 2*sin (16*pi*t/3) + cos (2*pi*t/3)
z(t) = x(t) * y(t)

Do not use for loop:
for (t=0; t less than 600; t=t+0.01)
instead
use for loop:
for (i=0; t less than 600; i++)
{

}

*Send this character to file '/t'
x[i] << '/t' << y[i] << '/t' << z[i] << endl;
  #9  
Old 11-Nov-2009, 22:33
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 802
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Sine and Cosine Calculations in C++?


Quote:
Store the sampled values in their respective files and use Excel program to plot the sampled...
That means take the file you program writes and open it in Excel.
(I have found that Excel will import a .csv file without being wizzed on.)
When you do that the data will be spread across cells of a row.
You can then select that row as data to be plotted in a chart.
As for the rest of the drool... it won't help to smoke another bowl...
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sine versus pythagoras juned C++ Forum 1 14-Dec-2008 09:17
Angle between vectors mosquito C++ Forum 14 21-Mar-2008 15:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:12.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.